The factory model is quite tangled when I look at English books. I feel like I'm confused in head first, and I can't even read it anymore. When I look at other people's summary in Chinese, a main line will come out right away.
There are three main factory models:
1) simple factory model (static factory model) 2) Factory method model 3) abstaract factory Model
The simple factory mode is also known as the static factory mode. It is easy to hear the name. It mainly has three roles, specific factory, abstract product interface, and specific product interface. Create a product task by submitting it to a specific factory.
// Abstract Product role public interface car {public void drive ();} // specific product role 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") ;}// factory class role public class driver {// factory method. note that 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 ();}}
The above code is copied elsewhere. Car is an abstract product interface, and Benz and BMW are the implementations of specific products. Driver is a specific factory. We can see that the actual factory does not comply with the principle of opening/closing.
====
Factory method factory method mode
A simple factory is responsible for all specific product creation tasks. It's really a good guy. It's also called the all-around factory, the god factory, and everything. The evolution to the factory method model is different, with the role abstraction factory and the specific factory added. The abstract factory defines an interface and implements the Abstract Factory method in a specific factory area. The client can call a specific class. When a new factory is added, it only needs to implement the abstract interface. You can use the client to call this interface.
The factory model has four roles: Abstract Factory, specific factory, abstract product, and specific product.
3.5 example of factory method mode: // abstract product public abstract class pencore {string color; public abstract void writeword (string S );} // specific product public class redpencore extends pencore {redpencore () {color = "red";} public void writeword (string s) {system. out. println ("write" + color + "word" + S) ;}// specific product public class bluepencore extends pencore {bluepencore () {color = "blue ";} public void writeword (string s) {system. out. println ("write" + color + "word" + S) ;}// specific product public class blackpencore extends pencore {blackpencore () {color = "black ";} public void writeword (string s) {system. out. println ("write" + color + "word" + S) ;}// factory public abstract class ballpen {ballpen () {system. out. println ("produce a container" + getpencore (). color + "pen-core ballpoint pen");} public abstract pencore getpencore ();} // factory-specific public class redballpen extends ballpen {public pencore getpencore () {return New redpencore () ;}/// factory-specific public class blueballpen extends ballpen {public pencore getpencore () {return New bluepencore ();}} // specific factory public class blackballpen extends ballpen {public pencore getpencore () {return New blackpencore ();}}
======
3. Abstract Factory Model)
Abstract Factory mode is the evolutionary version of the factory method mode. There is a concept called a product family. That is, what you produce is a species. As long as the species are defined in reality, it depends on the actual situation. You can attribute cats and dogs to a species, because they are all crawling animals, you can also classify cats and tigers as cats. The factory method mode creates only one product family, while the abstract factory mode creates multiple product families. The abstract factory method provides multiple product creation methods. The specific class needs to implement these multiple interfaces. Let's see the code.
Interface iproduct1 {public void show ();} interface iproduct2 {public void show ();} class product1 implements iproduct1 {public void show () {system. out. println ("this is a type 1 product");} class product2 implements iproduct2 {public void show () {system. out. println ("this is type 2 Product") ;}} interface ifacloud {public iproduct1 createproduct1 (); Public iproduct2 createproduct2 ();} class factory implements ifacloud {public iproduct1 createproduct1 () {return New product1 ();} public iproduct2 createproduct2 () {return New product2 () ;}} public class client {public static void main (string [] ARGs) {ifactory factory = new factory (); factory. createproduct1 (). show (); factory. createproduct2 (). show ();}}
Ifacloud has two interfaces: createproduct1 and createproduct2. The factory has two methods for creating products. Factory implements the ifacloud interface, which is a specific factory.
The abstract factory mode provides interfaces for creating multiple products.
Next, let's compare the factory method mode and the abstract factory mode.
Differences between Abstract Factory mode and factory method mode
Abstract Factory mode is an upgraded version of the factory method mode. It creates a group of related or mutually dependent objects. The difference between the factory method model and the factory method model is that the factory method model is for a product level structure, while the abstract factory model is for multiple product level structures. In programming, a product structure is usually represented as an interface or abstract class. That is to say, all products provided by the factory method mode are derived from the same interface or abstract class, the abstract factory model provides products derived from different interfaces or abstract classes. (Copy by Internet ).
There is a question: why does the abstract factory model come into being? Can the two factory models be handled? There must be a reason for this. Let's think about it.
Abstract Factory mode advantages Abstract Factory mode in addition to the advantages of factory method mode, the main advantage is that the product family can be constrained within the class. The so-called product family generally has a certain degree of Association. The abstract factory model can define and describe the association between product families within the class, instead of introducing a new class for management. Disadvantages of the abstract factory model: Product Family expansion will be very laborious. If a new product needs to be added to the product family, almost all factory classes need to be modified. Therefore, the classification of product hierarchy is very important when abstract factory models are used. Applicable scenarios when the object to be created is a series of products that are correlated or dependent on each other, the abstract factory mode can be used. A more clear point is that in an inheritance system, if there are multiple levels (that is, there are multiple abstract classes ), the abstract factory mode can be used if there are certain associations or constraints between implementation classes in each hierarchy. If there is no association or constraint between implementation classes in each hierarchy, it is more appropriate to create products using multiple independent factories. To sum up, both the simple factory model, the factory method model, and the abstract factory model belong to the factory model, which is extremely similar in terms of form and characteristics. Their ultimate goal is to decouple them. In use, we don't have to worry about whether this mode is factory method mode or abstract factory mode, because the evolution between them is often unpredictable. You will often find that the factory method mode is clearly used. When a new requirement comes and a slight modification is made, after a new method is added, because the products in the class constitute the product family with different levels of structure, it becomes the abstract factory model. For the abstract factory model, when a method is reduced to make the provided product no longer constitute a product family, it becomes the factory method model.
From the above summary, although abstract factories are multiple products, there are some associations between these products. However, when there are no associations between several products, we can consider several factory methods.
But how are these products associated? Is it code Association? Or, in the real world, it seems to be in the same category.
(Write it here today)