PHP design-singleton mode and factory mode, php design factory
1. The Singleton mode is also called the responsibility mode. It is used to create an access point for a single function in a program. In other words, the instantiated object is unique.
All Singleton modes have at least three common elements:
1. They must have a constructor and must be marked as private
2. They have a static member variable that saves the class instance.
3. They have a public static method to access this instance.
A singleton class cannot be directly instantiated in other classes, but can only be instantiated by itself. It does not create instance copies, but returns a reference to the instance stored in the singleton class.
Singleton mode instance
<? Php
Class Single {private $ name; // declare a private instance variable private function _ construct () {// declare a private constructor to prevent external code from using new to create an object. } Static public $ instance; // declare a static variable (the only instance saved in the class) static public function getinstance () {// declare a static getinstance () method, used to check whether there is an instance object if (! Self: $ instance) self ::$ instance = new self (); return self ::$ instance;} public function setname ($ n) {// assign a value to the variable $ name
$ This-> name = $ n;
} Public function getname () {// get the value of the variable $ name
Return $ this-> name;
} $ A = Single: getinstance (); $ B = Single: getinstance (); $ a-> setname ('Hello World '); $ B-> setname ('good morning'); echo $ a-> getname (); // good morning echo $ B-> getname (); // good morning
?>
2. The factory mode is a type and has some methods for creating objects for you. In this way, you can use the factory class to create objects without directly using new.
To change the type of the created object, you only need to change the factory.
<? Php
// Factory class (operator operations)
Class factory {
Public static function dx ($ ope ){
Switch ($ ope ){
Case "+ ":
Return new plus ();
Break;
Case "-":
Return new sub ();
Break;
Case "% ":
Return new rem ();
Break;
}
}
}
$ Ope = factory: dx ("+ ");
$ Ope-> a = 20;
$ Ope-> B = 10;
Echo $ ope-> opera ();
Factory mode instance
Class Factory {// create a basic Factory class static public function fac ($ id) {// create a static method that returns the object instance if (1 = $ id) return new A (); elseif (2 = $ id) return new B (); elseif (3 = $ id) return new C (); return new D () ;}} interface FetchName {// create an interface public function getname (); //} class A implements FetchName {private $ name = "AAAAA "; public function getname (){
Return $ this-> name;
} Class C implements FetchName {private $ name = "CCCCC"; public function getname (){
Return $ this-> name;
} Class B implements FetchName {private $ name = "BBBBB"; public function getname (){
Return $ this-> name;
} Class D implements FetchName {private $ name = "DDDDD"; public function getname (){
Return $ this-> name;
}
} $ O = Factory: fac (6); // call the method if ($ o instanceof FetchName) {echo $ o-> getname () in the Factory class (); // DDDDD} $ p = Factory: fac (3); echo $ p-> getname (); // CCCCC
?>