1. 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:
Copy codeThe Code is as follows: 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 );
2. 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:
Copy codeThe Code is as follows: 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.
Copy codeThe Code is as follows: // define the common function of the shape: Get the length of week and area.
Interface IShape {
Function getCircum ();
Function getArea ();
}
// Define the rectangle class
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 circle class
Class Circle implements IShape {
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.
3. 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.
Copy codeThe Code is as follows: // observer Interface
Interface IObserver {
Function onListen ($ sender, $ args );
Function getName ();
}
// Interface to be observed
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 to be observed
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 a class that can be observed: RadioStation
Class RadioStation extends Observable {
Public function addListener ($ listener ){
Foreach ($ this-> observers as $ observer ){
$ Observer-> onListen ($ this, $ listener );
}
}
}
// Simulate an observer class
Class RadioStationLogger extends Observer {
Protected $ name = 'logger ';
Public function onListen ($ sender, $ args ){
Echo $ args, 'join the radiostation. <br/> ';
}
}
// Simulate another observer class
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 ');