Design Pattern -- Abstract Factory Pattern [Abstract Factory Pattern]
Statement: This article is not original because it is compiled from online books.
When talking about the last factory method model, we talked about how to create women and people. People are made, and the world is busy. However, looking down, we all look clear, and lack of caring, hate, joys, sorrows, and sorrows, the life of human beings is too dull. nvwa suddenly shot his head and said, "Shit! What should I do if I forget to define gender for humans? Then, we will shuffles humans and prepare to create humans again.
Since the previous work has spent a lot of energy as a foreshadowing, and I don't want to start from the beginning, let's first talk about how to change the human (Product class, add a gender to each human, and then create a new one. What should we do if this problem is solved? There is only one, or both production and production are male, or even female. If yes, you can split a production line-the Concrete Factory in the Factory model, as a result, nvwa used the "Gossip copy technique" to change the original gossip furnace to two, and slightly changed it into a female gossip furnace (only produce female, a specific factory implementation class) and the male eight-way furnace (only male production, and a specific factory implementation class), the process class diagram is as follows:
First look at the class diagram of humans (that is, products:
Vc + 6w8DtveKjrMq1z9bA4MLvysfV5sq1tcSy + sa3o6zSu7j2vdCy + Signature = "here write picture description" src = "http://www.bkjia.com/uploads/allimg/151030/043K2IG-1.png" title = "\"/>
Here, the abstract factory only implements a createHuman method, which aims to simplify the workload of implementing the class code. This will be said when talking about the code. A New Enum type of Jdk 1.5 is also used here. In fact, this type can be fully implemented using static variables of the class, but I think it should be learned from learning, even if you are familiar with this mode, you may not have used the Enum type. It is a different knowledge point.
Let's look at the program implementation:
public interface Ihuman { public abstract void laugh(); public abstract void cry(); public abstract void talk(); public abstract void sex();}
Define the human interface, and then create three abstract classes based on the interface, that is, three product levels, to implement the laugh (), cry (), and talk () methods, take YellowHuman as an example:
Public abstract class YellowHuman implements Ihuman {public void laugh () {System. out. println (yellow humans will laugh, happy !); } Public void cry () {System. out. println (yellow humans will cry);} public void talk () {System. out. println (yellow humans speak, generally speaking, dubyte );}}
The other two abstract classes, WhiteHuman and BlackHuman, do not copy the code.
In fact, what is the significance of the abstract class here? That is, you are not allowed to create an abstract object. You can use a non-abstract class to completely replace it, however, since we have entered the Java door, we must abide by the Java rule. We can see the implementation class:
Implementation of male and yellow race:
Public class MaleYellowHuman extends YellowHuman {public void sex () {System. out. println (the gender of the yellow race is male ....);}}
Implementation of female pornography:
Public class FemaleYellowHuman extends YellowHuman {public void sex () {System. out. println (the gender of this yellow race is female ....);}}
The four other implementation methods that are similar to this class do not copy the code.
In the abstract factory model, the product levels and product families have been completed, that is, all the human beings and the human beings that have been generated have been defined, and the next step is to wait for the factory to be created, let's look at the factory class.
Before looking at the factory class, let's first look at the enumeration type. This is very interesting:
Public class HumanEnum {// defines all types in the world MaleYelloHuman (com. cbf4life. yellowHuman. maleYelloHuman), MaleWhiteHuman (com. cbf4life. whiteHuman. maleWhiteHuman), MaleBlackHuman (com. cbf4life. blackHuman. maleBlackHuman); FemaleYelloHuman (com. cbf4life. yellowHuman. femaleYelloHuman), FemaleWhiteHuman (com. cbf4life. whiteHuman. femaleWhiteHuman), FemaleBlackHuman (com. cbf4life. blackHuman. femaleBlackHuman), private String value =; // define the constructor to match private HumanEnum (String value) {this. value = value;} public String getValue () {return this. value ;}}
When the Enum type is passed as a parameter to a method, when you perform a unit test in Junit, you do not need to judge whether the input parameter is null and the length is 0, if the parameters passed in by the method are not of the Enum type, they will not be passed in. You can define a class and define a bunch of static variables. This is fine. This is not the case, the above Code explains that the constructor has nothing to say, and then the getValue () method is used to obtain the value of an element in the enumeration type. The elements in the enumeration type also have names and values, this is a bit similar to HashMap.
Then, let's look at our factory class, first look at the interface:
public interface IHumanFactory { public abstract IHuman createYellowHuman(); public abstract IHuman createWhiteHuman(); public abstract IHuman createBlackHuman();}
Then look at the abstract class:
Public class AbstractHumanFactory implements IHumanFactory {protected IHuman createHuman (HumanEnum humanEnum) {Human human Human = null; // if the Element passed in is not a specific Element in an Enum, if (! HumanEnum. getValue (). equals () {try {// directly generate an instance human = (Human) Class. forName (humanEnum. getValue ()). newInstance ();} catch (Exception e) {// This Exception will not occur because enum is used, unless your enum has a problem; e. printStackTrace () ;}} return human ;}}
See no. This is the benefit of introducing enum. The createHuman (HumanEnum humanEnum) method defines that the input parameter must be of the HumanEnum type and then uses humanEnum directly. the getValue () method can obtain the specific passed value. This is not much to say. It is not difficult for everyone to understand the program by themselves, the purpose of this abstract class is to reduce the amount of code for the following implementation classes. Let's look at the implementation classes:
Male factory, only create male:
public class MaleHumanFactory extends AbstractHumanFactory { public IHuman createYellowHuman() { return super.createHuman(HumanEnum.MaleYellowHuman); } public IHuman createWhiteHuman() { return super.createHuman(HumanEnum.MaleWhiteHuman); } public IHuman createBlackHuman() { return super.createHuman(HumanEnum.MaleBlackHuman); }}
Female factory, only create female:
public class FemaleHumanFactory extends AbstractHumanFactory { public IHuman createYellowHuman() { return super.createHuman(HumanEnum.FemaleYellowHuman); } public IHuman createWhiteHuman() { return super.createHuman(HumanEnum.FemaleWhiteHuman); } public IHuman createBlackHuman() { return super.createHuman(HumanEnum.FemaleBlackHuman); }}
The product has been defined, and the factory has been defined. If everything is ready and ready for use, let's start building the product. Oh, no, nvwa is starting to build people:
Public class NvWa {public void main (Object string [] args) {// The first production line, malanfactory maleHumanFactory = new MaleHumanFactory (); // The second production line, female Production Line HumanFactory femaleHumanFactory = new FemaleHumanFactory (); // after the production line is established, the producer is Human maleYellowHuman = maleHumanFactory. createYellowHuman (); Human femaleYellowHuman = femaleHumanFactory. createYellowHuman (); maleYellowHuman. cry (); maleYellowHuman. laugh (); femaleYellowHuman. sex ();/***..... * You can continue later */}}
Two octal furnaces, a female, a male, and full horsepower have been built into this world until now.
After talking about the abstract factory model, let's think about some questions: What are the advantages and disadvantages of the factory model? Let's talk about advantages first. I generally look at advantages first. What is very important is that the factory model conforms to the OCP principle, that is, the open and closed principle. How can we say this, for example, on gender issues, there are still dual sex people in this world. They are both male and female people. That is to say, we need to add a category of products to our product family and add a factory to solve this problem, I don't need to implement it again. It's very easy for everyone to draw their own class diagrams and then implement them.
Is there any other benefit? The abstract factory model also has a very large, high cohesion and low coupling. In a large project team, products are developed by a group of people, however, only the Factory method and Product Interface are provided for access by other members. That is to say, you only need to provide the Product Interface and Concrete Factory to generate the desired objects and methods, java's high cohesion and low coupling features are all at a glance, haha.