Factory methods (Factory method) mode

Source: Internet
Author: User
-->
Add to Favorites

Contact Us

Web Search
--> Home Alternative life. NET technology database Technology JavaScript Technology Web page design related Technical topics project System Blog website Forum
Ease to fly → project system → software methodology → design pattern

Factory Methods (Factory method) mode
Author: Lu Zhenyu
First, factory methods (Factory method) mode

The Factory method (FactoryMethod) pattern is the creation pattern of a class, which is intended to define a factory interface that creates product objects, deferring actual creation to subclasses.

The factory method model is a further abstraction and generalization of the simple factory model. Because of the use of polymorphism, the factory method pattern maintains the advantages of a simple factory model and overcomes its drawbacks.

In the factory method model, the core factory class is no longer responsible for the creation of all the products, but rather the specific creation work is given to subclasses. This core class is only responsible for giving the interfaces that a specific factory must implement without touching the details of which product class is instantiated. This allows the factory method model to allow the system to introduce new products without modifying the factory role.

In factory method mode, factory classes and product classes tend to have parallel hierarchies, with one by one corresponding to each other.


Second, Factory method mode role and structure:


Abstract Factory (Creator) Role: Is the core of the factory method pattern and is independent of the application. The factory class of any object created in the schema must implement this interface.

Specific factory (concrete Creator) role: This is a specific factory class that implements the abstract factory interface, contains logic that is closely related to the application, and is called by the application to create product objects. There are two of these roles in the above figure: Bulbcreator and Tubecreator.

Abstract product Role: The supertype of objects created by the factory method pattern, which is the common parent class of the Product object or the shared interface. In the above picture, this role is light.

Specific product (concrete product) Role: This role implements the interface defined by the abstract product role. A specific product has a specific factory to create, they often one by one correspondence.


Iii. Examples of procedures: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 ();

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.