Design Model-factory Model
Start with reality
Remember what I mentioned earlierSimple factory Mode?
It still needs to be used in the client (main ).AnimalFactory.getAnimale(0)
To select the animal type, when the new animal type is addedAnimalFactory
To modify the code, which is equivalentBackgroundModify
.
Now we need to solve the problem by removing the switch statement in the Factory and any judgment logic, and simply using inheritance or polymorphism on the client to achieve animal selection.
First, define the basic nature of an animal (EAT, call, run, and sleep ). Interface Animal {public void eat (); public void shout (); public void run (); public void sleep ();}
Then achieve species diversification
class Dog implements Animal{ @Override public void eat() { System.out.println("dog eat"); } @Override public void shout() { System.out.println("dog shout"); } @Override public void run() { System.out.println("dog run"); } @Override public void sleep() { System.out.println("dog sleep"); }}class Cat implements Animal{ @Override public void eat() { System.out.println("cat eat"); } @Override public void shout() { System.out.println("cat shout"); } @Override public void run() { System.out.println("cat run"); } @Override public void sleep() { System.out.println("cat sleep"); }}
So far, there is no difference with the simple factory model. Focus:
Virtualize factories so that specific factories can produce specific animals, and all specific factories should have abstract factory features (attributes or methods ). The abstract factory is as follows:
interface Factory{ public Animal getAnimal();}
For the above animals, a corresponding animal factory is required:
class DogFactory implements Factory{ @Override public Animal getAnimal() { return new Dog(); }}class CatFactory implements Factory{ @Override public Animal getAnimal() { return new Cat(); }}
In the end, the client implements the following:
public static void main(String[] args){ Factory dogFac = new DogFactory(); Animal dog = dogFac.getAnimal(); Factory catFac = new CatFactory(); Animal cat = catFac.getAnimal(); }
Rather than in the simple factory method:
public static void main(String[] args){ List zoo = new ArrayList(); for(int i=0;i<4;i++) zoo.add(AnimalFactory.getAnimale(0)); for(int i=0;i<2;i++) zoo.add(AnimalFactory.getAnimale(1)); for(int i=0;i<4;i++) zoo.add(AnimalFactory.getAnimale(2)); for(int i=0;i<2;i++) zoo.add(AnimalFactory.getAnimale(3)); for(Animale animale:zoo){ //animale.... } }
The advantage of doing so is that it fully reflects interface-oriented programming rather than implementation-oriented programming. This interface is equivalent to inserting a specific implementation into this interface. Even if something is wrong (DogFactory or CatFactory
), Replace it directly, or modify the corresponding implementation class. If you need to add new species (goat, hen, etc ), you only need to insert GoatFactory or ChickenFactory on the Factory interface.
In fact, according to this idea,Animal
The implementation of this interface can also be understood as inserting various specific animals on the Animal interface.
For example, on the computer's motherboard, there are various plug-ins, memory, hard disk, cpu, and so on. These are interfaces. interfaces are implemented accordingly, for example, memory stick, hard disk, and cpu of different specifications. When these implementations are broken, you can directly pull them down and change the plug-in, which is equivalent to the above example.Factory
InterfaceDogFactory
If there is a problem, you can directly modify the DogFactory instead of the CatFactory and the main method of the motherboard.
Imagine if the computer motherboard is not a plug-in, but directly welded to the original, then when any bad, all need to repair the motherboard, then this loss will be huge. In other words, in the simple factory mode, modifying any animal (add or delete) will makeFactory
Class, the logic in the client main may also need to be modified.
To sum up, the factory model does reflect the idea of interface-oriented programming. It also makes better use of polymorphism and effectively decouples classes. It is a classic design model.