Three kinds of design patterns factory pattern (Factory)

Three kinds of factory pattern

Factory mode can be divided into three categories:

  • Simple factory pattern (Simple Factory)
  • Factory method model (Factory Method)
  • Abstract Factory (Abstract Factory)

These three models from top to bottom and gradually abstract and more general.
In GOF "Design Patterns" book factory mode will be divided into two categories: factory method model (Factory Method) abstract factory pattern (Abstract Factory). The simple factory pattern (Simple Factory) is a factory method to see a special case of mode, both classified as a class.
The three factory pattern classification in the design mode are all created schema .

Create schema (Creational Pattern) instantiation process of the abstract class, can be created using separate software modules and objects in the object. In order to make more clear structure of the software, the outside world for these objects only need to know their common interface, but do not know the specific implementation details, the design of the entire system more in line with the principle of single responsibility.

Create schema is created in terms of what (What), who created (Who), when to create (When) and so provides the greatest possible flexibility for software designers.

Creating the schema hides the details of creating class instances, and how they are combined together to create the whole system to achieve the object is achieved by independent hidden objects.

Factory pattern is to create a schema more important. The main function of the factory model is to help us to instantiate an object. The reason why the plant model name contains words, because the instantiation object is achieved by a plant, the plant is replaced with a new operation.

The advantage of this package is an example of the details of the object, in particular the case for example of more complex life cycle or target should be centrally managed. It will bring greater scalability and minimize the amount of modification of your system.

Next we were introduced to the three factory pattern.

Simple factory pattern
simple factory pattern is also called static factory method (Static Factory Method) mode. Simple factory pattern is determined by a factory object to create an instance of what kind of product class. Simple factory pattern is the factory model family is the most simple and practical model can be understood as a special mode of realization of different plants.

Simple factory pattern summary

Factory class is the key to a simple factory pattern. It contains the necessary logic to determine, based on the information given outside, decide whether the object should be created which specific class. By using the factory class, the outside world can emerge from specific products directly create objects of embarrassment, just need to be responsible for "consumer" objects on it. And do you need these objects exactly how to create and how to organize. Clear the respective responsibilities and rights, help to optimize the entire software architecture.

However, due to factory class focused on creating a logical all instances of violation of the principle of high cohesion allocation of responsibilities, all create a logical focus to a factory class; it can only be created in advance taking into account the class, if you need to add a new class, then we need to change the factory class.

When the system is in specific product categories growing time, there may be requirements created by the factory needs of different instances according to different criteria. This judgment and the judgment of the conditions of the specific type of product intertwined, it is difficult to avoid the spread of module functions, the maintenance and expansion of the system is very unfavorable;

These shortcomings in the factory method pattern to a certain resolve.

 

Factory Method pattern

Factory method model (Factory Method Pattern), also known as factory mode, also called virtual constructor (Virtual Constructor) mode or a polymorphic plant (Polymorphic Factory) mode, which belongs to the class created schema.

Mode is a factory method to achieve the concept of object-oriented design patterns "plant." Like other creational patterns, it also deal with the problem in creating an object without specifying the specific type of the object.

The method uses factory mode

Factory Method pattern and factory pattern, though they are simple to create objects through the factory, the biggest difference between them is - factory method pattern is designed to complete in full compliance with "the principle of opening and closing."

The method can use the factory mode in the following situations:

A class does not know it needs an object of class: the factory method pattern, the client does not need to know the class name specific product category, you only need to know the corresponding plant to specific product object is created by the concrete factory class; the client needs to know to create product-specific factory class.

A class specified by a subclass which object created: mode method in the factory, the factory for the abstract class only needs to create a product interface, the object can be determined by the subclass to create specific, object-oriented polymorphism and Richter substitution principle, the program is running, an object subclass will override the parent class object, making the system easier to expand.

The task entrusted to create an object of a certain client multiple plants subclass when used without concern for what may be a sub-class factory to create products sub-category, and then dynamically assigned when needed, can be the name of a specific class factory class stored in the configuration file or database.

 

Factory method pattern includes the following roles :

Product: abstract product Operation( )

ConcreteProduct: specific products ( OperationAdd)

Factory: Abstract Factory ( IFactory)

ConcreteFactory: Concrete Factory ( AddFactory)

 

Abstract factory pattern

Abstract Factory (Abstract Factory Pattern): create a series of related or dependent objects interface, without specifying their concrete classes. Abstract Factory pattern, also known as Kit, belonging schema object creation.

Abstract factory model provides a way to separate the plant of the same product family may be encapsulated. In normal use, the client program needs to create a concrete implementation of the abstract factory, and then use the abstract concrete object factory to create the subject as an interface. The client does not need to know (or care) that specific types of objects obtained from the factory in the interior of these methods, since the common interface uses only these client objects. Abstract Factory pattern implementation details a set of objects and their general use separate.

Abstract factory pattern uses

Abstract factory pattern and factory method patterns, are compliant with open - closed principle. But the difference is that the factory method pattern to increase a specific product, to be increased corresponding factory. But the abstract factory pattern only when a new type of product need to add specific plant. That is to say, a factory model factory method can create only one specific product. And a factory abstract factory pattern can create a variety of specific products belong to a class type. The number of factories to create products between simple factory pattern and factory method pattern.

Abstract factory model can be used in the following cases:

A system should not depend on how the product class instance is created, a combination of details and expression, which is important for all types of factory pattern.

System has more than one product family, wherein each time only one product family.

Products belong to the same product family will be used together, the constraints must be reflected in the design of the system.

The system provides a product class library, all of the products appear in the same interface, so that the client does not depend on the specific implementation.

Abstract factory pattern implementation

Abstract Factory pattern includes the following roles:

The method used to declare generate an abstract product: AbstractFactory (Abstract Factory)

ConcreteFactory (particularly plant): abstract factory implements the methods declared abstract products generated, generating a set of specific products that constitute a family of products, each product a product is located in the hierarchical structure;

AbstractProduct (abstract product): a statement for each product interfaces, abstract business methods defined in the abstract product of the product;

Product (product-specific): Define the specific factory production of specific products subject to achieve abstract business methods defined in the interface product.

 

Three kinds of factory pattern comparison

The advantages and disadvantages of simple factory pattern

  • advantage:
    • 1, the specific implementation of shielding products, the caller only concerned with the interface of the product.
    • 2, simple
  • Disadvantages:
    • 1, increase product, you need to modify the factory class, does not meet the open - closed principle
    • 2, factory class focused on creating a logical all instances of violation of the principle of high cohesion allocation of responsibilities

Advantages and disadvantages of the factory method pattern

  • advantage:
    • 1, it inherits the advantages of simple factory pattern
    • 2, conforms to the Open - Closed Principle
  • Disadvantages:
    • 1, increase product, need to add new factory class, resulting in increasing the number of pairs of system classes, increasing the complexity of the system to some extent.

Advantages and disadvantages of the abstract factory pattern

  • advantage:
    • 1, isolating the generation of specific classes, so that customers do not need to know what is being created
    • 2, each class can be created by a plurality of objects a concrete factory product family, product family is increased or alternatively more convenient, to add new concrete factory product family and easy;
  • Shortcoming
    • Adding new products hierarchical structure is very complex, and the need to modify the abstract factory all the concrete factory class, to support "the principle of opening and closing," the inclination of the presentation.

Simple Factory: any product used to produce the same level structure. (For adding new products, mainly new products, it is necessary to modify the factory class in line with the principle of responsibility does not meet a single opening. - closed principle)

Factory method: for producing the same hierarchical structure of the product is fixed. (Support add any product, no need to change the existing factory when new products, the need to increase the product corresponding to a single factory in line with the principles of responsibility, in line with the open - closed principle but introduces complexity.)

Abstract Factory: used to produce different product families of all products. (When adding new products, the plant needs to be modified to increase the product family, the need to increase the plant in line with the principle of single responsibility, partly in line with the open - closed principle, reduced complexity)

Finally, all three models have advantages and disadvantages factory, there is no best, only the most suitable!

 

Reference link: https: //www.hollischuang.com/archives/3709

Guess you like

Origin www.cnblogs.com/lucky1024/p/11512284.html