Python design pattern-builder mode

Source: Internet
Author: User

Demand, draw characters, ask to draw a person's head, left hand, right hand, left foot, right foot and body, draw a skinny, a fat

Do not use design mode

#Encoding=utf-8__author__='[email protected]'if __name__=='__name__':    Print 'draw your left hand'    Print 'Draw right Hand'    Print 'draw the left foot'    Print 'Draw right foot'    Print 'draw a fat body'    Print 'draw your left hand'    Print 'Draw right Hand'    Print 'draw the left foot'    Print 'Draw right foot'    Print 'Draw Thin Body'

The shortcomings of this writing each draw a person, you have to draw his six parts, these parts of some things can be reused, so the call will be more cumbersome, and the customer may forget to call a part of the painting, so error prone.

Using builder mode

#Encoding=utf-8__author__='[email protected]' fromAbcImportAbcmeta, AbstractmethodclassBuilder ():__metaclass__=Abcmeta @abstractmethoddefDraw_left_arm (self):Pass@abstractmethoddefDraw_right_arm (self):Pass@abstractmethoddefDraw_left_foot (self):Pass@abstractmethoddefDraw_right_foot (self):Pass@abstractmethoddefDraw_head (self):Pass@abstractmethoddefdraw_body (self):PassclassThin (Builder):defDraw_left_arm (self):Print 'draw your left hand'    defDraw_right_arm (self):Print 'Draw right Hand'    defDraw_left_foot (self):Print 'draw the left foot'    defDraw_right_foot (self):Print 'Draw right foot'    defDraw_head (self):Print 'Drawing Head'    defdraw_body (self):Print 'Draw Thin Body'classFat (Builder):defDraw_left_arm (self):Print 'draw your left hand'    defDraw_right_arm (self):Print 'Draw right Hand'    defDraw_left_foot (self):Print 'draw the left foot'    defDraw_right_foot (self):Print 'Draw right foot'    defDraw_head (self):Print 'Drawing Head'    defdraw_body (self):Print 'draw a fat body'classDirector ():def __init__(self, person): Self.person= PersondefDraw (self): Self.person.draw_left_arm () self.person.draw_right_arm () self.person.draw_left_foot () Self.person.draw_right_foot () Self.person.draw_head () self.person.draw_body ( )if __name__=='__main__': Thin=Thin () Fat=Fat () Director_thin=Director (thin) Director_thin.draw () Director_fat=Director (FAT) director_fat.draw ()

Build an abstract class builder, define the method of painting six parts, each painting a kind of person, create a new class to inherit the builder, so the new class must implement the builder's method, here the main use of the abstract method of the characteristics, here to solve the problem of painting a part. Build a leader class director, enter a builder class, define a draw method, put the method calls of the six parts of the painting in the inside, so that the call will not be cumbersome.

So the builder pattern is used to separate the construction of a complex object from its representation, so that the same build process can create different representations.

Python design pattern-builder mode

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.