PHP policy mode and php policy Mode
Policy mode: defines a series of algorithms, encapsulates each algorithm, and enables them to replace each other. This mode allows algorithms to change independently of customers who use it. The rule mode separates objects from operation rules and has powerful functions, because the core idea of this design mode is the concept of object-oriented programming.
That is, we plan to travel. We can consider several strategies, such as cycling, cars, trains, and airplanes.
[Primary role in policy mode]
Context: A ConcreteStrategy object is used for configuration. Maintain a reference to the Strategy object. You can define an interface to allow Strategy to access its data. (For outgoing travel (interface class or abstract class), transport must consider the time (method to be implemented by the interface class or abstract class) and the cost)
Abstract Strategy: defines the public interfaces of all supported algorithms. Context uses this interface to call a ConcreteStrategy-defined algorithm. (For environmental implementation, select the transportation scope, train, airplane, bicycle ...)
ConcreteStrategy: a specific algorithm is implemented using the Strategy interface. (The information about the selected transportation mode is provided)
[Policy mode PHP example]
# Context interface TravelTool {public function useTime (); public function money () ;}# Strategy class Plane implements TravelTool {# airplane public function useTime () {return '1 H';} public function money () {return '$ 10';} class Train implements TravelTool {# Train public function useTime () {return '3 H';} public function money () {return '$ 5' ;}# a specific policy class (ConcreteStrategy) class doTravel {public $ obj; public function _ construct ($ method) {$ temp = new ReflectionClass ($ method ); # I saw other blogs with reflection and learned the http://www.php.net/manual/zh/book.reflection.php $ this-> obj = $ temp-> newInstance ();} public function money () {echo $ this-> obj-> money ();} public function useTime () {echo $ this-> obj-> useTime ();}} $ obj = new doTravel ('train'); $ obj-> money (); echo "<br/>"; $ obj-> useTime ();
For more detailed content (advantages, disadvantages, applicability) See here reprinted on http://www.howzhi.com/group/php/discuss/3456