PHP design pattern policy pattern

Source: Internet
Author: User
Policy mode defines algorithm families and encapsulates them separately so that they can be replaced with each other. This mode allows algorithms to change independently of customers who use it.

Introduction

Policy mode defines algorithm families and encapsulates them separately so that they can be replaced with each other. This mode allows algorithms to change independently of customers who use it.

Composition

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

Multiple classes only differ in different performance behaviors. you can use the policy mode to dynamically select the behavior to be executed during running.

Different policies (algorithms) must be used in different situations, or they may be implemented in other ways in the future.

Hiding implementation details of specific policies (algorithms) from customers is completely independent of each other.

Implementation

Procedure

Define the abstract role class (define the common abstract methods of each implementation)

Define specific policy classes (common methods for implementing parent classes)

Define the environment (receive and save instances, and uniformly execute the policy interface method)

Code

 ';}}/*** Class byBus specific policy role * take the bus */class byBus implements Travel {public function go () {echo' I took the bus out to play
';}}/*** Class byMetro specific policy role * take the subway */class byMetro implements Travel {public function go () {echo' I took the subway out to play
';}}/*** Class byTrain specific policy role * by train */class byTrain implements Travel {public function go () {echo' I'm going out by train
';}}/*** Class byAirplane specific policy role * fly */class byAirplane implements Travel {public function go () {echo' I am going out to play by plane
';}}/*** Class bySteamship specific policy role * ship */class bySteamship implements Travel {public function go () {echo' I'm going out on a ship
';}}/*** Class Mine environment role */class Mine {private $ _ strategy; private $ _ isChange = false; /*** constructor ** the concept of dependency injection and type constraints is used here. For details, see * 1. chat about PHP dependency injection (DI) and control inversion (IoC) * @ link https://segmentfault.com/a/1190000007209266 * 2. PHP type constraints * @ link https://segmentfault.com/a/1190000007226476 ** @ Param Travel $ travel */public function _ construct (Travel $ travel) {$ this-> _ strategy = $ travel ;} /*** change the Travel mode ** @ param travel $ Travel */public function change (travel $ travel) {$ this-> _ strategy = $ Travel; $ this-> _ isChange = true;} public function goTravel () {if ($ this-> _ isChange) {echo 'change your mind now ,'; $ this-> _ strategy-> go ();} else {$ this-> _ strategy-> go ();}}} /*** use the client */$ strategy = new Mine (new byBus (); // take the bus $ strategy-> goTravel (); // take the subway $ strategy-> change (new byMetro (); $ strategy-> goTravel (); // self-driving $ strategy-> change (new bySelfDriving ()); $ strategy-> goTravel (); // select other implementations based on the specific application.

Running result

I took the bus out to play
Now I changed my mind. I went out to play by subway.
Now I have changed my mind. I drove out to play.

Advantages and disadvantages

Advantages

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.

The rule 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.

The policy mode avoids the use of 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

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.

The rule 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.