- Singleton Mode
The Singleton Mode means that only one instance of this class exists in the application at any time. Generally, we use the singleton mode to allow only one object to access the database, thus preventing multiple database connections from being opened. To implement a singleton class, you must include the following:
- Unlike normal classes, a singleton class cannot be directly instantiated, but can only be instantiated by itself. Therefore, to obtain such a restricted effect, the constructor must be marked as private.
- To make a singleton class effective without being directly instantiated, you must provide such an instance for it. Therefore, the singleton class must have a private static member variable that can save the class and a public static method that can access the instance.
- In PHP, to prevent the singleton class object from being cloned to break the preceding Implementation form of Singleton classes, an empty private _ clone () method is also provided for the basics.
Below is a basic Singleton mode:
class SingetonBasic { private static $instance; // other vars.. private function __construct() { // do construct.. } private function __clone() {} public static function getInstance() { if (!(self::$instance instanceof self)) { self::$instance = new self(); } return self::$instance; } // other functions..}$a = SingetonBasic::getInstance();$b = SingetonBasic::getInstance();var_dump($a === $b);
- Factory Model
The factory mode allows you to create a class dedicated to implementation and return other classes based on input parameters or application configurations. Below is a basic factory model:
class FactoryBasic { public static function create($config) { }}
For example, this is a factory that describes shape objects. It wants to create different shapes based on the number of input parameters.
// Define the common function of the shape: Obtain the length and area of the week. Interface ishape {function getcircum (); function getarea () ;}// defines the class rectangle implements ishape {private $ width, $ height; public function _ construct ($ width, $ height) {$ this-> width = $ width; $ this-> Height = $ height;} public function getcircum () {return 2 * ($ this-> width + $ this-> height);} public function getarea () {return $ this-> width * $ this-> height ;}} // define the circular class circle implements I Shape {private $ radii; public function _ construct ($ radii) {$ this-> radii = $ radii;} public function getcircum () {return 2 * m_pi * $ this-> radii;} public function getarea () {return m_pi * POW ($ this-> radii, 2 );}} // create different shapes based on the number of input parameters. Class factoryshape {public static function create () {Switch (func_num_args () {Case 1: return new circle (func_get_arg (0); break; Case 2: return new rectangle (func_get_arg (0), func_get_arg (1); break ;}}// rectangular object $ c = factoryshape: Create (4, 2 ); var_dump ($ C-> getarea (); // circle object $ o = factoryshape: Create (2); var_dump ($ o-> getarea ());
Using the factory mode makes it easier to call a method because it only has one class and one method. If the factory mode is not used, it is necessary to decide which class and method should be called during the call; using the factory mode makes it easier to change the application in the future, such as adding a shape of support, you only need to modify the CREATE () method in the factory class, instead of using the factory mode, you need to modify the code block that calls the shape.
- Observer Mode
The observer mode provides another way to avoid close coupling between components. This mode is very simple: an object becomes observability by adding a method (This method allows another object, that is, the observer registers itself. When an observed object changes, the message is sent to the registered observer. The operations performed by these observers using this information are irrelevant to the observed objects. The result is that objects can communicate with each other without understanding the cause.
A simple example: When a listener is listening to a station (that is, a new listener is added to the station), it sends a prompt message, which can be observed by the log observer who sends the message.
// Observer interface iobserver {function onlisten ($ sender, $ ARGs); function getname () ;}// interface iobservable {function addobserver ($ observer ); function removeobserver ($ observer_name);} // observer class abstract class observer implements iobserver {protected $ name; Public Function getname () {return $ this-> name ;}} // class observable implements iobservable {protected $ Observers = array (); Public Function addobserver ($ observer) {If (! ($ Observer instanceof iobserver) {return ;}$ this-> observers [] = $ observer;} public function removeobserver ($ observer_name) {foreach ($ this-> observers as $ index =>$ observer) {if ($ observer-> getname () ===$ observer_name) {array_splice ($ this-> observers, $ index, 1); Return ;}}// simulate an observed class: radiostationclass radiostation extends observable {public function addlistener ($ listener) {foreach ($ this-> observers as $ observer) {$ observer-> onlisten ($ this, $ listener) ;}}// simulate an observer class radiostationlogger extends observer {protected $ name = 'logger'; Public Function onlisten ($ sender, $ ARGs) {echo $ ARGs, 'join the radiostation. <br/> ';}}// simulate another observer class otherobserver extends observer {protected $ name = 'other'; Public Function onlisten ($ sender, $ ARGs) {echo 'other observer .. <br/> ';}}$ rs = new radiostation (); // inject the observer $ RS-> addobserver (New radiostationlogger ()); $ RS-> addobserver (New otherobserver (); // remove the observer $ RS-> removeobserver ('other '); // you can see the observed information $ RS-> addlistener ('CCTV ');