The strategy mode of PHP design mode

Source: Internet
Author: User

Introduces the policy pattern: defines the algorithm family, encapsulates separately, lets them replace each other, this pattern lets the algorithm change independently from uses the algorithm the Customer. Encapsulation: the behavior is encapsulated by an interface, and we can take the frequently changed parts out of the current class separately and use the interface to encapsulate them individually. Replace each other: we encapsulate the interface, and change the algorithm by specifying different interfaces to implement the CLASS. Mind Mapping

Let me explain the process of this mind mapping: 1. Joe did a fairly successful simulation of the duck Game. A super class Duck is designed, and then the various ducks inherit this class. 2. Later the client proposed to let the duck have the ability to fly.   So Joe adds a fly () method to the superclass so that the following subclasses have a flying Behavior. The problem is:1> original duck of the sub-class unexpectedly has rubber duck, rubber duck is not fly.                     --joe the fly () method of the rubber duck is set to empty using the overloaded way. 2> cover Fly (), We see the rubber duck fly (), There is no code, if we add another non-flying ducks in the future, then I would like to deal with it?—— then the code is repeated! 3. The above 2 way we know there is a problem, so Joe thought to make duck interface, so that each subclass must implement the method in Duck.     This ensures that each duck can add behavior according to its own needs. The problem: the product is often in the update, the specifications are constantly changing. The result is that whenever a new duck is available, Joe is forced to check to see if the subclass covers the fly () method. --when you modify a behavior, you have to follow it down and modify it in each class that defines the Behavior. 4. In combination with the above questions, Joe thought of pulling out the parts of the change that never changed. For example, We do a separate interface flybehavior for the fly () Behavior. If Ducks want to fly the function, we let the duck achieve Flybehavior. 5. Further study: we want the Ducks to have different flight functions so that they do different flight movements during Operation. Let the duck class implement the interface, only let the duck have a behavior. So joe, thinking of using the combination of prevention, when the Ducks need other flight function requirements, we can use the Setbehavior () way, the specific way of flight. Code
<?phpinterface FlyBehavior{    publicfunctionfly();}classFlyWithWings implementsFlyBehavior{    publicfunctionfly(){        echo"Fly With Wings \n";    }}classFlyWithNo implementsFlyBehavior{    publicfunctionfly(){        echo"Fly With No Wings \n";    }}
classDuck{    private$_flyBehavior;    publicfunction performFly(){        $this->_flyBehavior->fly();    }    publicfunctionsetFlyBehavior(FlyBehavior $behavior){        $this->_flyBehavior = $behavior;    }}classRubberDuck extendsDuck{}
//Test case $duck  = new   rubberduck ();  /*  want to let ducks fly with wings */ $duck ->setflybehavior ( new   Flywithwings ()); $duck ->performfly ();             /*  want the Ducks to fly without wings */ Code class= "php plain" >->setflybehavior ( new   flywithno ()); $duck ->performfly ();

In summary, our design principles in development are as Follows: 1. Find out where changes in the application might be needed, separate them, and don't mix with code that doesn't need to change; 2. Programming for the interface, not for the implementation of programming; 3. Multi-Use combination, less inheritance; Reference: "head First Design mode"

The strategy mode of PHP design mode

Related Article

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.