Java design pattern (5) factory pattern

Source: Internet
Author: User

1. Define the abstract product class [html] package com. pattern. factory; import java. util. arrayList; public abstract class Pizza {String name; String dough; String sauce; ArrayList toppings = new ArrayList (); void prepare () {System. out. println ("Prepareing" + name); System. out. println ("Tossing dough... "); System. out. println ("Adding 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 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");} public String getName () {return name ;}} 2, product class [html] package com. pattern. factory; public class chicagstylecheesepizza extends Pizza {public chicagstylecheesepizza () {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") ;}} [html] package com. pattern. factory; public class NYStyleCheesePizza extend Pizza {public NYStyleCheesePizza () {name = "NY Style Sauce and Cheese Pizza"; dough = "Thin Crust Dough"; sauce = "Marinara Sauce "; toppings. add ("Grated Reggiano Cheese") ;}} 3. Define the abstract factory class [html] package com. pattern. factory; public abstract class PizzaStore {public Pizza orderPizza (String type) {Pizza pizza; pizza = createPizza (type); pizza. prepare (); pizza. bake (); pizza. cut (); pizza. box (); return pizza;} protected abstract Pizza createPizza (String type);} 4. Define a factory class [html] package com. pattern. factory; import com. pattern. factory. bean. chicagstyleclampizza; import com. pattern. factory. bean. chicagstylepepperonipizza; import com. pattern. factory. bean. chicagstyleveggiepizza; public class ChicagoPizzaStore extends PizzaStore {/*** @ see com. pattern. factory. pizzaStore # createPizza (java. lang. string) */@ Override protected Pizza createPizza (String type) {if (type. equals ("cheese") {return new chicagstylecheesepizza ();} else if (type. equals ("veggie") {return new Chicago styleveggiepizza ();} else if (type. equals ("clam") {return new chicagstyleclampizza ();} else if (type. equals ("pepperoni") {return new Chicago stylepepperonipizza ();} else {return null ;}} [html] package com. pattern. factory; import com. pattern. factory. bean. NYStyleClamPizza; import com. pattern. factory. bean. NYStylePepperoniPizza; import com. pattern. factory. bean. NYStyleVeggiePizza; public class NYPizzaStore extends PizzaStore {/*** @ see com. pattern. factory. pizzaStore # createPizza (java. lang. string) */@ Override protected Pizza createPizza (String type) {if (type. equals ("cheese") {return new NYStyleCheesePizza ();} else if (type. equals ("veggie") {return new NYStyleVeggiePizza ();} else if (type. equals ("clam") {return new NYStyleClamPizza ();} else if (type. equals ("pepperoni") {return new NYStylePepperoniPizza ();} else {return null ;}}5, test class [html] package com. pattern. factory; public class PizzaTestDrive {public static void main (String [] args) {PizzaStore nyStore = new NYPizzaStore (); PizzaStore Chicago store = new ChicagoPizzaStore (); Pizza pizza = nyStore. orderPizza ("cheese"); System. out. println ("Ethan ordered a" + pizza. getName () + "\ n"); pizza = chicagstore. orderPizza ("cheese"); System. out. println ("Joel order a" + pizza. getName () + "\ n") ;}} Test Result: Prepareing NY Style Sauce and Cheese PizzaTossing dough... adding sauce... adding toppings: Grated Reggiano serving for 25 minutes at 350 Cutting the pizza into diagonal slicesp?pizza in official ?boxethan ordered a NY Style Sauce and Cheese {chicago Style Deep Dish Cheese {dough... adding sauce... adding toppings:

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.