Design Pattern (1): Can the factory method be implemented using static?

Source: Internet
Author: User

○. Background and background knowledge

This is an example of QQ yesterday. This article is not about the factory method mode from the beginning, but only about its implementation details. For more information about this mode, see wayfarer and Lu Zhenyu'sArticle.

I. Analysis

Because one purpose of the factory method (in upper case, factory method indicates the mode name) mode is to abstract the creation behavior (Factory method in lower case indicates the creation behavior method), which is implemented by sub-classes, it is easy to expand. In a pure factory method mode, the factory method cannot be implemented using static. Generally, the simple factory mode is implemented by static.

However, it is tempting for a factory to have a static creation method. You can use the factory class name to create the desired product at any time. The DOTNET implementation provided at the end of the wayfarer article is an example. In another article by Lu Zhenyu (about the simple factory mode), the solution provided by mongodan is the same. In essence, both examples are not pure factory method mode or simple factory mode, but a combination of simple factory mode and factory method mode.

Reply by referencing mongodan:

The biggest benefit of simple factory is to centralize changes in one place. In addition, the dependencies in lightsimplefactory. Create () can also be eliminated. My approach is:

1. Declaration Constructor

Public interface ilightcreator
{
Light create ();
}

Public class bulblightcreator: ilightcreator
{
Public Light create ()
{
Return new bulblight ();
}
}
.

2. register the constructor

Creators. Register ("bulb", new bulblightcreator ());
Creators. Register ("tube", new tubelightcreator ());
.

3. Create an object in simple factory

Public class lightsimplefactory.Create(String lighttype)
{
Ilightcreator creator = creators. Find (lighttype );
Return creator. Create ();
}

The constructor is actually the factory method. In this way, the subaccount is constructed through registration. 3. The original switch () process is eliminated, and the dependency is removed. Among them, register () and find () are easy to understand and the method will not be written.

The new type can be extended through the newly registered constructor. Of course, registration in 2CodeThis is just an example. It can be implemented in the configuration file, for example:

<Lightcreators>
<Add name = "bulb" type = "bulblightcreator,"/>
<Add name = "tube" type = "tubelightcreator,"/>
</Lightcreators>

Its ilightcreator inheritance system is the factory method mode. The relationship between lightsimplefactory and creators is not described by mongodan. It can also be implemented in the Strategy Mode.

II. Extended questions

    1. Is the factory method in the schema abstract? No. You can define a default method to produce a product. In this way, this default product can be produced even if the factory does not have a subclass. In this case, factory is an interface and then defines a basicfactory to produce basicproduct. Other Factory types are derived from basicfactory. Note that the factory and product are usually parallel inheritance structures.
    2. The factory method decouples the client and concreteproduct. Does it introduce the coupling between the client and concretefactory? This is a good question. The answer is probably (this problem exists in the example in head first design patterns), but the client should not rely on concretefactory. If you rely on concretefactory, It's really superfluous (see Wayfarer's article ).
    3. What are the benefits of the factory method mode?
      1. Encapsulates object creation. If new is used to create an object, it is tightly coupled with the concreteproduct. The creation behavior of the factory method returns the product interface, decoupling the client and concreteproduct.
      2. Easy to expand. Compared with the simple factory mode, the factory method mode can be extended by derivation.

3. Conclusion

The factory method of a pure factory method mode cannot be implemented in static mode. It can be modified by combining it with other modes. In addition, the factory method mode is usually combined with other modes. Abstract Factory mode is usually implemented in the factory method mode.

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.