Builder mode (Python implementation)

Source: Internet
Author: User

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:

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.