Design Pattern 6: Factory method Pattern)

Source: Internet
Author: User

The Factory Method mode defines a Factory interface for creating objects and delays the actual creation of objects to subclass.

The core factory category is no longer responsible for the creation of specific products. It only provides interfaces that must be implemented by specific factory subclass, so that the core class becomes an abstract factory role, the advantage of this is that the factory method model enables the system to introduce new products without modifying the specific factory role.

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

 

We can see that the factory method model has four roles:

  1. Abstract Factory role (ICreator) is the core of the factory method model and has nothing to do with applications. Any factory class that creates an object in the mode must implement this interface.
  2. Concrete Creator: this is a specific factory class that implements Abstract Factory interfaces. It contains the logic closely related to the application and is called by the application to create product objects, there are two roles: BulbCreator and TubeCreator.
  3. Abstract Product role: interface of the object created in the factory method mode, that is, the common interface of the Product object. In the middle is ILight.
  4. Concrete Product role: this role implements Multi-definition interfaces of abstract Product roles. Specific products are created in a specific factory, which usually correspond to each other one by one.

The specific implementation code is as follows:

Public interface ICreator

{

ILight Factory ();

}

Public interface ILight

{

Void TurnOn ();

Void TurnOff ();

}

Public class TubeLight: ILight

{

Public void TurnOn ()

{

Console. WriteLine ("TubeLight TurnOn ");

}

Public void TurnOff ()

{

Console. WriteLine ("TubeLight TurnOff ");

}

}

Public class BulbLight: ILight

{

Public void TurnOn ()

{

Console. WriteLine ("BulbLight TurnOn ");

}

Public void TurnOff ()

{

Console. WriteLine ("BulbLight TurnOff ");

}

}

Public class BulbCreator: ICreator

{

Public ILight Factory ()

{

Return new BulbLight ();

}

}

Public class TubeCreator: ICreator

{

Public ILight Factory ()

{

Return new TubeLight ();

}

}

Public class Test

{

Public static void Main ()

{

ICreator create1 = new BulbCreator ();

ILight light = create1.Factory ();

Light. TurnOn ();

Light. TurnOff ();

 

Create1 = new TubeCreator ();

Light = create1.Factory ();

Light. TurnOn ();

Light. TurnOff ();

}

}

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

The specific factory classes of the factory method mode have a common interface, so the factory method mode is also called the polymorphism factory mode.

When a new product object needs to be added 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, it is in line with the "open and 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.

 

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.