设计模式——工厂方法模式
来源:互联网 发布:js 检测div大小变化 编辑:程序博客网 时间:2024/06/10 07:13
4.
OO原则:要依赖抽象,不要依赖具体类(依赖倒置原则)
OO模式:工厂方法模式:定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。
例子:披萨店的披萨生产
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;}}
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;}}
public class ChicagoPizzaStore extends PizzaStore {Pizza createPizza(String item) { if (item.equals("cheese")) { return new ChicagoStyleCheesePizza(); } else if (item.equals("veggie")) { return new ChicagoStyleVeggiePizza(); } else if (item.equals("clam")) { return new ChicagoStyleClamPizza(); } else if (item.equals("pepperoni")) { return new ChicagoStylePepperoniPizza(); } else return null;}}
import java.util.ArrayList;public abstract class Pizza {String name;String dough;String sauce;ArrayList<String> toppings = new ArrayList<String>(); void prepare() {System.out.println("Prepare " + name);System.out.println("Tossing dough...");System.out.println("Adding sauce...");System.out.println("Adding toppings: ");for (String topping : toppings) {System.out.println(" " + topping);}} void bake() {System.out.println("Bake for 25 minutes at 350");} void cut() {System.out.println("Cut 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 (String topping : toppings) {display.append(topping + "\n");}return display.toString();}}
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");}}
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");}}
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
- 设计模式—工厂方法
- 设计模式—工厂方法
- 设计模式——抽象工厂模式、工厂方法模式
- Java设计模式—工厂方法模式&抽象工厂模式
- Java设计模式—工厂方法模式&抽象工厂模式
- 设计模式——工厂方法模式
- 设计模式——工厂方法模式
- 设计模式——工厂方法模式
- 设计模式——工厂方法模式
- 设计模式——工厂方法模式
- 设计模式——工厂方法模式
- 设计模式——工厂方法模式
- 设计模式——工厂方法模式
- 设计模式——工厂方法模式
- 设计模式——工厂方法模式
- 设计模式——工厂方法模式
- 设计模式——工厂方法模式
- 设计模式——工厂方法模式
- 二维数组
- HTML5 -video和audio的设置
- 存储过程 游标嵌套
- XZ_iOS之Runtime使用运行时获取类的属性列表
- 前段.微信小程序开发Ⅰ(结构、逻辑、工作流)
- 设计模式——工厂方法模式
- 【Java笔记】反射机制中用Class操作的一些演示
- 后缀数组模板
- 自定义播放器
- 2.HTML浮动与内边框
- 深度学习之循环神经网络算法
- 集合框架
- VirutalBox 下安装 CentOS
- NYOJ 题目77 开灯问题