Php design mode-concept of policy mode and policy mode
Policy patterns define a series of algorithms, encapsulate each algorithm, and make them replaceable. The rule mode allows algorithms to change independently of customers who use it.
Primary role
Abstract policy role: a policy class, usually implemented by an interface or abstract class.
Specific policy roles: encapsulate related algorithms and behaviors.
Environment role: Hold a reference to the policy class and call it to the client.
Application scenarios
1. multiple classes only differ in performance behavior. you can use the Strategy mode to dynamically select the behavior to be executed during running.
2. different policies (algorithms) need to be used in different situations, or they may be implemented in other ways in the future.
3. Hiding implementation details of specific policies (algorithms) from customers is completely independent of each other.
Advantages
1. the policy mode provides methods for managing related algorithm families. The hierarchical structure of a policy class defines an algorithm or behavior family. Proper use of inheritance can transfer public code to the parent class to avoid repeated code.
2. the policy mode provides a way to replace the inheritance relationship. Inheritance can process multiple algorithms or actions. If the policy mode is not used, the environment class that uses algorithms or behaviors may have some subclasses. each subclass provides a different algorithm or behavior. However, in this way, the user of an algorithm or behavior is mixed with the algorithm or behavior itself. The logic that determines which algorithm to use or which behavior to take is mixed with the logic of the algorithm or behavior, so that it is impossible to evolve independently. Inheritance makes it impossible to dynamically change algorithms or behaviors.
3. use policy mode to avoid using multiple conditional transfer statements. Multiple transfer statements are not easy to maintain. they mix the logic of which algorithm or behavior is adopted with the logic of the algorithm or behavior and are all listed in a multiple transfer statement, it is more primitive and backward than the inheritance method.
Disadvantages
1. the client must know all the policy classes and decide which one to use. This means that the client must understand the differences between these algorithms so that appropriate algorithm classes can be selected in a timely manner. In other words, the policy mode is only applicable when the client knows all the algorithms or actions.
2. the policy mode creates many policy classes. each specific policy class produces a new class. Sometimes you can save the environment-dependent status to the client and design the policy class to be shared so that the policy class instance can be used by different clients. In other words, you can use the metadata mode to reduce the number of objects.
Rule mode instance
\ R \ n ";}}/ *** specific strategy class (ConcreteStrategy) * 2: Take the Train */class TrainStrategy implements TravelStrategy {public function travelAlgorithm () {echo" travelbyTrain ","
\ R \ n ";}}/ *** specific strategy class (ConcreteStrategy) * 3: Bicycle Ride */class BicycleStrategy implements TravelStrategy {public function travelAlgorithm () {echo" travelbyBicycle ","
\ R \ n ";}}/*** Context: * use a ConcreteStrategy object for configuration. * Maintain a reference to the Strategy object. You can define an interface to allow Strategy to access its data. * Algorithm solution class: */class PersonContext {private $ _ strategy = null; public function _ construct (TravelStrategy $ travel) {$ this-> _ strategy = $ travel;}/*** travel */public function setTravelStrategy (TravelStrategy $ travel) {$ this-> _ strategy = $ travel ;} /*** travel */public function travel () {return $ this-> _ strategy-> travelAlgorithm ();}} // traveling by train $ person = new PersonContext (new TrainStrategy (); $ person-> trav El (); // change the bike ride $ person-> setTravelStrategy (new BicycleStrategy (); $ person-> travel ();?>