Comparison of simple factory model, abstract factory model and factory method model in big talk Design Model

Source: Internet
Author: User

Simple factory mode, abstract factory mode, and factory method mode all belong to the Creation Mode in the design mode. They are somewhat similar in form and characteristics, the ultimate goal is to help us extract the instantiation part of the object, optimize the system architecture, and enhance the scalability of the system, that is, to better reflect the open and closed principle.

Simple factory mode:


The simple factory mode is the class creation mode, also called the static factory method mode. It is determined by a factory class based on the input parameters to create a product class instance, it involves factory roles, abstract product roles, and specific product roles.



1. A simple factory class is the key to the entire pattern. It contains the necessary logical judgment and determines the class object to be created based on the given information. The client can avoid the responsibility of directly creating product objects, but only be responsible for consuming objects. This approach separates responsibility and rights and facilitates the optimization of the structure.

II. The factory class integrates the logic of all instances and violates the high cohesion principle. The class it can create can only be considered in advance, if a new class needs to be added, the factory class needs to be changed. When the number of products increases, there may be a need for the factory class to create different instances based on different conditions, this kind of judgment on the condition is intertwined with the judgment on the specific product type, which is prone to errors and is not conducive to system maintenance and expansion. These are all improved in the factory method model.

Factory method mode: Concept:It is also called the virtual constructor mode or the multi-state factory. It first creates an interface for the object, and then lets the subclass decide which class to instantiate. That is to say, the factory method delays the instantiation of a class to its subclass. Structure: Analysis:I. In the factory method mode, the core factory category is no longer responsible for creating all products, but instead, the specific creation work is handed over to the subclass for completion, this allows the system to create a new product without modifying the factory role. II. The factory method mode encapsulates the creation of objects in a centralized manner. It can be implemented without major changes when changing objects, reducing the coupling between customer programs and product objects, is a further abstraction and promotion of the simple factory model. Abstract Factory mode: Concept:Provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes. Structure:Abstractproducta and abstractproductb are two abstract products, both of which may have two different implementations. Product1, product2, product3, and product4 are specific classifications of two abstract products. Abstractfactory Abstract Factory interface, including the abstract methods for creating all products. Concretefactory1 and concretefactory2 are specific factories that generate abstract products and generate a specific product. Analysis:1. Easy to switch product series. A specific factory class only needs to appear once during initialization in an application, which makes it easy to change the specific factory of an application, it only needs to change the specific factory to use different product configurations. 2. Separate the specific instance creation process from the client. The client uses their abstract interfaces to manipulate the instance. The specific product class name is also separated by the specific factory and will not appear in the Customer Code. 3. It is difficult and laborious to expand products. If you need to add a new product, you need to change almost all factory classes. Class instance diagram: Simple factory mode:

Factory method mode:

Abstract Factory mode:

In summary:

Product Family: a collection of products with different product levels and functions.

Simple factory mode:

Generate any product of the same level

Create a factory. The function of a factory is to create an interface.

The factory class contains the necessary logic judgment, and the related class is dynamically instantiated Based on the client Selection

Factory method:

Generate a fixed product in a unified hierarchical structure

Only one abstract product class can be derived from multiple specific product classes.

Create an instance of a specific product category for each specific factory category

Move the internal logic judgment of a simple factory to the client for implementation. To add a function, modify the client code

The simple factory of customer service violates the open and closed principle to maintain the advantages of the encapsulated object creation process.

Abstract Factory:

Create a product with an abstract factory

Produce all products of different product families

There are multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes.

Create multiple product instances for each specific factory category

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.