Abstract Factory Model (Abstracts factory pattern) detailed

Source: Internet
Author: User
Tags abstract stub

Abstract Factory Pattern: Provides an interface for creating a family of related or dependent objects without explicitly specifying a specific class.

All code: http://download.csdn.net/detail/u012515223/7403553

Specific methods:

1. Provide an abstract factory (abstract Factory) interface (interface) class, which is inherited by different specific factories (concrete factory).

Code:

/** 
 * @time May 26, 2014 * * *
package factory;  
      
/** * @author C.l.wang * * * * * */public
interface pizzaingredientfactory {public  
    dough createdough (); Public  
    sauce Createsauce ();  
    Public Cheese Createcheese ();  
    Public veggies[] Createveggies ();  
    Public pepperoni Createpepperoni ();  
    Public clams Createclam ();  
}

2. Specific factory (concrete factory), implement abstract factory (abstract Factory) interface (interface).

Code:

/** * @time May 26, 2014 * * * Package factory; /** * @author C.l.wang * */public class Nypizzaingredientfactory implements Pizzaingredientfactory {/ * (non-javadoc) * @see factory. Pizzaingredientfactory#createdough () */@Override public Dough Createdough () {//TODO Auto-ge  
    nerated method Stub return new Thincrustdough (); }/* (non-javadoc) * @see factory. Pizzaingredientfactory#createsauce () */@Override public sauce createsauce () {//TODO Auto-ge  
    nerated method Stub return new Marinarasauce (); }/* (non-javadoc) * @see factory. Pizzaingredientfactory#createcheese () */@Override public Cheese Createcheese () {//TODO Auto  
    -generated method Stub return new Reggianocheese (); }/* (non-javadoc) * @see factory. Pizzaingredientfactory#createveggies () * * * @OverridE public veggies[] Createveggies () {//TODO auto-generated Method stub veggies veggies[] = {NEW  
        Garlic (), new Onion (), New Mushroom (), New Redpepper ()};  
    return veggies; }/* (non-javadoc) * @see factory. Pizzaingredientfactory#createpepperoni () */@Override Public Pepperoni Createpepperoni () {//  
    TODO auto-generated Method stub return new Slicedpepperoni (); }/* (non-javadoc) * @see factory. Pizzaingredientfactory#createclam () */@Override public Clams Createclam () {//TODO Auto-gene  
    Rated method stub return new Freshclams ();  
      
}/** * @time May 26, 2014 * * * Package factory;  
      
    /** * @author C.l.wang * */public class Chicagopizzaingredientfactory implements Pizzaingredientfactory { /* (non-javadoc) * @see factory.  
Pizzaingredientfactory#createdough () */    @Override public Dough Createdough () {//TODO auto-generated A stub return new THICKCR  
    Ustdough (); }/* (non-javadoc) * @see factory. Pizzaingredientfactory#createsauce () */@Override public sauce createsauce () {//TODO Auto-ge  
    nerated method Stub return new Plumtomatosauce (); }/* (non-javadoc) * @see factory. Pizzaingredientfactory#createcheese () */@Override public Cheese Createcheese () {//TODO Auto  
    -generated method Stub return new Mozzarellacheese (); }/* (non-javadoc) * @see factory.  Pizzaingredientfactory#createveggies () */@Override public veggies[] Createveggies () {//TODO  
        auto-generated method Stub veggies veggies[] = {new Blackolives (), new spinach (), New Eggplant ()};  
    return veggies; }/* (non-javadoc) * @see factory. PizzAingredientfactory#createpepperoni () */@Override Public Pepperoni Createpepperoni () {//TODO  
    auto-generated method Stub return new Slicedpepperoni (); }/* (non-javadoc) * @see factory. Pizzaingredientfactory#createclam () */@Override public Clams Createclam () {//TODO auto-generate  
    D method Stub return new Frozenclams (); }  
      
}

3. Product Abstraction (abstract) parent class, providing interfaces for specific products (concrete product) calls.

Code:

/** 
 * @time May 26, 2014 * * *
package factory;  
      
/** 
 * @author C.l.wang 
 *
/Public abstract class Pizza {  
    String name;  
    Dough dough; Raw dough  
    sauce sauce;//sauce  
    veggies veggies[];  
    Cheese Cheese;  
    Pepperoni Pepperoni;  
    Clams clam;  
          
    abstract void Prepare ();  
          
    void Bake () {  
        System.out.println ("Bake for minutes at");  
    }  
          
    void Cut () {  
        System.out.println ("cutting the pizza into diagonal slices");  
          
    void Box () {  
        System.out.println ("place pizza in official Pizzastore box");  
    }  
          
    void SetName (String name) {  
        this.name = name;  
    }  
          
    Public String GetName () {return  
        name;  
    }  
}

4. The specific product (concrete product) inherits the product abstraction (abstract) parent class, implements the inheritance interface using the factory class, and produces different products through different factories;

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.