《Head First 设计模式》工厂模式
来源:互联网 发布:双色球红球最简单算法 编辑:程序博客网 时间:2024/05/01 15:35
工厂方法模式:定义了一个创建对象的接口,但由子类决定实例化的类是哪一个。工厂方法让实例化推迟到子类。
package headfirst.factory.pizzafm;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();}}
package headfirst.factory.pizzafm;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");}}
package headfirst.factory.pizzafm;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;}}
package headfirst.factory.pizzafm;public class NYPizzaStore extends PizzaStore {Pizza createPizza(String item) {if (item.equals("cheese")) {return new NYStyleCheesePizza();} else if (item.equals("veggie")) {return new NYStyleVeggiePizza();} else if (item.equals("clam")) {return new NYStyleClamPizza();} else if (item.equals("pepperoni")) {return new NYStylePepperoniPizza();} else return null;}}
package headfirst.factory.pizzafm;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");pizza = nyStore.orderPizza("pepperoni");System.out.println("Ethan ordered a " + pizza.getName() + "\n"); pizza = chicagoStore.orderPizza("pepperoni");System.out.println("Joel ordered a " + pizza.getName() + "\n");pizza = nyStore.orderPizza("veggie");System.out.println("Ethan ordered a " + pizza.getName() + "\n"); pizza = chicagoStore.orderPizza("veggie");System.out.println("Joel ordered a " + pizza.getName() + "\n");}}
抽象工厂模式:提供一个接口,用于创建相关或依赖的家族,而不需明确指明具体类。
0 0
- 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 设计模式》之工厂方法模式
- cmd 中报错:不是内部或外部命令,也不是可运行的程序
- LDA学习l
- loadlibrary 返回错误126 找不到指定的模块
- 开发笔记之20140312
- 对象的当前状态使该操作无效
- 《Head First 设计模式》工厂模式
- 黑马程序员---Object 中toString()与equal()方法
- onstat命令 for ipc
- Linux下redis安装部署
- 系统调用,stat,fstat以及lstat函数:检查文件的属性
- 新版Matlab中神经网络训练函数Newff的使用方法
- 超链接或form中是否需要加.action后缀
- js面向对象编程,一个具有各种方法,字段 完整的类
- 连接不到SQL SERVER 2008的解决办法