My understanding: replacing a model with a factory method for new operations.  The factory method encapsulates the new methods of several associated classes, and does not require new multiple times for each instantiation of these classes, only the corresponding methods of the factory class are called to instantiate the classes.
Combine simple examples and UML diagrams to explain the simple principle of Factory mode.
  first, introduction  saying ten years ago, there was an outbreak, His family had three cars (Benz, BMW), Audi (Audi), who seemed more patriotic, had no Japanese car, and hired a driver to drive for him. However, the outbreak of the car is always the case: on the Benz and the driver said, "Drive a Mercedes!" "When he was in BMW, he said," Drive a BMW! "When he was in Audi, he said," Open the Audi car! ”。 
 You must say: this man is sick! Why don't you just say drive?! 
 And when we put this outbreak into our programming language, we find that C has always been the way to ride! 
 Fortunately, this kind of illness can be avoided in the OO language. The following is the Java language based on the introduction of the topic of our article: Factory mode!! 
  two, Introduction  Factory mode is primarily to provide an interface for creating objects. The factory model is divided into three categories according to the reference in Java and mode: 
 1. Simple Factory 
 2. Factory method Mode (Factory) 
 3. Abstract Factory 
 These three modes are progressively abstracted from top to bottom and more general. The 
 also has a classification method, which is to see the simple factory model as a special case of the factory method pattern, two of which belong to one class. Both, this is the use of the Java and Pattern classification method. 
 Under what circumstances should we remember to use Factory mode? There are roughly two points: 
 1. You cannot foresee what kind of instance you need to create when coding. 
 2. The system should not rely on the details of how product class instances are created, combined, and expressed 
 what are the benefits of the factory model to our Ood and oop? 
 When we're done, maybe you'll know.  
  three, simple Factory mode  as the name implies, the pattern itself is very simple, and use it in a simpler business situation. 
 It consists of three roles: 
  factory class role : This is the core of this model, contains certain business logic and judgment logic, according to the logic does not pass, produces the concrete factory product. such as the driver class in the example. 
  Abstract Product role : It is generally the parent class of a specific product inheritance or an implemented interface. Implemented in Java by an interface or an abstract class. such as the car interface in the example. 
  Specific product role : The object created by the factory class is an instance of this role. Implemented in Java by a specific class, such as the Benz, BMW class in the example. 
 to use a class diagram to clearly represent the relationship between them:  
So how does the simple factory model work? Let me give you an example, I think this is much easier to understand than to say a large part of the theoretical word description! Here's a cure for the upstart: P
After using the simple factory model, now the upstart just need to sit in the car and say to the driver: "Drive" on it. To see how this is achieved:
Abstract Product Roles
public interface car{
public void Drive ();
}
Specific product roles
public class Benz implements car{
public void Drive () {
System.out.println (Driving Benz);
}
}
public class BMW implements car{
public void Drive () {
System.out.println (Driving BMW);
}
public class driver{
Factory method
Note The return type is abstract product role
public static Car Drivercar (String s) throws Exception {
Judgment logic, return the specific product role to the client
if (S.equalsignorecase (Benz))
return new Benz ();
else if (S.equalsignorecase (BMW))
return new BMW ();
......
else throw new Exception ();
。。。
Welcome to the upstart ...
public class magnate{
public static void Main (string[] args) {
try{
Tell the driver I'm riding today.
Car car = Driver.drivercar (Benz);
Next command: Drive
Car.drive ();
。。。
Fill out any additional information that is vacant in this program and run it. If you put all the classes in a file, don't forget that only one of the classes is declared public. This program runs through the jdk1.4.
The relationships of each class in the program are expressed as follows:
}
。。。 (Audi I will not write: P)
//factory class role 
 This is the simple Factory mode. How's that, easy? So what benefits does it bring? 
 First, after using the simple Factory mode, our program is not "sick", more in line with the reality of the situation, and the client is exempt from the direct creation of product object responsibility, but only responsible for "consumption" products (as the nouveau riche). 
 Below we analyze the Simple Factory mode from the opening and closing principle. When the upstart adds a car, as long as the contract is in line with the abstract product, then it can be used by the customer if the factory is informed. Then for the part of the product, it is in line with the opening and shutting principle-open to the expansion, the modification closed; but the factory part seems to be not ideal, because each additional vehicle, must add the corresponding business logic and the judgment logic in the factory class, this obviously violates the opening and closing principle. 
 for such a factory class (in our case, for the driver), we call it the Almighty or the God class. 
 Our example is the simplest case, and in practical applications it is possible that the product is a multi-layered tree structure. Since there is only one factory class in the simple factory model that corresponds to these products, this may break our God class and then spoil our lovely programmer: (
 As I mentioned earlier, the simple factory model applies to business will be simple.) The complex business environment may not be very adaptable. This should be done by the factory method mode!! 
  four, factory method mode  first look at its composition: 
  Abstract Factory Role : This is the core of the factory method pattern, which is independent of the application. is the interface that the specific factory role must implement or the parent class that must inherit. In Java it is implemented by an abstract class or interface. 
  Specific factory role : It contains code that is relevant to the specific business logic. Called by the application to create an object that corresponds to a specific product. In Java it is implemented by a specific class. 
  Abstract Product role : It is the parent class of a specific product inheritance or an interface that is implemented. In Java, there are generally abstract classes or interfaces to implement. 
  Specific product role : The object created by the specific factory role is an instance of this role. Implemented in Java by a specific class. 
 to use a class diagram to clearly represent the relationship between them:  
 
 We still use a complete example to see how the various roles in the factory model are coordinated. In the words of the upstart business more and more, their car is more and more. This can be bitter the driver master, what car it must remember, maintenance, all must go through him to use! So the nouveau riche sympathy he said: "Look at you and me for so many years, you do not have to work so hard, I assign you a few staff, you just keep them in mind!" As a result, the management of the factory method pattern arose. The code is as follows: the 
//abstract product role, the specific product role is similar to the simple factory model, just become more complex, here 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 correspond to specific products, here slightly ... 
//Invite Mr. Upstart 
 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 a specific class as the core in the simple factory pattern. Let's take a look at what the factory approach model brings to us. Use the open and close principle to analyze the plant method model. When a new product (ie, the upstart car) is produced, it can be used by the customer instead of modifying any existing code as long as it is generated according to the contract provided by the abstract product role and the abstract factory role. It seems that the factory method mode is completely in line with the open and closed principle! The 
 Use factory method mode is sufficient to cope with most of the business requirements that we may encounter. But when the product variety is very long, there will be a lot of corresponding factory class, this should not be what we want. So I suggest that in this case, the simple factory model combined with the factory method pattern should be used to reduce the factory class: The Simple factory pattern is used for similar species on the product tree (which is usually brothers in the leaves of the tree). 
 Of course, special treatment is special: There are different product trees in the system, and there are product families on the product tree (this noun will be explained in the next section). In this case, it is possible to use the abstract factory pattern. 
v. Summary let us take a look at the simple factory model, factory method model to our Enlightenment:
If we do not use Factory mode to implement our example, perhaps the code will be much less-just to implement the existing car, do not use polymorphism. But on maintainability, scalability is very poor (you can imagine adding a car to affect the class). So in order to improve scalability and maintainability, it is worthwhile to write more code.
Six, the abstract factory model first to understand what is the product family: in different product hierarchy structure, the function of the associated product family. I have to admire you if you can clearly understand this concept by looking at this sentence. Let's use an example to illustrate it in a figurative way.
 goes back to the topic of abstract product patterns. The 
 can say so, the difference between it and the factory method pattern is the complexity of the object that needs to be created. and the abstract factory model is the most abstract and most general of the three. The 
 Abstract Factory pattern is intended to provide an interface to clients that can create product objects in multiple product families. The 
 also uses the abstract Factory mode to satisfy the condition: 
 1. There are multiple product families in the system, and the system may only consume one of the products at a time 
 2. Products belonging to the same product family are used. 
 to look at the various roles of the abstract Factory pattern (as in the Factory method): 
  Abstract Factory role : This is the core of the factory method pattern, which is not application-agnostic. is the interface that the specific factory role must implement or the parent class that must inherit. In Java it is implemented by an abstract class or interface. 
  Specific factory role : It contains code that is relevant to the specific business logic. Called by the application to create an object that corresponds to a specific product. In Java it is implemented by a specific class. 
  Abstract Product role : It is the parent class of a specific product inheritance or an interface that is implemented. In Java, there are generally abstract classes or interfaces to implement. 
  Specific product role : The object created by the specific factory role is an instance of this role. Implemented in Java by a specific class. 
 By convention, take another figure:  
I have seen the first two patterns, the coordination between the various roles of this pattern should have a number of hearts, I will not cite specific examples. Just be sure to meet the criteria for using the abstract Factory mode Oh, or even if there are multiple product trees, there are product families, but not used.
The principle of Factory mode and its simple application