Introduction to design mode-policy mode (PHP version)

Source: Internet
Author: User
Scene: A simulated duck game, the game has all kinds of ducks, ducks will call, can swim.

The preliminary design diagram is shown in the UML class diagram as follows:


Change: This early look is impeccable, until one day the game needs ducks to fly, and the simplest solution is to add a fly method to the parent class, as shown in the UML diagram:


One day the disaster happened: there are many rubber ducks flying around in the game ....

The simplest solution at this point is to overwrite the Fly method in the Rubberduck class with the following class diagram:


Change: The game is more and more popular, in addition to rubber ducks, there are many kinds of ducks joined in, such as decoyduck (bait duck), will not be called and can not fly, we also want each class to cover the fly, quack method?

Solution:

First, the fly and quack design interface, only will be called flying ducks to inherit, the corresponding UML class diagram is as follows: (wrong, decoyduck do not need swim method)


Cons: This scheme is prone to duplication of code, without reusing the same fly and quack methods

Second, the flyable and quackable into a separate class, as the properties of ducks, the corresponding UML class diagram is as follows:

In this method, Redheadduck, Rubberduck, Mallardduck, and Decoyduck are used to implement specific fly and flyablity methods, respectively, using related methods in Quackablity and quack classes. This kind of scheme violates the principle of "programming for the interface and not the specific implementation" in the design thought, and there is no guarantee that the function of all kinds of ducks must exist in Flyablity and Quackablity class.

Third, the flyable and quackable designed to interface, the specific implementation by all kinds of flight skills and call skills to complete.

UML class diagrams are as follows:


Of course, the situation is still changing, rubber ducks now can not fly up does not mean that he will not fly up, we need a way to set his flying ability, the class diagram is as follows:

Code Download: Download code (http://www.walk-sing.com/strategy policy mode. zip)

Note: In the actual code writing process found quack in the implementation of quackable after the use of quack method, the Quack method is also quack construction method, so at the time of writing quack class name changed to Quacking class, hope to know.

The above describes the design pattern entry-Strategy mode (PHP version), including the aspects of the content, you want to be interested in PHP tutorial friends helpful.

  • 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.