Head First设计模式-工厂模式
来源:互联网 发布:python多进程编程 编辑:程序博客网 时间:2024/05/16 06:42
一、整体代码
PizzaStore.java
public abstract class PizzaStore { abstract Pizza createPizza(String item);//面向抽象编程,对扩展开放,对修改封闭。 public Pizza orderPizza(String type) {Pizza pizza = createPizza(type);System.out.println("--- Making a " + pizza.getName() + " ---");pizza.prepare();//面向抽象编程pizza.bake();pizza.cut();pizza.box();return pizza;}}
NYPizzaStore.java
public class NYPizzaStore extends PizzaStore {Pizza createPizza(String item) {if (item.equals("cheese")) {return new NYStyleCheesePizza();} else if (item.equals("clam")) {return new NYStyleClamPizza();} else return null;}}
ChicagoPizzaStore.java
public class ChicagoPizzaStore extends PizzaStore {Pizza createPizza(String item) { if (item.equals("cheese")) { return new ChicagoStyleCheesePizza(); } else if (item.equals("clam")) { return new ChicagoStyleClamPizza(); } else return null;}}
Pizza.java
import java.util.ArrayList;public abstract class Pizza {String name;String dough;String sauce;ArrayList toppings = new ArrayList(); void prepare() {System.out.println("Preparing " + 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;}public String toString() {StringBuffer display = new StringBuffer();display.append("---- " + name + " ----\n");display.append(dough + "\n");display.append(sauce + "\n");for (int i = 0; i < toppings.size(); i++) {display.append((String )toppings.get(i) + "\n");}return display.toString();}}
ChicagoStyleCheesePizza.java
public class ChicagoStyleCheesePizza extends Pizza {public ChicagoStyleCheesePizza() { 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");}}
ChicagoStyleClamPizza.java
public class ChicagoStyleClamPizza extends Pizza {public ChicagoStyleClamPizza() {name = "Chicago Style Clam Pizza";dough = "Extra Thick Crust Dough";sauce = "Plum Tomato Sauce"; toppings.add("Shredded Mozzarella Cheese");toppings.add("Frozen Clams from Chesapeake Bay");} void cut() {System.out.println("Cutting the pizza into square slices");}}
NYStyleCheesePizza.java
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");}}
NYStyleClamPizza.java
public class NYStyleClamPizza extends Pizza {public NYStyleClamPizza() {name = "NY Style Clam Pizza";dough = "Thin Crust Dough";sauce = "Marinara Sauce"; toppings.add("Grated Reggiano Cheese");toppings.add("Fresh Clams from Long Island Sound");}}
PizzaTestDrive.java
public class PizzaTestDrive { public static void main(String[] args) {PizzaStore nyStore = new NYPizzaStore();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");pizza = nyStore.orderPizza("clam");System.out.println("Ethan ordered a " + pizza.getName() + "\n"); pizza = chicagoStore.orderPizza("clam");System.out.println("Joel ordered a " + pizza.getName() + "\n");}}
二、总结
1、工厂方法模式定义了对象的接口,但由子类决定实例化的类是哪一个。工厂方法模式让类的实例化推迟到子类。
2、抽象类和接口可以实现多态,这样才保证了面向对象的可扩展性,封装变化,松耦合等特性。设计模式几乎都在与抽象类和接口打交到,用的越多,程序可扩展性越好。
- Head First设计模式-简单工厂模式
- Head First设计模式-工厂模式
- Head First设计模式-抽象工厂模式
- 《Head First 设计模式》工厂模式
- Head First设计模式-简单工厂模式
- Head First设计模式-抽象工厂模式
- 《Head First 设计模式》之工厂模式
- Head First 设计模式 --工厂模式
- Head First设计模式之工厂模式
- Head First-设计模式:工厂模式
- 《Head First 设计模式》之简单工厂
- Head First---工厂模式
- Head First 工厂模式
- <Head First 设计模式>:工厂模式1:方法模式--Pizza
- Head First 设计模式学习笔记(4)---工厂模式
- Head First Java 设计模式——工厂方法模式
- 《Head First设计模式》 读书笔记05 工厂模式(二)
- 《Head First 设计模式》之工厂方法模式
- 字串之定向排列
- Jump Game
- Struts2里ActionContext及获得HttpServletRequest对象
- 对C语言内存对齐的初步了解
- HDU 1498 坐标轴二分匹配
- Head First设计模式-工厂模式
- jQuery and jQuerymobile
- ios开发-适配图片大小
- c笔试题2
- unicode转utf-8
- Windows启动关闭Oracle监听和服务
- Java 面试题问与答:编译时与运行时
- bzoj 1901 动态区间第K大 树状数组套可持久化数据结构
- 斯坦福大学公开课《编程方法学》之笔记二——任务三:Karel与Java