Design Patterns Talk - Factory Method Pattern and Abstract Factory Pattern
refer to
"Dahua Design Pattern"
http://ichennan.com/2016/08/09/DesignPattern.html
Overview
Originally 工厂方法模式
, , I felt very clear and clear, but when I learned it later 抽象工厂模式
, I instantly felt confused. 工厂方法模式
Is 抽象工厂模式
n't it the same? There is no difference between the two. After the help of Du Niang, I have some of my own views to share with you.
Factory Method Pattern
Let's take a look at the professional explanation of the factory method pattern first.
Factory Method: Define an interface for creating objects and let subclasses decide which class to instantiate. A factory method delays the instantiation of a class to its subclasses.
My humble opinion:
I think the factory pattern is an optimization of the simple factory pattern and solves the problem that the simple factory violates the开放-封闭
principle. If an operation object is added to the factory method, there is no need to modify the factory class. Just build a new factory. It is up to the client to decide which subclass of the object to instantiate, rather than putting the object creation in a factory.
Although the factory method solves the开放-封闭
principle of violation of simple factories, it also brings about an increase in complexity: each new specific operation class needs to add an additional factory class. Created a lot of factory classes.
Abstract Factory Pattern
Similarly, let's take a look at the professional explanation about the abstract factory
Abstract Factory Pattern (Abstract Factory), provides an interface to create a series of related or interdependent objects without specifying their concrete classes.
In my humble opinion
, I think the biggest feature of the abstract factory is that the abstract factory creates a series of products, such as the peripheral factory in the figure above, which creates a series of peripheral-related products.
Summarize
difference
The biggest difference between the factory method pattern and the abstract factory pattern is: the factory method pattern, a factory only creates one product, while the abstract factory pattern creates a series of products.
common ground
Both factory methods and abstract factories are improvements or extensions to the simple factory pattern, but the core programming idea is still 工厂
the idea of .
finally
Sometimes, the analysis between design patterns is not so obvious. When we learn design patterns, we must not copy the templates of design patterns, but absorb their ideas. Sometimes, we may modify the standard patterns to make them suitable for specific Environment, sometimes we may only use part of a certain design pattern, or use multiple design patterns together.
Regarding this article, it is my own summary of the two modes. If there are any mistakes in the article, please correct me.