Design Mode-factory method mode

Source: Internet
Author: User
I. Concepts

The factory method mode defines a factory interface for creating a product object and delays the actual creation to the subclass. The core factory category is no longer responsible for product creation. In this way, the core class becomes an abstract factory role and is only responsible for the interfaces that must be implemented by specific factory subclass, the advantage of further abstraction is that the factory method mode enables the system to introduce new products without modifying the specific factory role.

The factory method model is derived from the simple factory model and solves many problems of the simple factory model. First, the 'open-close policy' is fully implemented to achieve scalability. Secondly, a more complex hierarchy can be applied to scenarios where product results are complex.

II. Key Points
    1. Abstract Factory role: the Core of the factory method Model and Its ApplicationProgramIrrelevant. Any factory class of the object created 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 applications and is called by applications to create product objects. There are two such roles: bulbcreator and tubecreator.
    3. Abstract Product role: the super type of the object created in the factory method mode, that is, the common parent class of the product object or the common interfaces. In, this role is light.
    4. Specific product role: this role implements the interface defined by the abstract Product role. A specific product is created in a specific factory, which is usually one-to-one.


Iii. Scenarios
    • The first case is that the caller knows exactly which factory service should be used for a product, instantiate the specific factory, and produce a specific product. The iterator () method in the Java Collection belongs to this situation.
    • In the second case, only one product is needed, and you do not need to know which factory is for production, that is, the decision on which specific factory is used is decided by the producer, they instantiate a specific factory based on the current system and return it to the user, which is transparent to the user.


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: 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.