Design Mode-simple factory Mode

Source: Internet
Author: User
ArticleDirectory
    • Advantages:
    • Disadvantages:
I. Concepts

The simple factory mode is determined by a factory object to create a product instance. In terms of the design pattern type, the simple factory pattern belongs to the creation pattern, also known as the staticfactory method pattern, but not one of 23 gof design patterns.

 

II. Key Points

The essence of the simple factory mode is that a factory class dynamically determines the product class to be created (these product classes inherit from a parent class or interface) based on the input parameters. Roles and responsibilities in this mode:

    1. Factory (Creator) Role:The core of the simple factory mode is to implement the internal logic for creating all instances. The factory class can be directly called by the outside world to create the required product objects.
    2. Abstract product (Product) Role:The parent class of all objects created in simple factory mode, which describes the common public interfaces of all instances.
    3. Specific product (Concrete Product) Role:Is the creation target of the simple factory mode. All created objects are instances of a specific class that acts as this role.

 

Iii. Scenarios
    • The factory class is responsible for creating fewer objects;
    • The customer only knows the parameters passed into the factory class and does not care about how to create objects (logic;
    • Because simple factories are easy to violate the High Cohesion responsibility allocation principle, they are generally used only in simple cases.

 

Iv. Advantages and Disadvantages Advantages:

The factory class is the key to the entire model. It contains the necessary logic judgment and determines the specific class object to be created based on the information given by the outside world. By using the factory class,The external world can get rid of the embarrassing situation of directly creating specific product objects. It only needs to be responsible for "consuming" objects.. Without having to worry about how these objects are created and organized, they clearly define their respective responsibilities and rights, which is conducive to the optimization of the entire software architecture.

Disadvantages:
    • Because the factory class integrates the creation logic of all instances, it violates the High Cohesion <responsibility allocation principle, and the entire creation logic is centralized into a factory class; the class it can create can only be considered in advance, if you need to add a new class, you need to change the factory class.
    • When the number of product categories in the system increases, the requirements for the factory category to create different instances based on different conditions may arise. this kind of judgment on the condition is intertwined with the judgment on the specific product type, and it is difficult to avoid the spread of module functions, which is very unfavorable for system maintenance and expansion;
    • These shortcomings exist inFactory method modeIt has been overcome.

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.