The strategy pattern of the design pattern (Python implementation)

Source: Internet
Author: User

Recently read "Simple design mode" This book, a lot of harvest, is going to write a series of blog, and Python to achieve each of the design patterns to deepen understanding.

Here is the first design pattern in the book: strategy mode

In the book to simulate the application of ducks as an example, a game application requires a variety of ducks.

Using inheritance to implement this application, the design is as follows:



This design is mainly based on the Duck class, all the ducks in the back are derived from this class, just at the beginning of the application is not very complicated, it seems that there is no problem, but when the derived class (duck type) more and more, the problem arises. Not all ducks can fly, like a leather duck. Not all ducks can croak, and ducks squeak. Perhaps you would say that I can override the base class method in a derived class to meet the application requirements and realize that different ducks have different names and different ways of flying. But there is one problem that cannot be solved, and the overriding function should not change the behavior of the original function, such as Fly (), a duck that cannot fly should not have fly () this function exists. And if we just rewrite the fly () function of the base class, it doesn't look right. and the quack () function is used to emit "quack", and the base class defines quack () which means that all ducks have quck (), if the duck is now called "Squeak", how to do? Add a "squeaky" function to the base class. That will affect other subclasses as well.

In this way, derivation is not the best solution to the problem, or it cannot be solved by derivation alone.

To analyze, the following design principles are obtained: programming for interfaces, rather than implementing frequently changing parts of a programming separation application


In the end, we separate the easily changing parts, flight behavior and quack behavior, and design the class diagram as follows:

I use Python code to achieve the following:

"" The pattern:strategy pattern.
Keynote:identify the aspects of your application that vary and separate them from what the stays. ' Class Flybehavior: ' Interface class:flybehavior ' Def Fly (self): return class flywithwing (Flyb

Ehavior): Def Fly (self): print ' I am flying with wings! '

Class Flynoway (Flybehavior): Def Fly (self): print ' I cannot fly! ' Class Quackbehavior: ' Interface behavior:quackbehavior ' def quack (self): return class Quack (QUACKBE

Havior): def quack (self): print ' quack! ' Class Squeak (Quackbehavior): def quack (self): print ' Squeak ' class Mutequack (quackbehavior): def quack (SE LF): print ' Mutequack ' class Duck: ' Base class:duck. All ducks are inherent from this class ' Def __init__ (self, Flyparam, quackparam): Self.flybehavior = Flypara
       M self.quackbehavior = Quackparam def performfly (self): Self.flyBehavior.fly () def performquack (self): Self.quackBehavior.quack () def swim (self): print ' All ducks can swim ... ' return def display (self): return class Redduck (Duck): Def __init__ (self,
        Flyparam=flywithwing (), Quackparam=mutequack ()): duck.__init__ (Self, Flyparam, Quackparam) def display (self):
        print ' I am a red duck! ' Return class Rubberduck (Duck): Def __init__ (self, Flyparam=flynoway (), Quackparam=quack ()): duck.__init__ (sel

F, Flyparam, Quackparam) def display (self): print ' I am a rubber duck! ' Duck = Redduck () duck.display () Duck.performfly () Duck.performquack (), duck.swim () Duck = Rubberduck () duck.display ()
 . Performfly () Duck.performquack () Duck.swim ()



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.