Factory method design mode

Source: Internet
Author: User

 

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;

Public abstract class light
{
Public abstract void turnon ();
Public abstract void turnoff ();
}

Public class bulblight: Light
{
Public override void turnon ()
{Console. writeline ("Bulb light is turned on ");}

Public override void turnoff ()
{Console. writeline ("Bulb light is turned off ");}
}

Public class tubelight: Light
{
Public override void turnon ()
{Console. writeline ("tube light is turned on ");}

Public override void turnoff ()
{Console. writeline ("tube light is turned off ");}
}

Public abstract class creator
{
Public abstract light Factory ();
}

Public class bulbcreator: Creator
{
Public override light Factory ()
{Return New bulblight ();}
}

Public class tubecreator: Creator
{
Public override light Factory ()
{Return New tubelight ();}
}

Public class client
{
Public static void main ()
{
Creator C1 = new bulbcreator ();
Creator C2 = new tubecreator ();

Light L1 = c1.factory ();
Light L2 = c2.factory ();

L1.turnon ();
L1.turnoff ();

Console. writeline ("-----------------");

L2.turnon ();
L2.turnoff ();
}
}

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

Using system;
Using system. collections;

// "Product"
Abstract class page
{
}

// "Concreteproduct"
Class skillspage: Page
{
}

// "Concreteproduct"
Class educationpage: Page
{
}

// "Concreteproduct"
Class experiencepage: Page
{
}

// "Concreteproduct"
Class introductionpage: Page
{
}

// "Concreteproduct"
Class resultspage: Page
{
}

// "Concreteproduct"
Class conclusionpage: Page
{
}

// "Concreteproduct"
Class summarypage: Page
{
}

// "Concreteproduct"
Class bibliographypage: Page
{
}

// "Creator"
Abstract class document
{
// Fields
Protected arraylist pages = new arraylist ();

// Constructor
Public document ()
{
This. createpages ();
}

// Properties
Public arraylist pages
{
Get {return pages ;}
}

// Factory Method
Abstract Public void createpages ();
}

// "Concretecreator"
Class resume: Document
{
// Factory method implementation
Override public void createpages ()
{
Pages. Add (New skillspage ());
Pages. Add (New educationpage ());
Pages. Add (New experiencepage ());
}
}

// "Concretecreator"
Class Report: Document
{
// Factory method implementation
Override public void createpages ()
{
Pages. Add (New introductionpage ());
Pages. Add (New resultspage ());
Pages. Add (New conclusionpage ());
Pages. Add (New summarypage ());
Pages. Add (New bibliographypage ());
}
}

/** // <Summary>
/// Factorymethodapp Test
/// </Summary>
Class factorymethodapp
{
Public static void main (string [] ARGs)
{
Document [] docs = new document [2];

// Note: constructors call factory Method
Docs [0] = New resume ();
Docs [1] = new report ();

// Display document pages
Foreach (document in docs)
{
Console. writeline ("" + document + "-------");
Foreach (page in document. Pages)
Console. writeline ("" + page );
}
}
}

 

From: http://hi.baidu.com/allyang007/blog/item/6db63ea0907a408246106448.html

 

 

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.