Brief introduction
According to the "head first design mode", there are three factory mode: Simple / static factory, factory method and abstract factory.
Note: In fact, not a static factory design pattern, it is more like a programming habits. But here it is based on notes made by [1], in order to facilitate the management of distinction, falsely claiming that it is a factory model.
This article describes the static factory method.
1, definitions: definition of a plant using a simple static method, a static factory.
2, the reason: the static factory mode, because no use to create an object instance of the object, it is possible to create a simple method using a static factory.
3, Disadvantages:
· Can not be changed through inheritance to create an object's behavior.
· Class if you do not contain public or protected constructor, can not be inherited.
4, the advantages:
· In general, you want to get examples of the most commonly used method is to provide a public constructor calls the class, but there will be better elasticity using a static factory method, when the implementation of the change, only need to change the static factory method on it.
· And construction methods are not the same, static factory method can have its own specific name.
· Do not always create a new object called. Use of external static factory method to provide a single example, an instance is prepared in advance, then multiple calls can be, so you can create an instance reduce duplication, bad place to note that thread safety issues.
· Can return an object of any subclass of the original return type.
5 , the specific implementation ( complete code ):
- Static Factory: create objects of processing details
public class SimplePizzaFactory{ public Pizza createPizza(String type){ Pizza pizza = null; if(type.equals("cheese")){ return new CheesePizza(); }else if(type.equals("pepperoni")){ return new PepperoniPizza(); }else if(type.equals("clam")){ return new ClamPizza(); }else if(type.equals("veggie")){ return new VeggiePizza(); }else return null; } }
- Client: used to create objects using a static factory
public class PizzaStore{ SimplePizzaFactory factory; public PizzaStore(SimplePizzaFactory factory){ this.factory = factory; } public Pizza orderPizza(String type){ Pizza pizza; pizza = factory.createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } }
- Abstract Product categories:
public abstract class Pizza { String name;//名称 String dough;//面团类型 String sauce;//酱料类型 ArrayList<String> toppings = new ArrayList<String>();//一套佐料 void prepare(){ System.out.println("Preparing " + name); System.out.println("Tossing dough..."); System.out.println("Adding sauce... "); System.out.println("Adding toppings: "); for(int i = 0; i < toppings.size(); i++){ System.out.println(" " + toppings.get(i)); } } void bake(){ System.out.println("Bake for 25 minutes at 350"); } void cut(){ System.out.println("Cutting the pizza into diagonal slices"); } void box(){ System.out.println("Place pizza in official PizzaStore box"); } public String getName(){ return name; } public String toString(){ StringBuffer display = new StringBuffer(); display.append("------" + name + "-----\n"); display.append(dough + "\n"); display.append(sauce + "\n"); for(String topping : toppings){ display.append(topping + "\n"); } return display.toString(); } }
- Specific product categories:
//芝士披萨 public class CheesePizza extends Pizza{ public CheesePizza(){ name = "Cheese Pizza"; dough = "Cheese Dough"; sauce = "CheeseSauce"; toppings.add("Cheese Cheese"); } } //蛤蜊披萨 public class ClamPizza extends Pizza{ public ClamPizza(){ name = "Clam Pizza"; dough = "Clam Dough"; sauce = "Clam Sauce"; toppings.add("Clam"); } } //意式腊肠披萨 public class PepperoniPizza extends Pizza{ public PepperoniPizza(){ name = "Pepperoni Pizza"; dough = "Pepperoni Dough"; sauce = "Pepperoni Sauce"; toppings.add("Pepperoni"); VeggiePizza () {publicPizza { the extendsVeggiePizzaclasspublicvegetarian pizza// } } name= "Veggie Pizza"; dough = "Veggie Dough"; sauce = "Veggie Sauce"; toppings.add("Veggie"); } }
- test:
public class PizzaTest{ public static void main(String[] args){ SimplePizzaFactory factory = new SimplePizzaFactory(); PizzaStore store = new PizzaStore(factory); Pizza pizza = store.orderPizza("cheese"); System.out.println("Ethan ordered a " + pizza.getName() + "\n"); System.out.println(pizza); pizza = store.orderPizza("veggie"); System.out.println("Joel ordered a " + pizza.getName() + '\n'); System.out.println(pizza); } }
Operating results as shown below:
References:
[1] head first design pattern