Comparison of simple factories, abstract factories, and factory models
Author: Tindlewei
Blog: Http://blog.csdn.net/mennoa (reprint please indicate the source)
Github:https://github.com/tindlewei
I think we are all familiar with the factory model, but to distinguish simple factories, abstract factories, the factory model may not be so easy. So I looked at a lot of information and design patterns about the factory model introduction, below to summarize.
Objective
Simple factory, abstract factory, Factory mode all belong to the creation mode.
The pattern is characterized by an abstraction of the instantiation process. They help a system to be independent of those objects that create, group, and represent it.
It has two distinct characteristics:
1. They all encapsulate information about which specific classes the system uses.
2. They hide how these instances are created and put together.
Classification
- The first method: 3 Kinds of
This is the general case, the factory model is divided into three kinds:
Simple Factory mode
Factory classes in simple Factory mode typically use static methods to return different object instances by varying the parameters that are received.
If you do not modify the code, you cannot extend it.
Abstract Factory mode
Alias: Virtual Constructor
Provides an interface to create a series of related or interdependent objects without specifying their specific classes. Simply put, the Declaration and implementation of the object is separated.
Factory Method Mode
Alias: Kit
Defines an interface for creating objects, letting subclasses decide which class to instantiate. Simply put, the instantiation of a class is deferred to its subclasses.
- The second method: 2 kinds of
According to GOF 's classic "Design Model", it divides the plant model into two types:
Factory method Mode
Abstract Factory mode
The simple factory model is classified as a special case in the factory method model. Factory classes in simple Factory mode typically use static methods to return different object instances by varying the parameters that are received. You will often see a different switch in their factory, depending on the parameters, to return the various products.
- The third method: 4 Kinds of
There is also a saying, in addition to the above three kinds, there is a static Factory mode :
such as tool classes in projects, Textutil.isempty, etc., Class + static methods
Characteristics
Factory method Mode:
An abstract product class that can derive a number of specific product classes.
An abstract factory class that can derive a number of specific factory classes.
Each specific factory class can only create an instance of a specific product class.
Abstract Factory mode:
Multiple abstract product classes, each abstract product class can derive multiple specific product classes.
An abstract factory class that can derive a number of specific factory classes.
Each specific factory class can create instances of more than one specific product class.
Difference
Simple factory: Used to produce any product in the same grade structure. (inability to add new products)
Factory method: Used to produce fixed products in the same grade structure. (Support for adding any product)
Abstract Factory: Used to produce all products of different product families. (for adding new products, powerless; Support for adding product families)
The factory method pattern has only one abstract product class, and the abstract factory pattern has multiple.
Factory-Method-specific factory classes can only create instances of a specific product class, whereas abstract Factory mode creates multiple
Discuss
There are two common ways to parameterize a system with classes of objects created by a system.
One is to generate subclasses of created objects, such as factory method patterns. But if you want to change the product class, you need to create a new subclass.
Another way to parameterize a system is to rely more on the composition of the object: Defining a new factory object is responsible for creating the Product object.
In the case of factory method patterns, all products come from a parent class of abstract products, and different subclasses are created by different parameters.
In the abstract factory approach, each categorized product has a parent class for that product, and each class of objects is created and combined separately.
Often we use the simplest factory method pattern as a standard way to create objects.
However, this is not necessary when the instantiated class does not change at all or when the instantiation appears in an operation that the subclass can easily define, such as initialization operations.
Reference
The foundation of design mode reusable object-oriented software –gof
http://zyjustin9.iteye.com/blog/2094960
http://lh-kevin.iteye.com/blog/1981574
http://blog.csdn.net/hitwhylz/article/details/40381721
http://blog.csdn.net/jason0539/article/details/23020989
http://blog.csdn.net/jason0539/article/details/23020989
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Comparison of simple factories, abstract factories, and factory models