Simple factory mode, factory method mode, abstract factory Mode Comparison

Source: Internet
Author: User

 

Simple factory mode:

Factory classes in simple factory mode generally use static methods to return instances of different objects by receiving different parameters. without modifying the code, they cannot be expanded.

 

Factory method mode

The factory method mode provides a factory class for each product. Different factory instances are used to create different product instances.

Any product can be added to the same level structure.

Abstract Factory mode:

Abstract The concept that the factory model should deal with the product family. For example, every automobile company may want to produce cars, trains, and buses at the same time. Every factory must have a method to create bridges, trucks, and buses.

It is easy to add new product lines, but new products cannot be added.

 

Summary:

In the factory model, the most important thing is the factory class, not the product class. Product classes can be in multiple forms, with multi-level inheritance or a single class. However, it should be clear that the interface in factory mode only returns one type of instance. This is worth noting when designing product classes. It is best to have a parent class or an interface that is implemented together.

In factory mode, the returned instance must be created by the factory, rather than obtained from other objects.

The instances returned in factory mode can not be newly created, and the returned instances created in factory mode can also be.

 

Differences:

Simple factory model: used to produce any product in the same level structure. There is nothing to do with and add new products

Factory model: used to produce fixed products in the same level structure. (Any product can be added)
Abstract Factory: used to produce all products of different product families. (There is nothing to do with adding new products; Support for adding product families)

The above three factory methods have different levels of support in the hierarchical structure and product family. Which method should be used based on the actual situation?

:

 

 

 

 

 

FAQ:

 

1. Differences between factory method mode and abstract factory mode:

Factory method mode:

An abstract product class can be derived from multiple specific product classes. An abstract factory class can be derived from multiple factory classes. Each factory class can only create one instance of a specific product class.

Abstract Factory mode:

Multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes. An abstract factory class can be derived from multiple factory classes. You can create multiple product instances for each specific factory class.

Differences:

The factory method mode has only one abstract product class, while the abstract factory mode has multiple.

The factory method mode can only create one instance for a specific product type, while the abstract factory mode can create multiple instances.

 

2. Differences between simple factory mode, factory method mode, and abstract factory mode:

The simple factory mode, factory method mode, and abstract factory mode all belong to the creation design mode. The three creation modes do not need to know specific classes. We master the idea that when creating an object, we need to encapsulate it where it is easy to change to control the changes to adapt to the changes of the customer and expand the project.

Features:

Simple factory mode:

A dedicated class is used to create instances of other classes. The created instance usually has a common parent class. It is also known as the static factory method mode. In essence, a factory class dynamically determines which product class (these product classes inherit from a parent class or interface) to be created based on input parameters. In simple factory mode, all created objects are instances of a specific class that acts as the role. In this mode, the factory class is the key to the entire model. It contains the necessary judgment logic to determine the specific class object to be created based on the information given by the outside world. You can directly create required instances based on the factory class during use. You don't need to know how to create and organize these objects. It is conducive to the optimization of the entire software architecture.

 

Factory method mode:

The factory method is a design pattern with a small granularity, because the expression of the pattern is only an abstract method. Define the interface used to create objects in advance so that the subclass decides to instantiate a specific class, that is, add interfaces between the factory and the product, and the factory is no longer responsible for product creation, the interface returns a specific class instance for different conditions, which is implemented by a specific class instance. The factory method model is derived from the simple factory model and solves many problems of the simple factory model. First, the "open-close principle" is fully implemented to achieve scalability. Secondly, a more complex hierarchy is implemented, which can be applied to scenarios with complicated product results. The factory method mode is an abstraction of the simple factory mode. There is an abstract factory class (which can be an abstract class and an interface). This class will not be responsible for specific product production, but will only formulate some specifications. The specific production work will be completed by its subclass. In this mode, the factory class and product class can usually correspond in sequence. That is, an abstract factory corresponds to an abstract product, and a specific factory corresponds to a specific product. This specific factory is responsible for producing corresponding products.

 

Abstract Factory mode:

Abstract Factory mode is the most abstract and general factory mode in all forms. Abstract Factory mode refers to a factory mode used when multiple abstract roles exist. Abstract Factory mode provides an interface to the client, so that the client creates product objects in multiple product families without specifying the specific product. It has multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes. An abstract factory class can be derived from multiple specific factory classes, you can create multiple product instances for each specific factory class.

Each mode is a solution to a certain problem. The factory method mode targets a product level structure, while the abstract factory mode targets multiple product levels.

 

Advantages:

Simple factory mode:

The factory class contains the necessary judgment logic to determine when to create a product class instance. The client can exempt the responsibility of directly creating product objects, instead of simply "consuming" the product. The simple factory model achieves division of responsibility through this approach. The simple factory mode determines the object of a specific class based on the information given by the outside world. Use it. You can drag it out from the embarrassing situation where you directly create a specific product object. Isolated from specific classes, low coupling. Clearly differentiate their respective responsibilities and powers, which is conducive to the optimization of the entire software architecture.

 

Factory method mode:

The factory method model was designed to overcome the disadvantages of the simple factory model. The factory class in the simple factory mode requires a lot of methods (or Code) as the product class increases, while the factory method mode only completes a single task for each specific factory class, and the code is concise. The factory method mode fully satisfies OCP, that is, it has excellent scalability.

 

Abstract Factory mode:

The abstract factory model mainly aims to cope with the changes in the needs of the new series. Separating specific classes, the abstract factory mode helps you control the classes of objects created by an application, because a factory encapsulates the responsibilities and processes of creating product objects. It separates the implementation of customers and classes. The customers manipulate instances through their abstract interfaces, and the product class names are also separated in the implementation of specific factories. They do not appear in the Customer Code. It makes product series Easy to switch. A specific factory class appears only once in an application-that is, when it is initialized. This makes it easy to change the specific factory of an application. It only needs to change the specific factory to use different product configurations. This is because an abstract factory creates a complete product series, so the entire product series will change immediately. It facilitates product consistency. When a series of product objects are designed to work together, an application can only use objects in the same series at a time. This is very important, and abstract factories can easily achieve this. The abstract factory model facilitates such team division, reduces the coupling between modules, and improves the team development efficiency.

 

Disadvantages:

Simple factory mode:

When the product has a complex multi-layer hierarchical structure, the factory class only has its own, and should not change, is the disadvantage of the model. Because the factory class integrates the creation logic of all products, the entire system will be affected once it fails to work normally. It is difficult to expand the system. Once a new product is added, the factory logic has to be modified. This may make the factory logic too complex and violate the "development-closed" principle. In addition, the simple factory mode usually uses the static factory method, which makes it impossible to inherit from sub-classes, and the factory role cannot form a hierarchy based on inheritance.

 

Factory method mode:

It is not easy to maintain. If a specific product class needs to be modified, it is very likely that the corresponding factory class needs to be modified. When you need to modify multiple product classes at the same time, modification to the factory class will become quite troublesome (it is already a problem to check the number)

 

Abstract Factory mode:

The abstract factory model is difficult to cope with changes in the needs of "new objects. It is difficult to support new types of products. It is difficult to expand abstract factories to produce new types of products. This is because the Abstract Factory almost determines the set of products that can be created. To support new types of products, you need to extend the factory interface, which involves changes to the abstract factory and all its subclasses.

 

Applicability

Simple factory mode:

The factory class is responsible for creating fewer objects. The customer only knows the parameters passed in to the factory class and does not care about how to create objects.

 

Factory method mode:

When a class does not know the class of the object it must create or a class wants the subclass to specify the object it creates, when the class delegates the responsibility of creating an object to one of multiple help subclasses and you want to localize the information of which help subclass is the proxy, you can use the factory method mode.

 

Abstract Factory mode:

A system should not depend on the details of product instances such as creation, combination, and expression, which is important for all forms of factory models. This system has more than one product family, and the system only consumes one of them. Products belonging to the same product family are used together. This constraint must be reflected in the system design. The system provides a product library. All products use the same interface, so that the client does not rely on implementation.

 

 

 

In fact, whether it is a simple factory model, a factory model or an abstract factory model, they are essentially extracting unchanged parts and leaving the variable parts as interfaces to maximize reuse. Which design mode is more suitable depends on the specific business needs?

 

 

 

 

 

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.