Design Mode C # description-factory method mode

Source: Internet
Author: User

Design Mode C #Description-factory method mode

The factory method mode is the Creation Mode of the class, also known as the virtual constructor mode or the polymorphism factory mode. It is intended to define a factory interface for creating product objects and delay the actual creation work to the subclass.

Disadvantages of the simple factory model:

Because the factory class focuses on the Creation logic of all products, if it cannot work normally, it will have a great impact on the system. To add a new product, you must modify the source code of the factory role.

Advantages of the factory method mode:

In the factory method mode, the core factory class is no longer responsible for the creation of all products, but rather the creation work is handed over to the Child class. This core class becomes the role of an abstract factory. It is only responsible for providing the interface that must be implemented by a specific factory subclass, without touching the details of product class instantiation. This allows the factory method model to allow the system to introduce new products without modifying the specific factory role, so that it has the advantage of surpassing the simple factory model.

 

The following is an example to discuss the implementation of this mode:


Abstract Factory role(Creator): serves as the core of the factory method mode and specifies the interface that should be implemented by the factory class for object creation.

Specific factory rolesConceretecreator: creates specific product objects and implements interfaces specified by creator.

Abstract Product role(Product): the super type of the object created in the factory method mode, specifying the interface that the product should have.

Specific product roles(Concreteproduct): implements the interface specified by product.

ExampleCodeAs follows:

Creator:

Public interface creator

{

Product Factory (); // factory Method

}

 

Concretecreator1:

Class concretecreator1: Creator

{

Public Product Factory () // factory Method

{

Return new concreteproduct1 ();

}

}

 

Concretecreator2:

Class concretecreator2: Creator

{

Public Product Factory () // factory Method

{

Return new concreteproduct2 ();

}

}

 

Product:

Public interface Product

{

}

 

Concreteproduct1:

Class concreteproduct1: Product

{

Public concreteproduct1 ()

{

Console. writeline ("creat concreteproduct1 ");

}

}

 

Concreteproduct2:

Class concreteproduct2: Product

{

Public concreteproduct2 ()

{

Console. writeline ("creat concreteproduct2 ");

}

}

 

Client:

Class Client

{

Private Static creator creator1, creator2;

Private Static product product1, product2;

[Stathread]

Static void Main (String [] ARGs)

{

Creator1 = new concretecreator1 ();

Product1 = creator1.factory ();

Creator2 = new concretecreator2 ();

Product2 = creator2.factory ();

}

}

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.