Understanding differentiation: simple factory pattern, factory method pattern, abstract factory pattern

Time:2021-4-14

preface

Many people don’t understand the difference between simple factory mode, factory method mode and abstract factory mode. It’s hard to see the theory. What should we do?

Here, we do not mention too many theories, but only from the perspective of code structure, we provide a framework thinking, as follows:

Simple factory model

Class structure

A factory class
A product abstract class (or interface)
N specific product implementation classes

use

1. According to the requirements, implement the product abstract class one by one
2. In the factory class, which product object is generated according to the requirements

Factory method model

Class structure

A factory abstract class (or interface)
N factory implementation classes
A product abstract class (or interface)
N product implementation classes

use

1. According to the requirements, implement the product abstract class one by one
2. According to the requirements, the abstract factory classes are implemented one by one, and the corresponding products are generated in each factory implementation class (compared with the simple factory mode, it is found that the production responsibilities are divided into specific factory classes, that is, if I want to add a new product now, I just need to add a new product implementation class and the corresponding factory implementation class)

Abstract factory pattern

Class structure

A factory abstract class (or interface)
N factory implementation classes
N product abstract classes (or interfaces)
N product implementation classes

use

1. According to the requirements, product abstract classes are implemented one by one according to product families
2. According to the requirements, implement the factory abstract classes one by one. In each factory implementation class, the production operation is carried out according to the product family (because it is no longer aimed at a single product, the production method in the factory implementation class is usually multiple)

summary

OK, now let’s look at it again. Is it very simple?

The complexity and abstraction of the three patterns are gradually increasing.

Simple, only abstract products

In general, the product and factory are abstracted, and the responsibilities are separated, which conforms to the principle of opening and closing

Abstract, no longer by product class, but by product family, so there will be n product abstract classes

But no matter what kind of product, the product must be abstract, only the third kind of abstraction is a little more

notes: the above list is only the structure that may appear under normal circumstances, and there is no list of variants

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]