Design mode [2] - Old talk - all kinds of factory models

xiaoxiao2021-03-06  39

Design mode [2] - Old talk - all kinds of factory models

First, preamble

In all known various modes, the most important and scope of application should be aware of the creation mode factory model. Especially in the programming of Framework, it is more exaggerated in almost any outstanding framework for almost any excellent framework. The plant model is simpler, but it has embodied the most important characteristics of object-oriented design, with abstraction, packaging, entrustment, inheritance, and polymorphisms in factory models. In fact, the plant model believes that everyone will not feel unfamiliar, and even more friends do not consciously or consciously use it during the actual work. Writing this article is to let people unclear or unfamiliar with factories to understand the idea of ​​this mode design and how to apply in actual software design and development.

Second, definition

In order to separate the object's creation process and the use process, the universal interface is defined to create an object.

Third, describe several factory models

Template Method

From simple to complex, it introduces the most basic way for complex things. From simple to talk in all factory modes, the easiest factory model should be used to create an instance of an object using Template Method. There are also many people think that this method is still not a factory model, but from the essence of the nature and the actual operation mechanism, it should be possible to be a special form of factory model.

The specific implementation method is to add a method in the target class, the purpose of this method is to create an example of the object or an abstraction of the object. Under normal circumstances, this method is usually declared in order to facilitate use.

Sample code:

Public Class Vehicle

{

Private vehicle ()

{

//

// TODO: Add constructor logic here

//

}

Private void preparebehavior ()

{

// Initialization Settings Object

}

///

/// Template Method is responsible for creating an instance of a VEHICLE

///

///

Public static vehicle getInstance ()

{

Vehicle objvehicle = new vehicle ();

Objvehicle.preparebehavior ();

Return Objvehicle;

}

}

The above code is the simplest application. As I designated in the previous article - the use of the old talk, the use of the model, using this way, can detach the New operation from the business logic class, to demonstrate business logic Coupling between the class and the Vehicle specific class (TYPE). Drop the operation of creating an object in the system from the business logic class, change to use a separate method to complete, so that even if the process of creating an object in the future, this change is not related to our business logic coding, we need It's just to modify the implementation code of this method. Before getInstance () method, we can do a variety of preparations for this instance: You can modify some states, perform some of our necessary methods, if we need us to let the getInstance () method directly return to the specific implementation of the Vehicle class Subtype. All of these implementations are sealed, and they have nothing to do with the outside.

In the actual project application, if the inheritance system of the target object is relatively simple, fixed, the function is relatively single, and the change in the future is easier to expect, and this method can be considered.

Simple factory model

Generally, a factory class corresponds to a plurality of specific product classes, and judges the creation of a particular product class through the external crossing parameters.

Since it is called "factory", certainly cannot produce only the same product, the simple factory only produces the same family product, specifically, the class created by the simple factory is from the same abstraction. Creating different classes based on customer different needs, but the class being created must meet a condition: must be the product of the same family (from the subclass of the same root). For example: there is a simple factory that produces fruits, according to customer needs, can produce apples, oranges, grapes, bananas, but must satisfy a request, it can only produce fruit. If the customer needs the machine, the house is waiting, then these requirements will not be met. Simple factory structure is simple, consisting of three parts:

Factory: It is the core part. As a simple factory-external interface, it is responsible for implementing the logic of creating instances.

Abstract product: The factory produces abstraction of all products, which may be an abstract class or interface, or may be a normal base class.

Specific product: an example of a specific class created by a simple factory.

Sample code

Public Class Vehicle

{

}

Public Class Car: Vehicle

{

}

Public Class Train: Vehicle

{

}

Public Class SimpleFactory

{

Public Vehicle Create (String Type)

{

IF (Conditional Judgment)

{

Return new car ();

}

IF (Conditional Judgment)

{

Return new train ();

}

}

.... Other judgment conditions

Return new other ();

}

In the three parts of the simple factory structure, the factory-class part is the weight, all core functions are implemented by factory class, such as which specific product created according to the conditions, and actually created products. In this point, it violates the "principle of balanced distribution of objects", because the function is too concentrated in the factory class itself, with the increase in the product species produced by simple factory, it will make it itself increasingly increlenting The more complicated and difficult to maintain and expand. For example, if you add a product, you must change the code logic of the factory class. Simple factory is easy to use than factory methods and abstract factories. Only by one factory class can create all product classes, and the disadvantage is that as the product is increased, it is increasingly difficult to use. When the factory only produces the specific products of the same variety, and when the number of products is limited, it is possible to consider using this plant mode.

Factory Method mode

Define a standard method in the factory parent class to produce products. Generally, this method should be declared as an abstract method (when the factory's parent class is an abstract class), or the empty method in the interface (factory parent class is just an interface), this method only has the definition of the method and has no specific implementation. The specific implementation of this method is delayed into the subclass. Such a parent class only needs to take out an abstract product without having to care about how to create a specific product and which specific product created. As for why specific products that should be created are determined by the specific subclasses. Considering that the simple factory models are defective in the Factory Method mode, the definition of factory production products and the implementation of specific products in the Factory Method mode are separated, and a class's inheritance system is used. Eliminate complex logical judgment statements in plant classes. Thus, the responsibility of the class can be relatively equalized.

The Factory Method structure consists of four parts:

Abstract factory: Define unified interfaces to create products.

Specific class factories: implementation of a class factory that creates a specific product class (generally multiple).

Abstract products: Factory produces abstraction of all products, possibly abstract or interface, or may be ordinary base classes.

Specific product: instance of the specific class created by Factory Method (multiple).

Sample code

Public Abstract Class AbstractFactory

{

Public Abstract vehicle create ();

}

Public Class Carfactory: AbstractFactory: AbstractFactory

{

Public Override Vehicle Create ()

{

Return new car ();

}

}

Public Class Train Factory: AbstractFactory

{

Public Override Vehicle Create ()

{

Return new train ();

}

}

In this way, the client program can create different specific products in a unified manner by changing the subclass of the factory class of the created product. Even if you need to increase the production of new products, you don't need to modify the existing code, just create a substru factory that produces this product. The premise is that the new product and the original factory must be a class (interface must be consistent). The advantage of the factory method mode is that due to the production of specific products, adding new products do not affect existing procedures, from this point to see the program is "closed". The implementation of the modified sub-plant class does not affect the use of the client. At the same time, its limitations are also obvious, in the factory method mode, because all specific products are inherited with the same parent class (or interface), it can only produce the specific product of "same kind". For example, this example: This plant can only produce various kinds of Vehicle, if you need food, fruits and electronic products, then the above factory method model is impossible to satisfy you. There is also because of the production of products directly in the client, if you need to modify the type of production product, you need to modify the client's code, which does not satisfy the abstract principles. If you want to solve this problem, you need to combine with other design patterns.

Abstract Factory mode

The factory model above is not capable of producing different kinds of products. In order to solve this problem, you need to use the Abstract Factory mode. Abstract factory models and factory method patterns have a big difference. The purpose of the Abstract Factory mode provides an interface to create a series of associated or interdependent objects without specifying their specific classes. Products created using abstract factories are "product families", which are products or more than two ethnic groups, and these products are "related", "interdependence". If only one product is produced, the abstract plant mode is completely degraded into factory method model.

So there are two prerequisites for abstract factories use:

1. The production product family is greater than a family, and only the products above the production of the two are using the abstract factory.

2. There are special relevant or dependencies between products. There should be a minimum of two relationships between products, and there is a inheritance relationship between products within each product group. For example: There is a inheritance relationship between ABSTRACTPRODUCTA and PRODUCTA1 and PRODUCTA2 is the same product. The products between the two product families should also have a correspondence between the two product families, such as the correspondence between ProductA1 and ProductB1, or the corresponducta2 and ProductB2. Under the premise of only the condition 1, it feels that multiple factory method patterns should be used instead of abstract factory models so that it should be convenient.

转载请注明原文地址:https://www.9cbs.com/read-52086.html

New Post(0)