Design Pattern 2: Factory method pattern (Factory method pattern)

Source: Internet
Author: User

Original article: http://www.cnblogs.com/zhenyulu/articles/36590.html

References: http://www.dofactory.com/Patterns/PatternFactory.aspx

I. Factory method mode

The factorymethod mode is the class creation mode. It defines a factory interface for creating product objects and delays the actual creation to the subclass.

The factory method mode is further abstract and promoted by the simple factory mode. Due to the use of polymorphism, the factory method pattern maintains the advantages of the simple factory pattern and overcomes its shortcomings.

In the factory method mode, the core factory class is no longer responsible for the creation of all products, but rather the specific creation work is handed over to the Child class. This core class is only responsible for providing the interface that must be implemented by a specific factory, without touching the details of which product class is instantiated. This allows the factory method mode to allow the system to introduce new products without modifying the factory role.

In the factory method mode, factory classes and product classes usually have parallel hierarchical structures, which correspond one to one.

Ii. Factory method role and structure:

 

Abstract Factory role: it is the core of the factory method model and has nothing to do with applications. Any factory class of the object created in the mode must implement this interface.

Concrete Creator: this is a specific factory class that implements Abstract Factory interfaces. It contains the logic closely related to applications and is called by applications to create product objects. There are two such roles: bulbcreator and tubecreator.

Abstract Product role: the super type of the object created in the factory method mode, that is, the common parent class of the product object or the common interfaces. In, this role is light.

Specific product role: this role implements the interface defined by the abstract Product role. A specific product is created in a specific factory, which is usually one-to-one.

3. program example:

Using system; </P> <p> public abstract class light <br/> {<br/> public abstract void turnon (); <br/> public abstract void turnoff (); <br/>}</P> <p> public class bulblight: light <br/> {<br/> Public override void turnon () <br/> {console. writeline ("Bulb light is turned on") ;}</P> <p> Public override void turnoff () <br/> {console. writeline ("Bulb light is turned off") ;}< br/>}</P> <p> public class tubelight: light <br/> {<br/> Public override void turnon () <br/> {console. writeline ("tube light is turned on") ;}</P> <p> Public override void turnoff () <br/> {console. writeline ("tube light is turned off ");} <br/>}</P> <p> public abstract class creator <br/>{< br/> public abstract light Factory (); <br/>}</P> <p> public class bulbcreator: Creator <br/> {<br/> Public override light Factory () <br/> {return New bulblight () ;}< br/>}</P> <p> public class tubecreator: creator <br/>{< br/> Public override light Factory () <br/> {return New tubelight ();} <br/>}</P> <p> public class client <br/>{< br/> Public static void main () <br/>{< br/> creator C1 = new bulbcreator (); <br/> creator C2 = new tubecreator (); </P> <p> light L1 = c1.factory (); <br/> light L2 = c2.factory (); </P> <p> l1.turnon (); <br/> l1.turnoff (); </P> <p> console. writeline ("-----------------"); </P> <p> l2.turnon (); <br/> l2.turnoff (); <br/>}< br/>}

Activity sequence diagram of factory methods

 

Activities include:

The client creates a bulbcreator object. The client holds this object type as creator, while the actual type is bulbcreator. Then the client calls the factory method of bulbcreator, and then bulbcreator calls the bulblight constructor to create the product bulblight object.

Iv. Factory method mode and simple factory Mode

The difference between the factory method mode and the simple factory mode is not obvious. The core of the factory method class is an abstract factory class, while the simple factory model places the core on a specific class.

The factory method mode is called the polymorphism factory mode because each factory class has a common interface or abstract parent class.

To add a new product object for system expansion, you only need to add a specific object and a specific factory object. The original factory object does not need to be modified or the client needs to be modified, it is in line with the "open-closed" principle. The simple factory mode has to modify the factory method after adding new product objects, and the scalability is poor.

After the factory method model degrades, it can be transformed into a simple factory model.

V. Evolution of factory method mode

Use an interface or abstract class
Abstract Factory roles and abstract field frequency roles can be implemented by interfaces or abstract classes.

Use multiple factory methods
Abstract Factory roles can define more than one factory method so that specific factory roles can implement these different factory methods that provide different business logic, to meet the task of providing different product objects.

Product Recycling
The factory method always calls the constructor of the product class to create a new product instance, and then provides the instance to the client. In actual situations, the factory method can be quite complicated.

A common complex logic is to use product objects cyclically. The factory object registers the created products to a cluster and queries the cluster based on the product status requested by the customer. If a product object meets the requirements, the product is directly returned to the client. If such a product object does not exist in the aggregation, a new product object meeting the requirements is created, register the object to the cluster and return it to the client. "Flyweight pattern" is such a pattern.

 

Loss of polymorphism and degradation of patterns
The implementation of a factory method mode depends on the polymorphism of factory roles and product roles. In some cases, this mode may degrade.

The type returned by the factory method should be an abstract type, rather than a specific type. Clients that call factory methods should rely on abstract product programming, rather than specific products. If the factory only returns a specific product object, it violates the intention of the factory method and degrades, then it is no longer the factory model.

Hierarchical Structure of the factory: The factory object should have an abstract super type. If there is only one specific factory class in the hierarchy, the abstract factory can be omitted, resulting in degradation.

Vi. Relationship between the factory method mode and other modes

Models Related to the factory method model also include:
Template Method mode, MVC mode, metadata mode, and memorandum Mode

7. Another example

 // Factory method pattern -- real world example </P> <p> using system; <br/> using system. collections; </P> <p> // "product" <br/> abstract class page <br/>{< br/>}</P> <p> // "concreteproduct "<br/> class skillspage: page <br/>{< br/>}</P> <p> // "concreteproduct" <br/> class educationpage: page <br/>{< br/>}</P> <p> // "concreteproduct" <br/> class experiencepage: page <br/>{< br/>}</P> <p> // "concreteproduct" <br/> class introductionpage: page <br/>{< br/>}</P> <p> // "concreteproduct" <br/> class resultspage: page <br/>{< br/>}</P> <p> // "concreteproduct" <br/> class conclusionpage: page <br/>{< br/>}</P> <p> // "concreteproduct" <br/> class summarypage: page <br/>{< br/>}</P> <p> // "concreteproduct" <br/> class bibliographypage: page <br/>{< br/>}</P> <p> // "creator" <br/> abstract class document <br/>{< br/>/ /fields <br/> protected arraylist pages = new arraylist (); </P> <p> // constructor <br/> public document () <br/> {<br/> This. createpages (); <br/>}</P> <p> // properties <br/> Public arraylist pages <br/>{< br/> get {return pages ;} <br/>}</P> <p> // factory method <br/> Abstract Public void createpages (); <br/>}</P> <p> // "concretecreator" <br/> class resume: document <br/>{< br/> // factory method implementation <br/> override public void createpages () <br/>{< br/> pages. add (New skillspage (); <br/> pages. add (New educationpage (); <br/> pages. add (New experiencepage (); <br/>}</P> <p> // "concretecreator" <br/> class report: document <br/>{< br/> // factory method implementation <br/> override public void createpages () <br/>{< br/> pages. add (New introductionpage (); <br/> pages. add (New resultspage (); <br/> pages. add (New conclusionpage (); <br/> pages. add (New summarypage (); <br/> pages. add (New bibliographypage ()); <br/>}</P> <p>/** // <summary> <br/> // factorymethodapp test <br/> /// </Summary> <br/> class factorymethodapp <br/> {<br/> Public static void main (string [] ARGs) <br/>{< br/> document [] docs = new document [2]; </P> <p> // note: constructors call factory method <br/> docs [0] = New resume (); <br/> docs [1] = new report (); </P> <p> // display document pages <br/> foreach (document in docs) <br/> {<br/> console. writeline ("" + document + "-------"); <br/> foreach (page in document. pages) <br/> console. writeline ("" + page); <br/>}< br/>}

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.