Home >Backend Development >PHP Tutorial >Introduction to Design Patterns-Strategy Pattern (php version)

Introduction to Design Patterns-Strategy Pattern (php version)

WBOY
WBOYOriginal
2016-08-08 09:27:571085browse

Scenario: A duck simulation game. There are various types of ducks in the game. Ducks can quack and swim.

The preliminary design plan is shown in the UML class diagram below:


Change: This initial stage seemed impeccable until one day the game required ducks to fly. At this time, the simplest solution was to add it to the parent class A fly method, the UML diagram is as follows:


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

The simplest solution at this time is to overwrite the fly method in the RubberDuck class. The class diagram is as follows:


Change: Games are becoming more and more popular. In addition to rubber ducks, there are many kinds of games. Such ducks have been added, such as DecoyDuck (decoy duck), which can neither quack nor fly. Do we also need to cover the fly and quack methods in every class?

Solution:

1. Design fly and quack as interfaces. Only ducks that can quack and fly can inherit. The corresponding UML class diagram is as follows: (The picture below is wrong, the swim method is not needed in decoyduck)


Disadvantages: This solution easily causes code duplication and does not reuse the same fly and quack methods.

2. Make flyable and quackable into an independent class as attributes of the duck. The corresponding UML class diagram is as follows:

In this method, redheadduck, rubberduck, mallardduck and decoyduck have to use the relevant methods in the flyablity and quackablity classes to implement specific fly and quack methods respectively. This solution violates the principle of "programming for interfaces, not programming for specific implementations" in the design philosophy. It cannot guarantee that there must be functions needed by various ducks in the flyablity and quackablity classes

3. Design flyable and quackable as interfaces. The specific implementation is completed by various flying skills and calling skills.

The Uml class diagram is as follows:


Of course the situation is still changing. Just because the rubber duck can’t fly now doesn’t mean it won’t be able to fly in the future. We need a way to set his flying ability, class diagram As follows:

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

Note: During the actual code writing process, it was found that quack is used after implementing quackable method, the quack method is also the constructor of quack on the mobile phone, so the quack class name was changed to the quacking class when writing. Hope you know.

The above has introduced Introduction to Design Patterns - Strategy Pattern (php version), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn