from :《Java Design patterns 》2009 year ( Geng Xiangyi Zhang Yueping Writing )
One , Generalization :
Factory method model ( Alias : Virtual construction )
Defines an interface for creating objects , Let the subclass decide which class to instantiate .Factory Method Delay the instantiation of a class to its subclasses .
Two , introduce
The most common way to get an instance of a subclass of a class is to use new Operators and The construction method of this subclass , But in general, the system does not allow the user code to couple with the subclasses of the class, or the user does not know which subclasses of the class are available , Let's say I have a In the abstract Car class , This class has two subclasses ,Porsche class and QQCar class , And the system wants users to use Factory An instance of a subclass of a class To get Car class An instance of a subclass of .
So , In the design Factory class when , Decided to make it dependent on Car class , And it provides a return Car Object's Method , such as :public Car createCar();
however ,Factory class I don't know what kind of car the user needs , I don't know the need to Car class An instance of which subclass of , Therefore, it is necessary to createCae() Method Set to Abstract method , spontaneous Factory class Also for abstract classes , So we can have its subclasses override createCar() Method , Return respectively Porsche class and QQCar class Example . therefore , When users need porsche class When an instance of the , You don't have to use new Operator and Porsche class To create objects , have access to Factory1 class Create an object , Let the object call createCar() Method You can get a Porsche class Example .
When the system is ready to provide users with subclass instances of a class , When you don't want user code to couple with the subclass , You can use the factory method pattern to design .
The key to the factory approach model is Define an interface or an abstract class Abstract method , This method returns an instance of a subclass of a class , The abstract class or interface Let it Subclass or implement the interface Class By rewriting this abstract method Returns an instance of a subclass .
3、 ... and , Code and class diagrams
The code for the above example :
package MM; abstract class Car { abstract public String getName(); abstract public void buyCar(); } class Porsche extends Car { public String getName() { return "porsche"; } public void buyCar() { System.out.printf(" jun can Would like to Taste try One Next single hand open " + getName() + "!\n"); } } class QQCar extends Car { public String getName() { return "QQcar"; } public void buyCar() { System.out.printf(" jun can Would like to Taste try One Next single hand open " + getName() + "!\n"); } } abstract class Factory { /* Every The first statement of a subclass constructor is an implicit call to super() Originally speaking , If you call in this class Factory(), that createCar().getName() Will go wrong , But an abstract class cannot define an abstract class object , in other words new Factory() This sentence will not appear therefore , This constructor is only called by subclasses , When the ,createCar() It's overloaded ,createCar().getName() That's right */ public Factory() { System.out.printf(" We made one " + createCar().getName() + ".\n"); } abstract public Car createCar(); } class Factory1 extends Factory { public Car createCar() { return new Porsche(); } } class Factory2 extends Factory { public Car createCar() { return new QQCar(); } } public class M { public static void main(String args[]) { Car c; Factory f; f = new Factory1(); c = f.createCar(); c.buyCar(); f = new Factory2(); c = f.createCar(); c.buyCar(); } }
Class diagram :
( notes : Because we can't find the right arrow , The arrow is not standard )
Four , The structure of the factory method pattern
① Abstract product (Product): Car class
② Specific products (ConcreteProduct): Porsche class and QQCar class
③ The builder (Creator): Factory class
④ The specific constructor (ConcreteCreator): Factory1 class and Factory2 class
============ =========== ========= ========= ========= ====== ===== === == =
Thought the emperor township · Spring outing Tang Dynasty : Weizhuang
Spring outing , Apricot blossom blows all over the head . Strangers who young , Full love ?
My concubine planned to marry him , To take life . Even if be mercilessly abandoned , Not ashamed .