In-depth plant mode

xiaoxiao2021-03-06  50

First, the inerture said ten years ago, there is an outbreak, and his family has three cars - Benz Mercedes, BMW BMW, Audi Audi, and hired drivers to drive him. However, the outbreak is always whimsted when the Benz car is said to "open Mercedes-Bena!" You must say: This person has disease! Say it directly not to drive? ! When the behavior of this outbreak is placed in our programming, it will find that this is a common phenomenon. Fortunately, this disease can be avoided in OO (object-oriented) language. Below, we will introduce our topics in Java language: factory model. Second, the classification plant mode is mainly to provide a transition interface for creating objects to block the specific process of creating objects, and achieve the purpose of improving flexibility.

Factory model is divided into three categories in "Java and Mode": 1) Simple Factory

2) Factory Method (Factory Method)

3) Abstract Factory mode (Abstract Factory) These three modes are gradually abstract from top to bottom, and more general. GOF divides factory models into two categories: Factory Method, and Abstract Factory. The Simple Factory is a special case of the factory method mode, both of which are classified.

Both can be used in this article using "Java and Mode". Let's take a look at how these plant models come to "treatment".

Third, simple factory model

Simple factory model is also known as static factory method mode. It can be seen that this mode must be seen in renaming. Its existence is simple: Define an interface for creating an object. Let's take a look at its composition:

1) 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.

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

3) Specific product role: The object created by the factory class is an instance 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 to use simple factory models? We will reform the outbreak house in the simple factory model - now the outbreak only needs to sit in the car to say the sentence: "Drive".

// Abstract product role

Public interface car {

Public void drive ();

// 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 that the type is an abstract product role public static car drivercar (string s) throughs exception {// judgment logic, return to the specific product role 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 on Mercedes-Benz Car Car = Driver.Drivercar today; // Under the 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, is it easy to use? 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 are analyzing from the principle of opening and closing (opening up; imposing the modified closure). 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 in the product part, it is in line with the opening and closing principle; but the factory part seems to be less than ideal, because each additional car is added to the factory class, it is obviously in violation of it. Closed principle. It is conceivable to know that the new product is added, the factory class is very passive. 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 to be tired, and tired of our programmers: (So factory method model as the savior.

Fourth, factory method mode

The factory method model removes the static properties of the traditional Chinese work plant method so that it can be inherited by subclasses. This focuses on the pressure in the factory method in the simple factory mode, can be shared by different factory subcams in the factory method model.

You should generally guess the structure of the factory method model, come and see its composition:

1) 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.

2) Specific factories role: it contains code related to specific business logic. The object is used to create the object of the corresponding specific product.

3) Abstract product role: It is the parent class or an implementation of the specific product inherited. There are generally abstract classes or interfaces in Java.

4) Specific product role: The object created by the specific factory role is an example of this role. Implemented by specific classes in Java. Use the class diagrams to clear the relationship between them:

Factory method mode uses multiple subclasses inherited from abstract factory roles to replace "God" in simple factory models. As mentioned above, this is shared on the pressure of the object; and this makes the structure flexible - when there is a new product (ie the outbreak car), as long as the abstract product role, the abstract factory character is provided. The contract is generated, then it can be used by customers without having to modify any existing code. It can be seen that the structure of the factory role is also in line with the principle of opening and closing!

We are still old rules, using a complete example to see how the factory models are coordinated. Saying that the outbreak family is more, and the car is getting more and more. This can suffer the driver master, what can't remember, maintenance, you have to use him! So the outbreak is sympathizing him: See you with me for so many years, you don't have to have such a hard work, I will assign you a few people, you only manage them! Thus, the management of factory method patterns appeared. code show as below:

// 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 ... // Have a promise of Mr. Public Class Magnate

{

Public static void main (string [] args)

{

Try {driver DRIVER = New Benzdriver ();

Car car = driver.drivercar ();

Car.Drive ();

}

......

}

It can be seen that the addition of the factory method is to increase the number of objects. When the product type is very much, there will be a large number of factory objects, which is not what we hope. Because if this is not avoided, you can consider using simple factory models to reduce factory categories: that is, similar species on the product tree (generally the leaves of the tree) using simple factories Mode is implemented.

Five, small knot

The factory method model seems to have a perfect way to create the object of the object, so that only the interface provided by the customer program only. Then we must be in the factory in the code? It is not necessary. Maybe in the following cases you can consider using factory method patterns:

1) When the client does not need to know the creation process of the object to use.

2) The objects used by the client program have a variable possible, or I don't know which specific object used.

Simple factory model and factory method model really avoid code changes? No. In simple factory mode, the addition of new products to modify the judgment statement in the factory role; in the factory method mode, the judgment logic is left in the abstract factory role, or write the specific plant role in the client program ( Like the example above). Moreover, changes in product object creation conditions will inevitably cause a modification of factory roles.

In this case, Java's reflex mechanism breaks through the smart combination of the profile - this is perfect in Spring. Sixth, abstract factory model

First, let's know what product family: a family consisting of functionally associated products in different product grade structures. 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 factory model.

It can be said that the difference between the abstract factory model 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 intention of abstract factory model is: provide an interface to the client, you can 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 can only consume one of the products at a time.

2) Products belonging to the same product family with its use.

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

1) 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.

2) Specific factories 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.

3) Abstract product role: It is the parent class or an implementation of the specific product inherited. There are generally abstract classes or interfaces in Java.

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

The class diagram is as follows:

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 be sure to meet the conditions that use abstract factory models.

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

New Post(0)