Factory mode (factory pattern) detailed

Source: Internet
Author: User
Tags abstract prepare stub

Factory method Pattern: Defines an interface for creating objects, but subclasses decide which class to instantiate. The factory method lets classes defer instantiation to subclasses.

Including:

The Creator parent class (creator), which contains methods for creating objects (instead of new specific objects, creating different objects through parameters), and some basic methods;

The concrete creator (concrete creator), inherits the creator parent class, implements the method of creating the object, and different parameters can create different objects;

The Product class parent (product), which contains the basic use method of the product, is used by the basic method of the Creator parent class (creator);

The specific product (concrete creator), inherits the product class parent class, customizes the specific product for the specific creator (concrete creator) to create;

Specific methods:

1. The Product class is the parent class (product), which contains 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> ();  
        decorated 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. Specific product categories (concrete product), specific product customization methods.

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";  
        Dough = "Thin crust dough";  
              
        sauce = "marinara sauce";  
    Toppings.add ("grated Reggiano Cheese");  
      
}/** * @time May 26, 2014 * * * Package factory; /** * @author C.l.wang * */public class Chicagostylecheesepizza extends Pizza {public Chicagostylecheesepizz  
        A () {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"); }  
}

3. Creator parent Class (creator), the specific use method, the method that invokes the Product class parent (product), provides a different specific product class (concrete product) based on the parameters.

Code:

/** 
 * @time May 26, 2014 * * *
package factory;  
      
/** 
 * @author C.l.wang 
 * 
 *
/Public abstract class Pizzastore {public  
    Pizza Orderpizza (String Item) {  
        Pizza Pizza;  
        Pizza = Createpizza (item);  
              
        Pizza.prepare ();  
        Pizza.bake ();  
        Pizza.cut ();  
        Pizza.box ();  
              
        return pizza;  
    }  
          
    Abstract Pizza Createpizza (String item);  

4. Specific creator (concrete creator), creating different specific products (concrete product) through parameters.

Code:

/** * @time May 26, 2014 * * * Package factory; /** * @author C.l.wang * */public class Nypizzastore extends Pizzastore {/* (non-javadoc) * @se E factory.  Pizzastore#createpizza (java.lang.String) */@Override Pizza Createpizza (String Item) {//TODO auto-generated method Stub if (item.equals ("cheese")) {return new Nystylecheesepi  
        Zza ();  
    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) */@Override Pizza Createpizza (String Item) {//TODO Aut o-generated method Stub if (item.equals ("cheese")) {return new CHICAGOSTYLECHEESEP  
        Izza ();  
    else return null;  
      }
} 

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

5. Test, create a different specific creator (concrete creator), that is, the factory, by calling the function parameters, select a different product to use.

Code:

/** 
 * @time May 26, 2014 * * *
package factory;  
      
/** 
 * @author C.l.wang * */Public
class Pizzatestdrive {  
      
    /** 
     * @param 
     args
    * * public static void Main (string[] args) {  
        //TODO auto-generated method stub  
        Pizzastore nystore = new Nypizzastor E ();  
        Pizzastore Chicagostore = new Chicagopizzastore ();  
              
        Pizza Pizza = Nystore.orderpizza ("cheese");  
        System.out.println ("Ethan ordered a" + pizza.getname () + "\ n");  
              
        Pizza = Chicagostore.orderpizza ("cheese");  
        System.out.println ("Joel ordered a" + pizza.getname () + "\ n");  
    }  
      

6. Output:

Preparing NY Style sauce and Cheese Pizza  
tossing dough ... Thin crust dough  
adding sauce ... Marinara sauce  
Adding toppings: grated reggiano Cheese bake for "minutes at" cutting the  
pizza into  
D iagonal slices Place  
pizza in official Pizzastore box  
Ethan ordered a NY Style sauce and Cheese pizza  
      
G Chicago Style Deep Dish Cheese Pizza tossing  
... Extra Thick crust dough  
adding sauce ... Plum tomato sauce  
adding toppings: shredded mozzarella Cheese bake for "minutes at" cutting the  
p Izza into the square slices place  
pizza in official Pizzastore box  
Joel ordered a Chicago Style Deep Dish Cheese Za

Object-Oriented design principles:

Dependency inversion principle: to rely on abstraction, do not rely on specific classes.

Guiding principles:

1. A variable cannot hold a reference to a specific class.

2. Do not allow classes to derive from specific classes.

3. Do not overwrite methods that have been implemented in the base class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.