Java design pattern factory model (simple factory method)

xiaoxiao2021-03-14  225

Factory mode

The factory model belongs to the creation mode of the class, and the factory model is responsible for instantiating a large number of common interface classes. Factory mode can dynamically decide which class instantiate, without having to know which class is to be instantified each time. Factory model has three basic forms:

1. Simple Factory mode, also known as static factory method mode (Static Factory Method) (STATIC FACTORY METHOD)

Pattern).

2. Factory Method mode, also known as polymorphic factory mode or virtual constructor mode.

3. Abstractical Factory mode, also known as the toolbox (Kit or Toolkit) mode.

Simple factory model

We use the legend to understand the basic semantics of the simple factory model. All of the following illustrated is only a representative form of understanding, in order to express it, in certain aspects, it may violate the basic semantics in the modeling diagram.

figure 1

The simple factory model has a very important role in learning a single example and multiple models.

Figure 1 shows the fruit planting situation of the farm, which is obvious, we can know that the Fruit interface class has three methods (Plant, Harvest, Grow); due to the Apple and Grape class implementing the Fruit interface, you must implement all methods in the Fruit class, where Apple and Grape each have related methods. As for the relationship between them, I will not list the code.

Farm gardener (

FRUITGARDENER is also part of the system, which is naturally represented by a suitable class. This class is the Fruitgardener class,

The FRUITGARDENER class creates different fruit objects, such as Apple, Grape (GRAPE), based on the request of the client. And if you receive an illegal requirement, the Fruitgardener class will throw the BadFRuitexception exception.

/ ******************* The following is the gardener of the farm

******************** /

Public Class Fruitgardener

{

/ **

* Static factory method

* /

Public Static Fruit Factory (String Which) THROWS BADFRUITEXCEPTION

{

IF (Which.equalsignoreCase ("apple"))

{

Return new apple ();

}

Else IF (Which.equalsignorecase ("grape"))

{

Return new grape ();

}

Else

{

Throw New Badfruitexception ("Bad Fruit Request");

}

}

}

/ ********************* The following is a custom anomalous ******************** /

Public Class BadfruitException Extends Exception

{

Public BadFRuitexception (String MSG)

{

Super (MSG);

}

}

/ ********************* User call ******************** /

..................

Try

{

Fruitgardener.Factory ("grape"); Fruitgardener.Factory ("apple");

Fruitgardener.Factory ("xxx");

}

Catch (Badfruitexception E)

{

...

}

.......................

It can be seen that simple plants involve three roles:

1. Factory role: responsible for creating a product. As the Fruitgardener class above. It is the core of the factory method model. It is often achieved by a specific class.

2. Abstract product role: Implement the product of the product, which can be implemented by an abstract class or interface. FRUIT above

3. Concrete products: A specific product, such as Apple and Grape

note

1. Factory roles in the factory method can also be played by an abstract product role. That is, the abstract product role has served as an object. In the Java API, such as: java.text.dateformat,

Abstract product roles are defined as an abstract class instead of an interface. (Differences and applications between abstract classes and interfaces, I will continue to list related content later). An abstract class cannot have its own instance, the factory method of DateFormat is a static method, not a normal method. That is to say, it is directly in the class itself, and does not instantiate the class.

figure 2

2. If the abstract product role is omitted, the factory role can be merged with the specific product role.

Public Class Fruitgardener

{

Public fruitgardener () {}

/ **

* Static factory method

* /

Public static fruitgardner factory ()

{

Return new fruitgardener ();

}

}

The above two points are similar and multi-class modes, but it is not equal to a single example or multiple models. In the back mode design, it will be mentioned, don't worry. This is a degradation phenomenon of simple factory model. As follows:

Simple factory model and other models

1.

Relationship with single example mode

Simple factory mode is used in single case mode. In other words, a single example has an instance of a static factory method. An abstract product class is also a factory factory. (as shown in picture 2)

However, single example mode is not a degradation of simple factory models.

Single-size mode requires a single-case constructor to be private, so that the client cannot instantiate it directly, and must be instantiated by this static factory method, and the single-size class itself is its own factory role. In other words, a single case is responsible for the creation of its own instance. Single examples use a static property to store their own unique instances, factory methods will always provide this instance. (To say this, I am very excited, I finally know why these modes in Javaapi provided by Sun)

2.

Relationship with multiple cases

Multiple models is the promotion of single example mode. The commonality of multiple models and singletics is

They all prohibit the exemplification of the outside world, and they provide a cyclic use of their own instances through a static factory method. They differ in that only one instance is only one example, and the plurality of models can have multiple instances.

Multiple modes often have a gathering attribute that registers the instance of the loop usage instance by registering this aggregation property. In general, a typical multiple class has some internal state, which can be used to distinguish each instance; there is only one instance of each of the internal states.

Note: The relationship between the aggregation representation is the overall relationship between the overall and part, it is divided: sharing aggregation and composition;

The part of the shared condensed condenses can be part of a plurality of overall; the composition is only a whole and partial relationship, and the overall disappearance is disappeared. It should be noted that some object-oriented masters are different from the definition of aggregation. Other understanding of multiple models, is not difficult, we can understand, especially aggregation relationships through JavaAPI.

According to the participation of the outside world, the factory method can query your own registration aggregation. If you have an instance of this state already existed, you will directly provide this instance to the outside world; Go to the gathering, then then provide to the client.

About single example mode and multi-model modes detail, will be described later.

3.

Memo mode

Single examples and multi-case models use an attribute or aggregate property to register the created product object so that you can find and share the product objects that have been created by querying this property or aggregation property. This is the application of the memo mode.

4. MVC

mode

MVC

The mode is not a strict design pattern, but in a higher level of architectural mode.

MVC

Mode can be decomposed into a combination of several design modes, including synthesis patterns, policy modes, and observer patterns, and may include decorative mode, mediation mode, iterative score, and factory method mode.

The objects created by simple factory models often belong to a product level structure, which can be views in the MVC mode (VIEW); and factory role itself can be a controller (Controller). A MVC mode can have a controller and a plurality of views, as shown below.

image 3

We combine simple factory methods, you can know that the Controller in the figure is also a factory role, (Fruitgardener) it is responsible for creating product View. If the system needs multiple controllers to participate in this process, simple factory models do not apply, and we should consider using factory method patterns, we will describe it later.

Application of simple factory method

The use of static factory methods is to hide the work of specific subclasses, so that the client does not have to consider how to instantiate specific subclasses because abstract classes

DateFormat provides an example of its appropriate specific subclass. This is an excellent application of simple factory method model.

With the specific product class, it hides its real type, which is the advantage of providing system scalability. If new specific subclasses in the future are added to the system, then the factory class can replace an object that handed over to the client to a new subclass, and there is no impact on the client.

This type of return type of the factory method is made into an abstract product type, called the abstract programming, which is dependent on the principle of reversal (

Application of DIP). We will describe the specific applications of "Relying on the Principle (DIP)".

Disadvantages of this mode

It is not difficult to see that if new products such as: Orange, you need to modify the factory class (FRUITGARDENER) to limit its flexibility; and for the product character class is suitable, simple factory is only supported "open-closed "in principle.

The factory class integrates all the creation work, which is a comparison of the factory class. If there is a problem, all creation work will not be possible.

When the product class has different interface types, the factory class needs to judge when to create some kind of product. This determination of the timing and the judgment logic of which specific product are mixed, so that the system is difficult to expand in the future. This disadvantage is overcome in the factory method mode.

Since the simple factory model uses a static method as a factory method, the static method cannot be inherited by subclasses, so

Factory roles cannot form a hierarchical structure based on inheritance. This disadvantage will be overcome in the factory method mode.

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

New Post(0)