In-depth analysis of the use of builder mode in Python Design Mode Programming, python Design Mode
Builder Mode: Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
Basic Ideas
The construction of a product is composed of many complex components;
Some details of these components are different, and the product appearance is slightly different;
The product is created step by a conductor according to the product creation steps;
To create different products, you only need to derive a specific builder and rewrite the corresponding component build method.
Code structure
Class Builder (object): "base class" def Part1 (self): # for different types of products, the details of this step may vary with raise NotImplementedError () def Part2 (self): # the details of this step may vary with raise NotImplementedError () class Builder1 (Builder): "derived class, product "def Part1 (self): print 'builder1 part1' def Part2 (self): print 'builder1 part2' class Builder2 (Builder ): "derived class, which produces builder2 products" def Part1 (self): print 'builder2 part1' def Part2 (self): print 'builder2 part2' class ctor (object): "conductor, responsible for organizing the product building process" def Build (self, builder): builder. part1 () builder. part2 () def client (): director = Director () director. build (Builder1 () director. build (Builder2 ())
Here is a question about the role of the conductor. In addition to increasing the call burden on the client, it seems useless. Why not place the product building process in the Builder base class, as shown below:
Class Builder (object): "base class" def Part1 (self): raise NotImplementedError () def Part2 (self): raise NotImplementedError () def Build (self ): self. part1 () self. part2 () class Builder1 (Builder): def Part1 (self): print 'builder1 part1' def Part2 (self): print 'builder1 part2' class Builder2 (Builder ): def Part1 (self): print 'builder2 part1' def Part2 (self): print 'builder2 part2' def client (): Builder1 (). build () Builder2 (). build ()
Yes, the above is the implementation routine of the typical template method mode. Let's review the definition of the template method mode:> template method mode: defines the basic skeleton of a workflow or algorithm, the implementation of some specific steps is delayed in the subclass.
The template method mode focuses more on algorithm flows, while the builder mode focuses more on the creation of complex objects. The template mode has more application scenarios than the builder mode, it is more natural to write.
Class Diagram
Instance
# Encoding = UTF-8 ## by panda # builder mode def printInfo (info): print unicode (info, 'utf-8 '). encode ('gbk') # builder base class PersonBuilder (): def BuildHead (self): pass def BuildBody (self): pass def BuildArm (self ): pass def BuildLeg (self): pass # fat class PersonFatBuilder (PersonBuilder): type = 'fat' def BuildHead (self): printInfo ("build % s Header" % self. type) def BuildBody (self): printInfo ("build % s body" % self. type) def BuildArm (self): printInfo ("build % s hand" % self. type) def BuildLeg (self): printInfo ("build % s foot" % self. type) # Lean class PersonThinBuilder (PersonBuilder): type = 'skinny 'def BuildHead (self): printInfo ("build % s Header" % self. type) def BuildBody (self): printInfo ("build % s body" % self. type) def BuildArm (self): printInfo ("build % s hand" % self. type) def BuildLeg (self): printInfo ("build % s foot" % self. type) # conductor class PersonDirector (): pb = None; def _ init _ (self, pb): self. pb = pb def CreatePereson (self): self. pb. buildHead () self. pb. buildBody () self. pb. buildArm () self. pb. buildLeg () def clientUI (): pb = PersonThinBuilder () pd = PersonDirector (pb) pd. createPereson () pb = PersonFatBuilder () pd = PersonDirector (pb) pd. createPereson () return if _ name _ = '_ main _': clientUI ();
Articles you may be interested in:
- Measure the test taker's knowledge about the observer mode and Strategy Mode in Python Design Mode Programming.
- Simple program example of interpreter mode in Python Design Mode Programming
- An example of how to use the design pattern in Python
- Analysis of the key points of the Decorator pattern in Python
- Instance parsing Python Design Mode Programming-Application of Bridge Mode
- Examples of Adapter mode in Python Design Mode Programming
- Application Example of prototype in design mode in Python Program
- An example of how to use the abstract factory mode in Python Design Mode Programming
- The example explains how to use the factory method mode of programming in Python design mode.
- Detailed description of the use of the factory method mode in the design mode in the Python Program
- Use the simple factory mode for Python Design Mode Programming
- Example of using the responsibility chain mode and iterator mode in the design mode in Python