1. Single Case mode
The so-called singleton pattern, which is that at any time, an instance of this class exists in the application. Often, we use a single case pattern to allow only one object to access the database, thereby preventing multiple database connections from being opened. To implement a single example class, you should include the following points:
Unlike ordinary classes, a singleton class cannot be instantiated directly, but only by itself. Therefore, to obtain such a limit effect, the constructor must be marked private.
For a singleton class to work without being directly instantiated, you must provide an instance of it. Therefore, you must have a singleton class that has a private static member variable that holds an instance of the class and a corresponding public static method that accesses the instance.
In PHP, in order to prevent the cloning of a single instance class object from breaking the above implementation form of a single instance class, it is often also provided with an empty private __clone () method for the base.
The following is a basic single example pattern:
Copy Code code 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 mode
The factory pattern is that you can create a class that is specifically designed to implement and return an instance of another class, depending on the input parameters or the application configuration. The following is a basic factory model:
Copy Code code as follows:
Class Factorybasic {
public static function Create ($config) {
}
}
For example, this is a factory that describes Shape objects, and it wants to create different shapes based on the number of parameters passed in.
Copy Code code as follows:
Define the public function of a shape: get the perimeter and area.
Interface IShape {
function Getcircum ();
function Getarea ();
}
Defining a rectangular 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;
}
}
To define a 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 parameters passed in.
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 ());
Round Object
$o = Factoryshape::create (2);
Var_dump ($o->getarea ());
Using Factory mode makes it easier to invoke a method, because it has only one class and one method, if you do not use Factory mode, decide which class and which method should be invoked at the time of the call, and use Factory mode to make it easier to make changes to the application in the future, such as adding support for a shape, You only need to modify the Create () one method in the factory class, not the Factory mode, to modify the code block that calls the shape.
3. Observer mode
The Observer pattern gives you another way to avoid tight coupling between components. The pattern is simple: an object makes itself observable by adding a method that allows another object, the observer, to register itself. When an observable object changes, it sends the message to the registered observer. The actions that these observers perform with this information are independent of the observable objects. The result is that objects can talk to each other without having to understand why.
A simple example: when the listener listens to the radio (that is, the radio joins a new audience), it sends out a message that can be observed by the log viewer who sent the message.
Copy Code code as follows:
Observer interface
Interface IObserver {
function Onlisten ($sender, $args);
function GetName ();
}
can be observed interface
Interface IObservable {
function Addobserver ($observer);
function Removeobserver ($observer _name);
}
Observer class
Abstract class Observer implements IObserver {
protected $name;
Public Function GetName () {
return $this->name;
}
}
can be observed class
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 Observer
$rs->removeobserver (' other ');
You can see the observed information
$rs->addlistener (' CCTV ');