Detailed description of Design pattern-abstract factory pattern

Source: Internet
Author: User

Abstract factory pattern


Address: http://blog.csdn.net/caroline_wendy/article/details/27091671


Reference factory Model: Http://blog.csdn.net/caroline_wendy/article/details/27081511


Abstract Factory mode:Provides an interface for creating families of related or dependent objects without specifying specific classes.

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


Specific Method:

1. ProvideAbstract factory interface Class, DifferentSpecific factory (concrete factory)This class is inherited.

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 createpperoni (); public Clams createClam ();}

2. Specific factory (concrete factory), Implementation 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 () */@ Overridepublic Dough createDough () {// TODO Auto-generated method stubreturn new ThinCrustDough ();}/* (non-Javadoc) * @ see factory. pizzaIngredientFactory # createSauce () */@ Overridepublic Sauce createSauce () {// TODO Auto-generated method stubreturn new MarinaraSauce ();}/* (non-Javadoc) * @ see factory. pizzaIngredientFactory # createCheese () */@ Overridepublic Cheese createCheese () {// TODO Auto-generated method stubreturn new ReggianoCheese ();}/* (non-Javadoc) * @ see factory. pizzaIngredientFactory # createVeggies () */@ Overridepublic Veggies [] createVeggies () {// TODO Auto-generated method stubVeggies veggies [] = {new Garlic (), new Onion (), new Mushroom (), new RedPepper ()}; return veggies;}/* (non-Javadoc) * @ see factory. response # createpperoni () */@ Overridepublic Pepperoni createpperoni () {// TODO Auto-generated method stubreturn new SlicedPepperoni ();}/* (non-Javadoc) * @ see factory. pizzaIngredientFactory # createClam () */@ Overridepublic Clams createClam () {// TODO Auto-generated method stubreturn new FreshClams ();}} /*** @ time May 26, 2014 */package factory;/*** @ author C. l. wang **/public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {/* (non-Javadoc) * @ see factory. pizzaIngredientFactory # createDough () */@ Overridepublic Dough createDough () {// TODO Auto-generated method stubreturn new ThickCrustDough ();}/* (non-Javadoc) * @ see factory. pizzaIngredientFactory # createSauce () */@ Overridepublic Sauce createSauce () {// TODO Auto-generated method stubreturn new PlumTomatoSauce ();}/* (non-Javadoc) * @ see factory. pizzaIngredientFactory # createCheese () */@ Overridepublic Cheese createCheese () {// TODO Auto-generated method stubreturn new MozzarellaCheese ();}/* (non-Javadoc) * @ see factory. dependencies # createVeggies () */@ Overridepublic Veggies [] createVeggies () {// TODO Auto-generated method stubVeggies veggies [] = {new BlackOlives (), new Spinach (), new Eggplant ()}; return veggies;}/* (non-Javadoc) * @ see factory. response # createpperoni () */@ Overridepublic Pepperoni createpperoni () {// TODO Auto-generated method stubreturn new SlicedPepperoni ();}/* (non-Javadoc) * @ see factory. pizzaIngredientFactory # createClam () */@ Overridepublic Clams createClam () {// TODO Auto-generated method stubreturn new FrozenClams ();}}

3. Abstract parent class, Provides interfaces Specific product (concrete product)Call.

Code:

/*** @ Time May 26, 2014 */package factory;/*** @ author C. l. wang **/public abstract class Pizza {String name; Dough dough; // Dough Sauce sauce; // dressing Veggies veggies []; Cheese cheese; Pepperoni pepperoni; Clams clam; abstract void prepare (); 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");} void setName (String name) {this. name = name ;}public String getName () {return name ;}}

4. Specific product (concrete product)Inheritance Abstract parent class, Use FactoryImplementation Inheritance Interface, Through Different factories produce different products;

Code:

/*** @ Time May 26, 2014 */package factory;/*** @ author C. l. wang **/public class CheesePizza extends Pizza {PizzaIngredientFactory pizzaIngredientFactory; public CheesePizza (PizzaIngredientFactory pizzaIngredientFactory) {this. pizzaIngredientFactory = pizzaIngredientFactory;}/* (non-Javadoc) * @ see factory. pizza # prepare () */@ overrisponid prepare () {// TODO Auto-generated method stubSystem. out. println ("Preparing" + name); dough = pizzaIngredientFactory. createDough (); sauce = pizzaIngredientFactory. createSauce (); cheese = pizzaIngredientFactory. createCheese (); System. out. println ("Ingredient:" + dough + "," + sauce + "," + cheese) ;}/ *** @ time May 26, 2014 */package factory; /*** @ author C. l. wang **/public class ClamPizza extends Pizza {PizzaIngredientFactory pizzaIngredientFactory; public ClamPizza (PizzaIngredientFactory pizzaIngredientFactory) {this. pizzaIngredientFactory = pizzaIngredientFactory;}/* (non-Javadoc) * @ see factory. pizza # prepare () */@ overrisponid prepare () {// TODO Auto-generated method stubSystem. out. println ("Preparing" + name); dough = pizzaIngredientFactory. createDough (); sauce = pizzaIngredientFactory. createSauce (); cheese = pizzaIngredientFactory. createCheese (); clam = pizzaIngredientFactory. createClam (); System. out. println ("Ingredient:" + dough + "," + sauce + "," + cheese + "," + clam );}}

5. Details Call a function, Through Pass different parameters, Call different factories to produce Different Products.

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 stubPizza pizza = null; PizzaIngredientFactory pizzaIngredientFactory = new items (); if (item. equals ("cheese") {pizza = new CheesePizza (pizzaIngredientFactory); pizza. setName ("New York Style Cheese Pizza");} else if (item. equals ("clam") {pizza = new ClamPizza (pizzaIngredientFactory); pizza. setName ("New York Style Clam Pizza");} return pizza;}/*** @ 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 stubPizza pizza = null; PizzaIngredientFactory pizzaIngredientFactory = new items (); if (item. equals ("cheese") {pizza = new CheesePizza (pizzaIngredientFactory); pizza. setName ("Chicago Style Cheese Pizza");} else if (item. equals ("clam") {pizza = new ClamPizza (pizzaIngredientFactory); pizza. setName ("Chicago Style Clam Pizza");} return pizza ;}}

6. Test Functions

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 stubPizzaStore nyStore = new NYPizzaStore (); PizzaStore chicagstore = new ChicagoPizzaStore (); Pizza pizza = nyStore. orderPizza ("cheese"); System. out. println ("Ethan ordered a" + pizza. getName () + "\ n"); pizza = chicagstore. orderPizza ("cheese"); System. out. println ("Joel ordered a" + pizza. getName () + "\ n"); pizza = nyStore. orderPizza ("clam"); System. out. println ("Ethan ordered a" + pizza. getName () + "\ n"); pizza = chicagstore. orderPizza ("clam"); System. out. println ("Joel ordered a" + pizza. getName () + "\ n ");}}

7. Output:

Preparing New York Style Cheese PizzaIngredient : Thin Crust Dough, Marinara Sauce, Reggiano CheeseBake for 25 minutes at 350Cutting the pizza into diagonal slicesPlace pizza in official PizzaStore boxEthan ordered a New York Style Cheese PizzaPreparing Chicago Style Cheese PizzaIngredient : ThickCrust style extra thick crust dough, Tomato sauce with plum tomatoes, Shredded MozzarellaBake for 25 minutes at 350Cutting the pizza into diagonal slicesPlace pizza in official PizzaStore boxJoel ordered a Chicago Style Cheese PizzaPreparing New York Style Clam PizzaIngredient : Thin Crust Dough, Marinara Sauce, Reggiano Cheese, Fresh Clams from Long Island SoundBake for 25 minutes at 350Cutting the pizza into diagonal slicesPlace pizza in official PizzaStore boxEthan ordered a New York Style Clam PizzaPreparing Chicago Style Clam PizzaIngredient : ThickCrust style extra thick crust dough, Tomato sauce with plum tomatoes,...Bake for 25 minutes at 350Cutting the pizza into diagonal slicesPlace pizza in official PizzaStore boxJoel ordered a Chicago Style Clam Pizza








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.