This article mainly introduces the Adapter mode in php design mode, and uses php to implement the Adapter mode. if you are interested, refer to it.
I. intention
Define a series of algorithms, encapsulate them one by one, and make them replaceable. Policy mode allows algorithms to change independently of customers who use it.
Algorithms are used to change the rule mode.
II. Structure of policy mode
III. main roles in policy mode
Abstract Strategy role: defines the public interfaces of all supported algorithms. It is usually implemented using an interface or abstraction. Context uses this interface to call the algorithm defined by ConcreteStrategy
ConcreteStrategy: a specific algorithm is implemented using the Strategy interface.
Environment (Context) role: Holds a reference to the Strategy class and uses a ConcreteStrategy object to configure
IV. Advantages and disadvantages of the policy model
Advantages of the policy mode:
1. the policy mode provides methods for managing related algorithm families.
2. the policy mode provides a way to replace the inheritance relationship. it is closed in an independent Strategy class so that you can change it independently of its Context.
3. use policy mode to avoid using multiple conditional transfer statements.
Disadvantages of Rule mode:
1. the customer must understand all policies. this is a potential disadvantage of the policy model.
2. communication overhead between Strategy and Context
3. the policy mode will cause many strategies.
V. applicable scenarios of policy modes
1. many related classes only have different behaviors. "Policy" provides a method to configure a class with one of multiple actions.
2. different variants of an algorithm are required.
3. algorithms use data that customers should not know. Policy modes can be used to avoid exposing complex, algorithm-related data structures.
4. a class defines multiple actions, and these actions appear in multiple forms in the operations of this class. Remove related condition branches and their respective Strategy classes to replace these condition statements
VI. policy mode and other modes
Template Mode:The difference between the template method mode and the policy mode is that the policy mode uses the delegate method to provide different algorithm behavior, while the template method uses the inherited method to provide different algorithm behavior.
Enjoy the metadata mode (flyweight mode ):If multiple client objects need to call the same unique strategy class, you can enable them to enjoy the metadata mode.
VII. policy mode PHP example
<? Php/*** abstract policy role to implement */interface Strategy {/*** Algorithm interface */public function algorithmInterface ();} /*** specific policy role A */class ConcreteStrategyA implements Strategy {public function algorithmInterface () {echo 'algorithminterface
';}}/*** Specific policy role B */class ConcreteStrategyB implements Strategy {public function algorithmInterface () {echo 'algorithminterface B
';}}/*** Specific policy role C */class ConcreteStrategyC implements Strategy {public function algorithmInterface () {echo 'algorithminterface C
';}}/*** Environment role */class Context {/* referenced policy */private $ _ strategy; public function _ construct (Strategy $ strategy) {$ this-> _ strategy = $ strategy;} public function contextInterface () {$ this-> _ strategy-> algorithmInterface ();}} /*** Client */class Client {/*** Main program. */public static function main () {$ strategyA = new ConcreteStrategyA (); $ context = new Context ($ strategyA); $ context-> conte XtInterface (); $ strategyB = new ConcreteStrategyB (); $ context = new Context ($ strategyB); $ context-> contextInterface (); $ strategyC = new ConcreteStrategyC (); $ context = new Context ($ strategyC); $ context-> contextInterface () ;}} Client: main ();?>
The above is the code that uses php to implement the policy mode. There are also some concepts about the policy mode, which will be helpful for your learning.