Big talk involves pattern Python implementation-factory method mode

Source: Internet
Author: User

Tag: Performer implements coding factory encapsulated object Border connection Ges use

Factory method Pattern (Factory mode): Defines an interface for creating objects, so that subclasses decide which class to instantiate, and the factory method delays the instantiation of a class to its subclasses.

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 __author__='Andy'5 6 """7 Liar design Mode8 design mode-factory method mode9 Factory method Pattern (Factory mode): Defines an interface for creating objects, so that subclasses decide which class to instantiate, and the factory method delays the instantiation of a class to its subclasses.Ten The factory method mode overcomes the shortcomings of the simple Factory mode against the open-closed principle, and preserves the advantages of the encapsulation object creation process . One scene: Lei Feng factory, do not care about the performers, only care about the results of implementation A """ -  - classLeifeng (object): the  -     defSweep (self): -         Print "sweeping" -  +     defWash (self): -         Print "Laundry" +  A     defBuyrice (self): at         Print "Buy Rice" -  -  - classifactory (Leifeng): -  -     defCreateleifeng (self): in         Pass -  to #College students + classUndergraduate (Leifeng): -     Pass the  * #New community service providers $ classVolunteer (Leifeng):Panax Notoginseng     Pass -  the #study of Lei Feng's University student factory + classundergraduatefactory (ifactory): A  the     defCreateleifeng (self): +         returnUndergraduate () -  $ #Add a new community service factory e $ classvolunteerfactory (ifactory): -  -     defCreateleifeng (self): the         returnVolunteer () - Wuyi  the if __name__=="__main__": -Student =undergraduatefactory () WuVolunteer =volunteerfactory () - student. Buyrice () About student. Sweep () $Volunteer. Wash ()

The design of the above class is as follows:

Create an abstract factory class Createleifeng and an abstract object class Leifeng, when creating a concrete Leifeng object-undergradiate go to DoSomething, Use the Undergraduatefactory factory class that inherits the abstract factory, which returns a Undergradiate instance, and if you want to do another dosomething, Create a volunteerfactory factory method to create a volunteer instance.


Andy
Source: http://www.cnblogs.com/onepiece-andy/
The copyright of this article is owned by the author and the blog Park, welcome reprint, but without the author's consent must be in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Big talk involves pattern Python implementation-factory method 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.