Let's talk about Singleton in the PHP Singleton mode. The Singleton mode is a creation mode. it is the simplest mode in the design mode. of course, it is used everywhere. The Singleton mode ensures that one class has only one instance, and provides the Singleton mode as the creation mode. it is the simplest mode in the design mode, and of course it is used everywhere.
The Singleton mode ensures that a class has only one instance and provides a global access point to access it.
When you need to control the number of instances of a class and the caller can access it from a public, well-known access point, we can consider using the Singleton mode.
We use UML to design the Singleton mode. of course, we will use UML to describe our design in the design part of the future design mode, which is more visualized.
From the UML Design diagram, we can see that in order for a class to have only one Instance, it must create a static variable, and then we use a public static Instance () method to create it, however, to avoid the class's own constructor being able to create objects, we set the constructor to protected or private, so that the external can only use Instance () to create a static Singleton class. It seems that we have achieved our goal. next we will look at the code:
public class Singleton { private static Singleton instance; protected Singleton() public static Singleton Instance() { if(instance != null) instance = new Singleton(); return instance; }}
From this point of view, we can do the following steps to implement the Singleton mode:
- Create a static variable in the class. the variable type is the current class;
- Create a public static method in the class so that you can use this method to create such static objects;
- Finally, set the constructor to protected or private.
Program List: the simplest singleton class
Program List: Scalable singleton class
An Extensible singleton class seems impossible, but the following program is very close to this effect.
rndId = rand(); } public function whatAmI(){ echo 'I am a Apple('.$this->rndId.')
'; } public static function getInstance(){ return Fruit::getSingleton(get_class()); }}class GreenApple extends Apple { public function whatAmI(){ echo 'I am a GreenApple('.$this->rndId.')
'; } public static function getInstance(){ return Fruit::getSingleton(get_class()); }}$apple1 = Apple::getInstance();$apple2 = GreenApple::getInstance();$apple1->whatAmI();// should echo 'I am a A(some number)$apple2->whatAmI();// should echo 'I am a B(some number)$apple1 = Apple::getInstance();$apple2 = GreenApple::getInstance();$apple1->whatAmI();// should echo 'I am a A(same number as above)$apple2->whatAmI();// should echo 'I am a B(same number as above)// $a = new A();// this should fail// $b = new B();// this should fail?>
Program running result:
I am a Apple(4462)I am a GreenApple(8207)I am a Apple(4462)I am a GreenApple(8207)
Program List: singleton class and its derived class
showColor();?>
Program running result:
I am constructedMy Color is Red.
Bytes. The Singleton mode ensures that one class has only one instance and provides...