Differences between simple factory mode and factory method mode
Comparing the two structure charts, we will find that there is a big difference in the red border on the right of each structure chart. The simple factory of the former has no child class, and there is only some logic judgment code; while the latter resends four specific factories. Here, one of the differences between the two modes comes out. So where is the logic judgment code in the factory method mode? The answer is: client. In the previous mode, to modify the Add function, you must modify it in the factory class, and the latter is to modify the client.
Contact:
There are operation classes and clients, and the operation classes all have corresponding subclasses.
Differences:
Most obvious: the factory method mode requires a factory interface.
Second, the factory method model refers to the specific factory rather than a simple factory.
Finally, when adding a function, the former changes the factory class (adding a subclass of the related function for the factory class), and the latter changes the client (adding the judgment logic code for the corresponding function ).
The example (calculator) is described as follows:
(1) There are operation classes (including addition, subtraction, multiplication, division, and division of four sub-classes) and a client is required.
(2) The factory class in the factory method mode generates four specific factories corresponding to the subclass of the operation class: Addition factory, subtraction factory, multiplication factory and division factory.
(3) build a factory interface in the code, and then implement the interface in four specific factories.
Code Description
//Animal .javapublic abstract class Animal { public abstract void sleep();}
//AnimalFactory .javapublic interface AnimalFactory { public Animal createAnimal();}
//Dog.javapublic class Dog extends Animal { @Override public void sleep() { System.out.println("dog sleep"); }}
//DogFactory .javapublic class DogFactory implements AnimalFactory{ @Override public Animal createAnimal() { return new Dog(); }}
public class AnimalTest { public static void main(String[] args) { AnimalFactory af = null; af = new DogFactory(); Animal dog = af.createAnimal(); dog.sleep(); AnimalFactory af2 =new PigFactory(); Animal pig = af2.createAnimal(); pig.sleep(); AnimalFactory af3 = new DogFactory(); Animal a = af3.createAnimal(); a.sleep(); af3 = new PigFactory(); a = af3.createAnimal(); a.sleep(); }}
The difference between the factory method mode and the simple factory mode in structure is not obvious. The core of the factory method class is an abstract factory class, while the simple factory model places the core on a specific class.
The factory method mode is called the polymorphism factory mode because each factory class has a common interface or abstract parent class. To add a new product object for system expansion, you only need to add a specific object and a specific factory object. The original factory object does not need to be modified or the client needs to be modified, it is in line with the "open-closed" principle. The simple factory mode has to modify the factory method after adding new product objects, and the scalability is poor.
After the factory method model degrades, it can be transformed into a simple factory model.
I am the dividing line of tiantiao
Source code: http://pan.baidu.com/s/1dD1Qx01
Java).zip
Reprinted please indicate the source: http://www.cnblogs.com/yydcdut