Factory mode (factory pattern) detailed description
This address: http://blog.csdn.net/caroline_wendy/article/details/27081511
Factory Method Mode: defines an interface to create an object, but subclasses decide which class to instantiate. The factory method defers the instantiation of the class to the subclass.
Including:
Creator parent Class (creator), including methods for creating objects (instead of new detailed objects, creating different objects by reference), and some main methods;
Detailed creator (concrete creator), inherits the creator parent class, implements the method of creating the object, different parameters can create different objects;
The product class parent (products), including the basic usage of the product, is used by the basic method of the Creator parent class (creator);
Detailed Product (concrete creator), inherit the product class parent class, the specific product is customized for the detailed creator (concrete creator) choose to create;
Detailed method:
1. Product Class The parent class (product), including some basic methods of the product.
Code:
/** * @time May 26, 2014 */package factory;import java.util.arraylist;/** * @author C.l.wang * */public abstract class Pizza {String name; String Dough; Raw dough string sauce; Sauce arraylist<string> toppings = new arraylist<string> (); decorate void Prepare () {System.out.println ("Preparing" + name); System.out.println ("tossing dough ..." + dough); System.out.println ("Adding sauce ..." + 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 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;}}
2.
Detailed Product class (concrete products), detailed product customization method.
Code:
/** * @time May 26, 2014 */package factory;/** * @author C.l.wang * */public class Nystylecheesepizza extends Pizza {public Nystylecheesepizza () {name = "NY Style Sauce and Cheese Pizza";d ough = "Thin crust dough"; Sauce = "Marinara Sauce"; topping S.add ("grated Reggiano Cheese");}} /** * @time May 26, 2014 */package factory;/** * @author C.l.wang * */public class Chicagostylecheesepizza extends Pizza {PU Blic Chicagostylecheesepizza () {name = "Chicago Style deep Dish Cheese Pizza";d ough = "Extra Thick crust dough"; sauce = "P Lum Tomato Sauce "; Toppings.add (" Shredded mozzarella Cheese ");} void Cut () {System.out.println ("cutting the pizza into square slices");}}
3.
Creator parent Class (creator), detailed usage, invokes the method of the Product class parent class (Products), providing a different detailed product class (concrete products) based on the parameters.
Code:
/** * @time May 26, 2014 */package factory;/** * @author C.l.wang * */public abstract class Pizzastore {public Pizza Orderpi Zza (String Item) {Pizza Pizza;pizza = Createpizza (item);p izza.prepare ();p izza.bake ();p izza.cut ();p izza.box (); return Pizza;} Abstract Pizza Createpizza (String item);}
4.
Detailed creator (concrete creator), create different detailed products (concrete product) by the number of references.
Code:
/** * @time May 26, 2014 */package factory;/** * @author C.l.wang * */public class Nypizzastore extends Pizzastore {/* (non- Javadoc) * @see factory. Pizzastore#createpizza (java.lang.String) */@OverridePizza Createpizza (String Item) {//TODO auto-generated method STUBIF (item.equals ("cheese")) {return new Nystylecheesepizza ();} else return null;}} /** * @time May 26, 2014 */package factory;/** * @author C.l.wang * */public class Chicagopizzastore extends Pizzastore {/* (non-javadoc) * @see factory. Pizzastore#createpizza (java.lang.String) */@OverridePizza Createpizza (String Item) {//TODO auto-generated method STUBIF (item.equals ("cheese")) {return new Chicagostylecheesepizza ();} else return null;}}
5.
Test , create a different detailed creator (concrete creator), that is, the factory, by calling the function of the parameters, select different products to use.
Code:
/** * @time May 26, 2014 */package factory;/** * @author C.l.wang * */public class Pizzatestdrive {/** * @param args */publi c static void Main (string[] args) {//TODO auto-generated method Stubpizzastore Nystore = new Nypizzastore (); Pizzastore Chicagostore = new Chicagopizzastore (); Pizza Pizza = Nystore.orderpizza ("cheese"); System.out.println ("Ethan ordered a" + pizza.getname () + "\ n");p Izza = Chicagostore.orderpizza ("cheese"); System.out.println ("Joel ordered a" + pizza.getname () + "\ n");}}
6.
Output :
Preparing NY Style Sauce and Cheese pizzatossing dough ... Thin crust doughadding sauce ... Marinara sauceadding toppings: grated Reggiano cheesebake for minutes in 350Cutting the pizza into diagonal slicesp Lace Pizza in Official Pizzastore Boxethan ordered a NY style Sauce and Cheese pizzapreparing Chicago Style deep Dish Chee Se pizzatossing dough ... Extra Thick crust doughadding sauce ... Plum Tomato sauceadding toppings: shredded mozzarella cheesebake for minutes in 350Cutting the pizza into square sl Icesplace Pizza in Official Pizzastore Boxjoel ordered a Chicago Style deep Dish Cheese Pizza
Object-oriented design principles:
Dependency inversion principle: to rely on abstraction, do not rely on detailed classes.
Guidelines:
1. A variable cannot hold a reference to a detailed class.
2. Do not let the class derive from the detail class.
3. Do not overwrite methods that have been implemented in the base class.
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Design mode-Factory mode (factory pattern) detailed description