PHP design mode-Singleton mode

Source: Internet
Author: User
The Singleton mode refers to the design mode in which the entire application class has only one object instance. It is usually used to create objects, ensure that a class has only one instance, and instantiate and provide this instance to the entire system. It is a common design mode. in computer systems, drivers of thread pools, caches, log objects, dialog boxes, printers, database operations, and graphics cards are often designed as Singleton.

Introduction

The Singleton mode refers to the design mode in which the entire application class has only one object instance. It is usually used to create objects, ensure that a class has only one instance, and instantiate and provide this instance to the entire system.

It is a common design mode. in computer systems, drivers of thread pools, caches, log objects, dialog boxes, printers, database operations, and graphics cards are often designed as Singleton.

Features

A class has only one instance in the entire application.

Class must create this instance by itself

You must provide this instance to the entire system.

Function

The biggest function of Singleton mode is to reduce system overhead and save resources. For example, in daily development using php, we often need to connect to various relational and non-relational databases such as MySQL or redis multiple times. if we do not use the Singleton mode, when you create a new instance multiple times, a large amount of memory and system resources are consumed.

Key points

1. a static member variable is required to save the unique instance of the class.

private static $_instance;

2. constructor and clone functions must be declared as private to prevent external programs from losing the meaning of Singleton mode due to new classes.

// Privatized construction method to prevent new instance private function _ construct () {$ this-> _ db = pg_connect ('demo');} // overwrite _ clone () method to prohibit cloning private function _ clone (){}

3. a public static method (usually the getInstance method) must be provided to access this instance to return a reference of a unique instance.

// Return the public static method of the object instance public static function getInstance () {// Determine whether the current instance has been created if (! (Self: $ _ instance instanceof self) {// if no, create self ::$ _ instance = new self () ;}// finally return the instance return self:: $ _ instance ;}

Explanations

Next I will take a MySQL operation class as an example to show you how to write it.

Code:

 _ Db = pg_connect (...);} // overwrite the _ clone () method. do not clone private function _ clone () {}// return the public static function getInstance () of the object instance () {// Determine whether the object instance if (! (Self: $ _ instance instanceof self) {// if it does not exist, create and save self ::$ _ instance = new self () ;}// return the object instance return self :: $ _ instance;} // Other methods public function addUserInfo (...) {} // Other methods public function getUserInfo (...) {}}// demo $ db = Db: getInstance (); $ db-> addUserInfo (...); $ db-> getUserInfo (...);

Summary

I have talked about the content of so many Singleton modes, but it does not mean that it has no disadvantages. below I will list the advantages and disadvantages of using Singleton mode. you can choose to use it based on your actual needs.

Advantages and disadvantages of Singleton mode:

Advantages:

Improve System design

Is an improvement for global variables

Saves system resource overhead

Disadvantages:

Difficult to debug

Hidden dependency

An error type cannot be used to overwrite a singleton.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.