In-depth analysis of the use of builders ' patterns in Python design pattern programming

Source: Internet
Author: User
Builder Mode: Separates the construction of a complex object from his presentation, allowing the same build process to create different representations.

Basic ideas
The construction of a certain kind of product consists of many complex components;
Some of the details in these components are different, and the appearance of the product is slightly different;
The creation of the product is carried out by one conductor according to the steps of the product creation step by step;
When you need to create a different product, you only need to derive a concrete builder, overriding the appropriate component building method.

Code structure

Class Builder (object): "" "  base Class" ""  def Part1 (self):    # Different types of products, the details of this step may be different    raise Notimplementederror ()  def Part2 (self):    # Different types of products, the details of this step may be different    raise Notimplementederror () class Builder1 (Builder): "" "  derived class, Production builder1 type of Product "" "  def Part1 (self):    print ' Builder1 Part1 '  def Part2 (self):    print ' Builder1 Part2 ' class Builder2 (Builder): "" "  derived class, production builder2 type of Product" ""  def Part1 (self):    print ' Builder2 Part1 '  def Part2 (self):    print ' Builder2 Part2 ' class Director (object): "" "  conductor, responsible for organizing product build Process" ""  def Build (Self, builder):    Builder. Part1 ()    Builder. Part2 () def client (): Director  = Director () Director  . Build (Builder1 ())  director. Build (Builder2 ())

Here's a question of what the role of the conductor is. The feeling does not seem to be useful except to increase the client's calling burden. Why not put the product build process in the builder base class, like this:

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, this is the typical pattern of template method implementation routines, review the definition of template method Pattern: > Template Method Pattern: Defines a basic skeleton of a workflow or algorithm, and delays implementation of some specific steps into subclasses.

The template method pattern focuses more on algorithmic processes, while the builder pattern focuses more on the creation of complex objects, with template pattern scenarios that are more specific than the builder pattern and more natural to write.

Class diagram

Instance

#encoding =utf-8 # #by Panda #建造者模式 def printinfo (info): Print Unicode (info, ' utf-8 '). Encode (' GBK ') #建造者基类 class Perso NBuilder (): Def buildhead (self): Pass def buildbody (self): Pass def buildarm (self): Pass def Bu Ildleg (self): Pass #胖子 class Personfatbuilder (personbuilder): type = ' fat ' def buildhead (self): printinfo ("Structure 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's feet"% self.type) #瘦子 class Personthinbuilder (Personbui Lder): type = ' thin ' def buildhead (self): Printinfo ("Build%s's head"% self.type) def buildbody (self): Printinfo ("Fabric Build%s Body "% self.type" def buildarm (self): Printinfo ("Build%s's hand"% Self.type) def buildleg (self): Printinfo ("Build   %s's foot "% self.type" #指挥者 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 = Personthinbuilde R () PD = Persondirector (PB) PD. Createpereson () PB = Personfatbuilder () PD = Persondirector (PB) PD.  Createpereson () return if __name__ = = ' __main__ ': Clientui ();
  • 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.