Ps: Similar to the original.
There is a game, the content is the life of ducks, you can play a duck, swimming, quack can also be duck-type takeoff. In the game interior design, has a ancestor class (superclass), has included the duck's basic life behavior, the different duck kind only needs to inherit again to rewrite the abstract method, may derive the duck, the week black Duck and so on own characteristic ducks, the UML modelling is as follows:
游戏很受欢迎(强行受欢迎),于是要进行下一版本的开发,要添加让鸭子飞行的功能!!!这有啥难的,在祖类(Superclass)中添加一个fly方法,所有通过继承派生出来的鸭子不就能飞了么,于是bulabula,新的模型出来了,如下所示:
At this time, there will be a small partner to find a big problem, Donald Duck fly up also forget, but you black duck fly up to calculate what, lost duck fly ... As a melted down, this second version of the game needs to be re-created. At this time, in the redesign, we consider the program's Extensibility issues (Ps: correctness, robustness, reliability, efficiency, ease of use, readability (understandable), extensibility, reusability, compatibility).
To solve the problem of black duck flying up, the quickest solution is to make the Black Duck's flying bug disappear by covering the parent class in the week Black Duck Class! However, the game is very popular (forced very), follow-up will definitely add more duck species, if the use of coverage, then every new category of ducks to join the game, then the fly method to determine whether the need to overwrite. Some small partners may have said that ducks can have how many kinds, so judge the workload is not too large, and then too young too simple, duck birds in the taxonomy of species generally divided into 10 subfamily: There are 44 genera 156 species.
What's more, there are all kinds of ducks that exist in the two-dimensional? This is just fly cover, different ducks are different, Donald Duck can speak, Zhou Black duck ... Zhouhe thought: "I want to be quiet, don't ask me who is quiet." When a large number of objects appear, overwriting will cause a lot of duplication of effort, so at this point, let's consider what if we use the interface? Flying fly and Ducks called quack out of the interface, each duck as long as the implementation of these two interfaces can be set to their own categories of flight mode and tweet features, however, the workload does not seem to reduce the number of Ah, even the amount of code has become more, if the code can be reused.
So, what if you create classes that specifically implement the Fly interface and the quack interface, the composition behavior class? The simple model is as follows:
So, among the many ducks, ducks flying with wings only need to use flywithwings to achieve their own flight, and do not have to repeat the flight code in each duck class, but how to use the Flywithwings class? Donald Duck, Zhouhe and other ducks have inherited the duck, according to grammatical rules, can not inherit flywithwings. So, what do we define two instance variables in duck? As shown below:
So that's the problem? How do we make the act effective? We add two more behavior methods Performfly and Performquack in the Duck class that are similar to the quack and fly methods in the second model. The model is modified as follows:
The rough code is as follows:
Right now! Let's create a black duck class out of a week, the code is as follows:
The code for Mutequack is as follows:
So now you just need to test to see the final result:
end note:
In fact, most of the content belongs to the transport, the original very few, but also deleted the original book part of the content, will slowly more practical understanding to explain a pattern, the first test of water, code is not all the small partners to write their own, do not Baidu, code this thing, although the wheel has been created, but their own side, Will understand better.
Finally, Head first design mode This book is good, Force Amway.
Design Patterns-Learning Essays (i)