Home > Article > Backend Development > php design pattern (reprinted)
Original link: http://www.cnblogs.com/siqi/archive/2012/09/09/2667562.html
1. Singleton mode
Why use PHP singleton mode
/**
* Singleton mode of design pattern
* $_instance must be declared as a static private variable
* The constructor must be declared as private to prevent external programs from new classes and thus lose the meaning of the singleton mode
* getInstance The () method must be set to public, and this method must be called to return a reference to the instance
* :: The operator can only access static variables and static functions
* new objects will consume memory
* Usage scenarios: The most commonly used places are Database Connectivity.
* After using the singleton pattern to generate an object, the object can be used by many other objects.
*/
class man
{
//Save the instance in this attribute private static $_instance;
//The constructor is declared as private, Prevent direct creation of objects private function __construct()
';
} //
Single case method public static function get_instance() (self::$_instance
));
if(!isset(self::$_instance))
return
self::$_instance; } } function
test()
{ echo("test"); }
}
// This way of writing will go wrong because the constructor is declared as private//$test = new man;
//The following will get the Example class Singleton object
$test = man::get_instance();$test = man::get_instance();
$test
->test();// The copied object will Resulting in an E_USER_ERROR.
//$test_clone = clone $test;
2. Simple factory pattern
①Abstract base class: Define some abstract methods in the class, which are used to Implemented in subclasses
②Subclasses that inherit from abstract base classes: implement abstract methods in base classes
③Factory class: used to instantiate all corresponding subclasses
classOperation{ //
Abstract method Cannot contain function body
/*
* * Addition class getValue($num1
,$num2){ +$num2; } } }/** * Subtraction class */ OperationSub
extends
Operation {
,$num2){
* Multiplication class
operation ; Division class 2
){ num2 == 0) { Throw
exception ("The division cannot be 0"); catch (Exception
$e){ } }
By using object-oriented inheritance features, we can easily extend the original program, such as: 'power', 'square root', 'logarithm', 'trigonometric function', 'statistics', etc. , to also avoid loading unnecessary code.
If we need to add a remainder class now, it will be very simple
We only need to write another class (this class inherits the virtual base class) and complete the corresponding functions in the class (for example: finding the power operation), and greatly reduces the degree of coupling, which is convenient for future maintenance and expansion
class
OperationRem
extends
Operation { %$num12
;
There is still an unresolved problem, which is how to let the program instantiate the corresponding object according to the operator input by the user? Solution: Use a separate class to implement the instantiation process. This class is the factory
/** * Engineering class, mainly used to create objects * Function: According to By inputting the operation symbols, the factory can instantiate the appropriate object
ate
){
$operate){ break
; .
} } $test
=Factory::createObj('/'); $result=$test
->getValue(23 ,0);
echo
$result;
Other notes about this mode:
Factory mode:
Take transportation as an example: Please be able to customize both the transportation and the production process of the transportation
1> Customize the transportation
1. Define an interface, which contains the methods of handover tools (start, run and stop)
2. Let airplanes, cars and other classes implement them
2> Customization factory (similar to above)
1. Definition An interface that contains the manufacturing method of delivery tools (start, run and stop)
2. Write factory classes for manufacturing aircraft and cars respectively to inherit and implement this interface
Original address: http://bbs.phpchina. com/thread-242243-1-1.html
3. Observer Pattern
The observer pattern is a behavioral pattern that defines a one-to-many dependency relationship between objects so that when the state of an object occurs When it changes, all objects that depend on it are notified and refreshed automatically. It perfectly separates the observer object and the observed object. A list of dependencies (observers) that are interested in the principal can be maintained in a separate object (the principal). Have all observers individually implement a common Observer interface to eliminate the direct dependency between the principal and dependent objects. (I can’t understand it anyway)
Used spl (standard php library)
class MyObserver1 implements SplObserver {
Reference original text: http://www.php.net/manual/zh/class.splsubject.php
4. Strategy mode
In this mode, algorithms are extracted from complex classes and can thus be easily replaced . For example, if you want to change the way pages are ranked in search engines, Strategy mode is a good choice. Think about the parts of a search engine ——One part traverses the pages, one part sorts each page, and the other part sorts based on the results of the arrangement. In complex examples, these parts are all in the same class. By using the Strategy pattern, you can put the arrangement part into another class to change the way the page is arranged without affecting the rest of the search engine's code.
As a simpler example, shown below is a User List class that provides a way to find a set of users based on a set of plug-and-play policies
//Define interface interface IStrategy {