Whether it is a factory method or abstract factory, the so-called factory is the use of production, the role of the factory method is to produce an object.
The factory method is defined as: Define an interface for the object to be created, but let the subclass decide which class to instantiate. The factory method delays the instantiation of a class to the subclass.
Definition: Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defter Instantiation to subclasses.
Header first introduces an OO design principle ---- depend upon implements actions. do not depend upon concrete classes. you can find a similar saying in <gof> frameworks use abstract classes to define and maintain relationships between objects. it can be seen that the factory method separates abstraction from implementation, so that the client does not care about the specific implementation of the object, but only the relationship between the object.
The structure of the abstract factory is as follows:
If the factory method in the parent class does not implement the content, the design mode of the template method is used. If there is implemented content, the factory method provides a method to generate a default object, this increases the flexibility of the factory method and does not require override For subclass.
In the structure of parallel inheritance, the factory method can be used to relate Two inheritance systems;
You can use lazy initialization in the factory method.
The factory method should be widely used. The sample code below comes from
public abstract class PizzaStore { public Pizza orderPizza(String type) { Pizza pizza; pizza = createPizza(type); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; } protected abstract Pizza createPizza(String type);}public class NYPizzaStore extends PizzaStore { public Pizza createPizza(String type) { if (type.equals("cheese")) { return new NYStyleCheesePizza(); } return null; }}public class ChicagoPizzaStore extends PizzaStore { protected Pizza createPizza(String type) { if (type.equals("cheese")) { return new ChicagoStyleCheesePizza(); } return null; }}import java.util.ArrayList;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 class NYStyleCheesePizza extends Pizza { public NYStyleCheesePizza() { name = "NY Style Sause and Cheese Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce"; toppings.add("Grated Reggiano Cheese"); }}public class ChicagoStyleCheesePizza extends Pizza { public ChicagoStyleCheesePizza() { name = "Chicago Style Deep Dish Cheese Pizza"; dough = "Extra Thick Crust Dough"; sauce = "Plum Tomato Sauce"; toppings.add("Shredded Mozzarella Cheese"); } void cut() { System.out.println("Cutting the pizza into square slices"); }}import java.util.ArrayList;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; }}
The difference between factory method and abstract factory is that factory method only produces one product, while abstract factory produces families of products.
Abstract Factory can use factory method for every product in the production product set.