Builder Mode: Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
Related models: Ideas andTemplate Method ModeLike this, the template method encapsulates algorithm flows and provides interfaces for certain details to be modified by sub-classes. The Builder mode is more advanced and all details are handed over to sub-classes for implementation.
Code:
# 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 () returnif _ name _ = '_ main _': clientui ();
Class Diagram: