Summary: The core actors of the creation pattern, factory, simple factory, abstract factory, remember? Review and compare.

The author | | “high source ali technology to the public

preface

The core actors of the creation pattern, factory, simple factory, abstract factory, remember? Review and compare this article.

Why is a factory needed

There is always a need to create objects in the system, and you usually use new() to create objects. Create objects can be as simple as a new (), can also have complex processing logic, if the object is created in the main program, so is the main logic and the logic coupled together to create objects of the trunk, will have to do a factory pattern is non-core object creation logic and decoupling, the trunk logic by calling the factory the creation method of direct access to an object.

Role division in factory mode

  • Abstract product roles (both available)
  • Specific product roles (both available)
  • Abstract Factory roles (factory method patterns and abstract factory patterns have)
  • Specific factory roles (both available)
  • Context roles (both) – Call factories to get objects

Basic knowledge of the factory

The factory method always returns the corresponding product object, that is, the factory was used to produce the product, based on some input parameter.

Factory methods may create different concrete product entities based on different conditions, so the return type of factory methods is an abstract product type.

Simple division of factory model

1 Simple factory pattern

Products have an abstraction layer, but factories have no abstraction layer, and the corresponding product types are created in a factory based on the pass parameters.

If you need to add a new product family (for example, add C_cpu to A_cpu and B_cpu), then you need to make logical changes in the factory method. There is no open closed principle – open for extension, closed for modification.

The simple factory mode is the static factory mode, which is not part of GOF23. It is a simple encapsulation. In the static factory mode, the factory method is static, so it is called static factory method.

2. Factory method pattern

There are layers of abstraction for products, and there are layers of abstraction for factories, one factory for each product (that is, a factory can only create one product category).

If new products are added, only new factories need to be added. There is no need to modify the original factory logic, which conforms to the open and closed principle.

Abstract factory pattern

Products have layers of abstraction, factories have layers of abstraction, one factory for multiple products (that is, one factory can create multiple product categories).

It is used to solve the substitution problem of product family (A product family, B product family) when there are multiple product categories (chip, motherboard, graphics card).

Detailed explanation of simple factory model

In the simple factory pattern, there is only one factory class and no level of abstraction for the factory role.

Different product implementations are produced using this one factory. For example, specific product implementations such as ACpu and BCpu are produced using the CpuFactory product class.

If you want to add a new product implementation, such as CCpu, you need to modify the code for the factory method.

In the simple factory pattern, the logic that determines which products to build is implemented inside the factory.

1 DEMO: Abstract product interfaces

2 DEMO: Specific product implementation

3 DEMO: Specific product implementation

4 DEMO: Simple factory class

5 DEMO: Test class — client code that calls the factory-created object

Detailed explanation of factory method model

In the factory method pattern, the factory is also abstracted. An abstract factory still corresponds to a product category. For example, CpuFacroy interface corresponds to the production of Cpu products, but the specific product implementation is handed over to each specific factory implementation. That is, under the CpuFactory interface, there are ACpuFactory and BCpuFactory to produce ACpu and BCpu products respectively.

In the factory method pattern, the specific factory is responsible for creating specific products, and the logic inside the factory is only responsible for creating corresponding objects. The logic that determines what products are generated is in the external client, and the client indirectly decides what products are generated by choosing to use the specific factory.

1 DEMO: Abstract product interface

2 DEMO: Specific product implementation

3 DEMO: Specific product implementation

4 DEMO: Abstract factory

5 DEMO: specific factory implementation

6 DEMO: specific factory implementation

7 DEMO: Test class — context

THINK: The difference between simple factory and factory method model

The factory class in the simple factory pattern has no abstraction layer, whereas the factory method pattern has abstract factories.

Where is the logic to determine what products are generated?

  • In the simple factory pattern, the client decides what type of parameters to pass and creates different objects based on different parameters in the factory, that is, there is logic inside the factory that decides to create products.
  • In the factory method pattern, the inside of the specific factory is pure, the type of factory is responsible for creating the corresponding product, and the logic of decision is all implemented in the client side.

The simple factory pattern needs to modify the factory logic when a new product implementation is needed, whereas the factory method pattern only needs to add a concrete factory implementation and does not need to modify the internal factory logic.

THINK: Factory Approach & The difference between abstract factory patterns

1. Factory method pattern

The factory method pattern is used to solve the case where one product (Cpu) has multiple product families (ACpu,BCpu). Abstract products, such as CpuFactory, are used as factories. Each product family corresponds to a specific factory, such as ACpuFactory and BCpuFactory.

In the factory mode, if a new abstract product is added, a new abstract factory, such as MainboardFactory, needs to be created. That is, if you need to add new product lines (XianKa), then need to add abstract factories (XianKaFactory); If you need to add a specific product under a product (Cpu), you need to add a specific factory (CCpuFactory).

In the factory method pattern, factories are built in the abstract product dimension, one factory for each abstract product, when the product family (A,B,C…) Increases, the need for each product the abstract factory new concrete factory (CCpuFactory CMainboardFactory, CXianKaFactory).

Abstract factory pattern

In the factory method mode, when the product line (category: CPU/motherboard/graphics card) and product family (A/B/C) are expanded horizontally, if A new product family is added, all products of the new product family need to be added under all factories, that is, in the CPU factory, motherboard factory, graphics card factory… Add C concrete factory, this is obviously too much change, at this time to consider using abstract factory mode.

The abstract factory pattern, the dimension of product family to A factory, A factory have more than one creation method, each creation method is responsible for creating A product line, such as A factory and B factory, each factory has created the method of CPU, the way to create the mainboard, creating graphics method, when the need to add A new product family, only need to add A plant, such as C factory, Create individual products in the factory.

3 summary

The major difference between the factory method pattern and the abstract factory pattern is that the factory method pattern targets a single product level structure, while the abstract factory pattern targets multiple product level structures.

Abstract factory model in detail

Abstract factory pattern roles

  • Abstract product
  • Specific products
  • Abstract Factory: A factory class is built on the product family dimension, which contains multiple abstract methods, each of which creates a product.
  • Concrete factory: The concrete factory implements the abstract factory and implements all product creation methods within it, creating objects that belong to the current concrete product.
  • Environment class: Holds abstract factories and can inject concrete factories at run time.

2 DEMO: Abstract product

3 DEMO: Specific product

4 DEMO: Abstract factory

5 DEMO: Specific factory

DEMO: Test class – like program context

THINK: Are methods in factory mode necessarily static?

In a simple factory, the factory method has no corresponding abstraction and can be defined as static.

In both the factory method pattern and the abstract factory pattern, factories have corresponding abstract interfaces, and abstract methods in interfaces cannot be defined as static, so factory methods cannot be static either.

11 THINK: indicates the understanding of static interface

The interface and the methods in the interface are public abstract.

Methods in interfaces are abstract and have no body, so they are not allowed to be static (static means that a class can call a method).

As of JDK8, an interface can have static methods, which must have a method body. That is, an interface can have non-abstract methods, just like an abstract class.

Factory pattern & Usage scenario of abstract factory

A system should not depend on the details of how instances of product classes are created, composed, and expressed, which is a concern of all factory patterns.

When the products in the system have multiple product families, the system should be designed for the product family, that is, the abstract factory pattern, although any specific request belongs to only one of the product families.

When the system has multiple product family, and the product family of products is usually used together, together to create, if this constraint needs reflected in the system design, you should use the abstract factory pattern (for example: whether A product family, or B product family of chips, motherboard, disk, these products usually need to create together).

Advantages and disadvantages of the Abstract factory pattern

1. Advantages of abstract factory

Separating the interface from the implementation

The client uses an abstract factory to create the desired objects, and the client has no idea who the implementation is. The client is just a product-oriented interface, which means that the client is decoupled from the concrete product.

Makes it easy to switch product families

Because A specific factory represents A product family, for example, from A series to B series in the above example, we only need to switch the specific factory.

2. Disadvantages of abstract factories

It’s not easy to expand new products

If you need to add a new product to the product family, you need to modify the abstract factory, you need to add new product creation methods to the abstract factory, and you need to add interfaces to all the concrete factories.

14 Best Practices

Both use the abstract factory pattern to build factories along product family dimensions, with one method in the factory if there is only one product and multiple methods if there are multiple products.

The original link

This article is the original content of Aliyun and shall not be reproduced without permission.