This article transferred from: http://www.cnblogs.com/archimedes/p/java-factory-method-pattern.html
Factory method Mode (alias: Virtual construction)
Defines an interface for creating objects, letting subclasses decide which class to instantiate. The Factory method defers the instantiation of a class to its subclasses.
Overview
A factory method pattern can be used to design a system when the system is ready to provide the user with an instance of a subclass of a class and does not want the user code to be coupled with the subclass. The key to a factory method pattern is to define an abstract method in an interface or abstract class that returns an instance of a subclass of a class that has its subclasses or classes that implement the interface to return an instance of a subclass by overriding the abstract method.
Applicability
1. When a class does not know the class of the object it must create.
2. When a class wants its subclass to specify the object it creates.
3. When the class delegates the responsibility of creating an object to one of several helper subclasses, and you want to put which helper subclass is the agent this information is localized.
participants
1.Product defines the interface of the object created by the factory method.
2.CONCRETEPRODUCT implements the product interface.
3.Creator declares the factory method, which returns an object of type product. Creator can also define a default implementation of a factory method that returns a default Concreteproduct object. You can call the factory method to create a product object.
4.ConcreteCreator redefine the factory method to return a concreteproduct instance.
Structure and use of factory method pattern
The structure of the pattern consists of four roles:
• Abstract products (product)
• Specific Products (concreteproduct)
• Constructor (Creator)
• Concrete constructor (Concretecreator)
UML Class diagrams for schemas
Actual combat part
Example 1: Suppose there are three refills, the red refill, the blue refill, and the black refill, respectively. The user wants to use a ballpoint pen to clarify the color of the refill.
Description and use of the structure of the pattern
1. Abstract (product): Pencore.java
Public abstract class pencore{ String color; public abstract void Writeword (String s);}
2. Specific products (concreteproduct) _1: Redpencore.java
public class Redpencore extends pencore{ redpencore () { color= "red"; } public void Writeword (String s) { System.out.println ("write" +color+ "word:" +s);} }
Specific products (concreteproduct) _2: Bluepencore.java
public class Bluepencore extends pencore{ bluepencore () { color= "blue"; } public void Writeword (String s) { System.out.println ("write" +color+ "word:" +s);} }
Specific products (concreteproduct) _3: Blackpencore.java
public class Blackpencore extends pencore{ blackpencore () { color= "black"; } public void Writeword (String s) { System.out.println ("write" +color+ "word:" +s);} }
3. Constructor (Creator): Ballpen.java
Public abstract class ballpen{ Ballpen () { System.out.println ("produced a ball pen containing" +getpencore (). color+ "Refill"); } Public abstract Pencore getpencore ();//Factory method}
4. Concrete Constructor (Concretecreator):
Redballpen.javapublic class Redballpen extends ballpen{public Pencore Getpencore () { return new Redpencore (); C2/>}}blueballpen.javapublic class Blueballpen extends ballpen{public Pencore Getpencore () { return new Bluepencore (); }} Blackballpen.javapublic class Blackballpen extends ballpen{public Pencore Getpencore () { return new Blackpencore (); }}
5. Application Application.java
public class application{public static void Main (String args[]) { pencore pencore; Ballpen ballpen=new Blueballpen (); Pencore=ballpen.getpencore (); Pencore.writeword ("Hello, nice to meet You"); Ballpen=new Redballpen (); Pencore=ballpen.getpencore (); Pencore.writeword ("How is You"); Ballpen=new Blackballpen (); Pencore=ballpen.getpencore (); Pencore.writeword ("Nice to Meet You");} }
Advantages of the Factory method model
• Use the factory method to decouple the code of the user from that of a subclass of a particular class.
• The factory method eliminates the need for users to know how the object it is being created, just know what the object has to do with it.
"Turn" Java design mode-factory method Mode (Factory methods)