Factory method mode

Source: Internet
Author: User
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

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.