Description
The factory method mode defines an interface for creating objects, but the subclass determines the class to be instantiated. The factory method mode delays Instantiation to subclass.
The difference with a simple factory is that each factory only produces its own products, while a simple factory produces various products.
Schema Structure
Program example
Note:
One log class, two Derived classes (File log and Event Log), one log factory class (returned log class), and two Derived classes (respectively producing file log classes and Event Log classes)
Code:
1 class log(object): 2 def write(self): 3 pass 4 5 class eventLog(log): 6 def write(self): 7 print ‘eventLog‘ 8 9 class fileLog(log):10 def write(self):11 print ‘fileLog‘12 13 class logFactory(object):14 def create(self):15 return log()16 17 class eventLogFactory(logFactory):18 def create(self):19 return eventLog()20 21 class fileLogFactory(logFactory):22 def create(self):23 return fileLog()24 25 if __name__==‘__main__‘:26 factory = fileLogFactory()27 log = factory.create()28 log.write()29 30 factory = eventLogFactory()31 log = factory.create()32 log.write()
Running result:
Reference Source:
Http://www.cnblogs.com/chenssy/p/3679190.html
Http://www.cnblogs.com/wuyuegb2312/archive/2013/04/09/3008320.html
Http://www.cnblogs.com/Terrylee/archive/2006/07/17/334911.html
Factory method mode