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