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;