Design Patterns 3 don't hesitate-factory method mode factorymethod

Source: Internet
Author: User

Factory methodMode factorymethod

The implementation of the factory method mode delays the creation of specific products into sub-classes, thus solving the problem that the simple factory mode is difficult to expand.

The simple factory class is decomposed into abstract base classes and several specific classes are as follows:

// Abstract Factory method "headquarters" class factorymethod public abstract class factorymethod {// return the Abstract Factory method creator public Abstract Food creator () of the target food object ();} // potatoesfactory public class potatoesfactory: factorymethod, a subclass of "headquarters" {// specific factory method for implementing functions creator public override food creator () {// return the new specific object return New Potatoes () ;}// another specific subclass of "headquarters" tomatoesfactory public class tomatoesfactory: factorymethod {public override food creator () {return New tomatoes ();}}

If you want to add new features or products, you can usePolymorphismTo easily complete the expansion.

In the factory method model, the factory class and the specific product class have a parallel hierarchical structure, and they correspond one to one.

That is to say, when adding a function, it needs to be added in pairs.

Note: In the factory method mode, each specific factory class only creates a single instance. To achieve plant diversification (one factory produces multiple instances), the abstract factory mode is required.

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.