In-depth plant mode

xiaoxiao2021-03-06  57

First, the primer

Ten years ago, there was an explosive household, and his family had three cars (Benz (Mercedes), BMW (BMW), and Audi (Audi) seems to be patriotic, no Japanese car), and hired drivers to drive him. However, when the explosive households are always like this: I'm talking to the driver after the Benz car and said, "Mercedes!", After sitting on BMW, he said, "Kai Bao!", He said, "Okudi!"

You must say: This person has disease! Say it directly not to drive? !

And when the behavior of this explosive household is placed in our program language, we found that the C language has always been in this way!

Fortunately, this disease can be avoided in the OO language. The following is based on Java language-based topics: factory model! !

Second, introduction

Factory model is mainly to provide an interface for creating objects. The factory model is divided into three categories in accordance with the proposed law in Java and Mode:

1. Simple Factory (Simple Factory)

2. Factory Method (Factory Method)

3. Abstract factory model (Abstract Factory)

These three modes are gradually abstract from top to bottom, and more general.

There is also a classification method, which is a special case of the simple factory model as a factory method model, and two are classified as a class. Both can be, this is a classification method using "Java and Mode".

In what case, should we remember using factory models? There are two points:

1. Do not foresee the instance of which kind of instance you need to create when encoding. 2. The system should not rely on how the product instance is created, combined, and expressed in the factory model to give us OOD. What are the benefits of OOP? ?

After we finish, you can know you.

Third, simple factory model

As the name suggests, this model is very simple, and it is used in the case of simple business.

It consists of three characters:

Factory role: This is the core of this mode, with a certain business logic and judgment logic. It is often implemented by a specific class in Java.

Abstract product role: It is generally the parent class or implementation of the specific product inherited. Implemented by an interface or abstract class in Java.

Specific product role: Objects created by factory classes are examples of this role. Implement a specific class in Java.

Classify the relationship between them with classes (if you don't know much about class diam, please refer to my article about class diagrams):

So how is the simple factory model? I will give an example, I think this is more likely to understand this than a large paragraph theory! Let's give the outbreak charter: P After using the simple factory model, now the outbreak only needs to sit in the car to say the sentence: "Drive". Take a look at how to achieve: // Abstract product role • public interface car {• public void driving (); •} • // Specific product role public class benz imports car {• public void DRIVE () {• System.out. Println ("Driving Benz"); •} •}

Public Class BMW IMPLEMENTS CAR {

Public void drive () {

System.out.println ("driving bmw");

}

}. . . (Audi, I don't write: p) // factory role

Public Class Driver {• // Factory Method / Note Return Type As Abstract Product Role

Public Static Car Drivercar (String S) THROWS EXCEPTION {

• // Judgment logic, return to specific product roles to client

IF (S.Equalsignorecase ("Benz")))

Return new benz ();

ELSE IF (S.Equalsignorecase ("BMW"))

Return New BMW ();

...... else throw new exception ();

. . .

// Welcome the outstower to play ...

Public class Magnate {

Public static void main (String [] args) {

Try {// Tell the driver, I am sitting today.

Car car = driver.drivercar ("benz"); // Next command: driving

Car.Drive ();

. . .

The other information of this procedure is filld after being completely fill it. If you put all the classes in a file, please don't forget that there can be only one class is declared as public. This program runs in JDK1.4. The relationship between the various classes in the program is as follows:

This is a simple factory model. How is it, very simple? So what is it benefited? First, after using simple factory mode, our procedures are not in "disease", more in line with realities; and clients are only responsible for direct product objects, but only responsible for "consumption" products (just like outbreak households) ). Below we analyze the simple factory model from the principle of opening and closing. When the outbreak has increased a car, as long as the abstract product is developed, it can be used by the customer as long as the factory class knows. So for the product part, it is in line with the principle of opening and closing - open to the extension, close to the modification; but the factory part seems to be less than ideal, because each additional car is added to increase the corresponding business logic in the factory class. And judgment logic, this is naturally a violation of the principle of opening and closing. For such factory classes (in our example, we call it all-round or God class. Our example is the simplest situation, and in practical applications, it is likely that the product is a multi-level tree structure. Since there is only one factory class in simple factory model to correspond to these products, this may take our God's class, and then tired our lovely programmers: (as the simple factory model mentioned earlier is suitable for business Simply put. And for complex business environments may not be adapted. This should be played by the factory method model !! Fourth, factory method model first look at its composition: Abstract factory role: This is factory The core of the method mode is independent of the application. It is an interface that must be implemented or must inherit. It is implemented by an abstract class or interface in Java. Specific factory role: It contains and specific business logic Code. Apply object to the application to create objects of the corresponding specific product. It is implemented by the specific class in Java. Abstract product role: It is the parent class or an implementation of the specific product inherited. In Java, there is generally abstraction Class or interface is implemented. Specific product role: The object created by the specific factory role is an instance of this role. Implementing the specific class in Java. To use the class diagram to clearly represent the relationship between them: us Or the old rule uses a complete example to see how it is coordinated between the various characters of the factory model. If the outbreak households do more, the more and more car cars are also more and more. This is the driver master , I have to remember, maintenance, I have to use him! So I have sympathy to say: See you with me so many years, you don't have to have this hard, I will assign you a few people. You only manage them! So, the management of factory method models has appeared. The code is as follows:

// Abstract product role, specific product role is similar to simple factory model, just become more complicated, here is slightly // Abstract Factory role • public interface Driver {• public Car driverCar (); •} • public class BenzDriver implements Driver {• public Car driverCar () {• return new Benz (); •} •} • public class BmwDriver implements Driver {• Public Car Drivercar () {

Return New BMW ();

}

}

... // should form a correspondence with the specific product, here ...

// Have a promise of the outbreak

Public Class Magnate

{

Public static void main (string [] args)

{

Try {

Driver driver = new benzdriver ();

Car car = driver.drivercar ();

Car.Drive ();

} catch (Exception E)

{

}

}

}

The factory method uses an abstract factory role as the core instead of using the specific class in simple factory model as the core. Let's take a look at the factory method model to bring us? Use the opening and closing principle to analyze the factory method mode. When there is a new product (i.e., the car) is generated, as long as the abstract product role, the abstract factory role is generated, then it can be used by the customer, without having to modify any existing code. It seems that the factory method model is fully in line with the opening and closing principle!

Using factory method model is enough to cope with most of the business needs we may encounter. However, when the product type is very much, there will be a lot of factory classes, which should not be what we hope. So I suggest that in this case, the simple factory model is used to reduce factory categories. That is, the similar species on the product tree (generally the leaves of the tree) use simple factory models. achieve.

Of course, special situations are specially treated: There are different product trees in the system, and there is a product family on the product tree (the next section will explain this noun). So in this case

It may be possible to use the abstract factory mode.

Five, small knot

Let's take a look at the simple factory model, the factory method model gives us enlightenment:

If we do not use the factory model to implement our example, maybe the code will decrease much - only need to achieve existing cars, do not use polymorphism. However, in maintenanceability, scalability is very poor (you can imagine, the class to be touched after adding a car). Therefore, in order to improve scalability and maintenance, it is worthwhile to write some code.

In the typical example of our factory model, a phenomenon will be found: the factory model is not used alone, but is accompanied by other modes of singles. So learning to understand the fact that the factory model has a great help to learn other models. If you look at the factory mode, we can do not see it as a model, but a good programming style.

Sixth, abstract factory model

First, let's know what product family: a family consisting of functionally associated products in different product grade structures. If you look at this sentence, you can understand this concept. I have to admire you. Or let us use an example to explain it.

BMWCAR and BENZCAR in the figure are two product trees (product hierarchy); Benzsportscar and BMWSportscar as shown are a product family. They can all be placed in the sports car family, so the function is associated. Similarly BMWBussinessCar and Benzsportscar are also a product family.

Back to the topic of abstract product mode.

It can be said that the difference between it and the factory method model is that the complexity of the need to create an object. And the abstract factory model is the most abstract and most general.

The abstract factory model is integrated: gives a client to create product objects in multiple product families.

And use abstract factory models to meet the conditions:

1. There are multiple product families in the system, and the system only may consume one of the products.

2. Products that belong to the same product are used.

Let's take a look at the various roles of the abstract factory model (the same as the factory method):

Abstract factory role: This is the core of factory method model, which is not related to the application. It is the interface that must be implemented or the parent class that must be inherited. It is implemented by an abstract class or interface in Java.

Specific factory role: it contains code related to specific business logic. The object is used to create the object of the corresponding specific product. It is implemented by the specific class in Java. Abstract product role: It is a parent class that is inherited by the specific product or an implementation interface. There are generally abstract classes or interfaces in Java.

Specific product role: The object created by the specific factory role is an instance of this role. Implemented by specific classes in Java.

According to the practice, come back to a picture:

I have seen the first two models, and the coordination between the various characters of this model should have a number in my heart, I will not give the specific example. Just pay attention to meet the conditions to use abstract factory models, otherwise even if there are multiple product trees, there are also product families, but cannot be used.

Seven, summary

This is a consolidation of my factory model, and puts forward some of my own understanding and ideas. After all, this person is shallow, it is inevitable to understand the wrong and superficial place. I hope that the high people can correct, and hope to help you.

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

New Post(0)