披萨项目:要方便披萨品种的扩展、要便于维护、要能运行时扩展
披萨族设计:
1、正常逻辑
抽象披萨类(Pizza )、两个具体披萨类(CheesePizza、GreekPizza)、披萨订单类(OrderPizza )
如果我们有新的披萨类,就要对OrderPizza类,增加else if,违背了开闭原则。
public abstract class Pizza { protected String name; /** * 披萨准备 */ public abstract void prepare(); /** * 披萨烘焙 */ public void bake() { System.out.println(name + " baking;"); } /** * 披萨切割 */ public void cut() { System.out.println(name + " cutting;"); } /** * 披萨打包 */ public void box() { System.out.println(name + " boxing;"); } public void setname(String name) { this.name = name; } }
public class CheesePizza extends Pizza { @Override public void prepare() { super.setname("CheesePizza"); System.out.println(name + " preparing;"); } }
public class GreekPizza extends Pizza { @Override public void prepare() { super.setname("GreekPizza"); System.out.println(name + " preparing;"); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderPizza { public OrderPizza() { Pizza pizza = null; String ordertype; do { ordertype = gettype(); if (ordertype.equals("cheese")) { pizza = new CheesePizza(); } else if (ordertype.equals("greek")) { pizza = new GreekPizza(); } else { break; } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } while (true); } private String gettype() { try { BufferedReader strin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("input pizza type: "); String str = strin.readLine(); return str; } catch (IOException e) { e.printStackTrace(); return ""; } } public static void main(String [] args) { OrderPizza orderPizza = new OrderPizza(); } }
2、使用简单工厂模式
简单工厂模式的设计方案:定义一个实例化披萨对象的类,封装创建对象的代码
简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为
增加一个简单工厂类(SimplePizzaFactory )和修改披萨订单类(OrderPizzaBySimple )
public class SimplePizzaFactory { public Pizza createPizza(String ordertype) { Pizza pizza = null; if (ordertype.equals("cheese")) { pizza = new CheesePizza(); } else if (ordertype.equals("greek")) { pizza = new GreekPizza(); } return pizza; } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderPizzaBySimple { private SimplePizzaFactory simplePizzaFactory; public OrderPizzaBySimple(SimplePizzaFactory simplePizzaFactory) { this.simplePizzaFactory = simplePizzaFactory; Pizza pizza = null; String ordertype; do { ordertype = gettype(); pizza = this.simplePizzaFactory.createPizza(ordertype); if (pizza != null) { pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } } while (true); } private String gettype() { try { BufferedReader strin = new BufferedReader(new InputStreamReader( System.in)); System.out.println("input pizza type:"); String str = strin.readLine(); return str; } catch (IOException e) { e.printStackTrace(); return ""; } } public static void main(String [] args) { SimplePizzaFactory simplePizzaFactory = new SimplePizzaFactory(); OrderPizzaBySimple orderPizza = new OrderPizzaBySimple(simplePizzaFactory); } }
3、使用工厂模式
披萨项目新需求的困惑:披萨项目加盟店
如果用简单工厂模式来处理,每个加盟店都要创建一个简单工厂。
工厂方法模式设计方案:将披萨项目里的披萨对象实例化功能抽象成抽象方法,在 不同加盟店具体实现功能。
工厂方法模式:定义了一个创建对象的抽象方法,由子类决定要实例化的类。 工厂方法模式将对象的实例化推迟到子类
披萨抽象类(OrderPizzaByFactory)、两个具体的子类(伦敦加盟商LDOrderPizza、纽约加盟商NYOrderPizza)
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public abstract class OrderPizzaByFactory { abstract Pizza createPizza(String ordertype); public OrderPizzaByFactory() { Pizza pizza = null; String ordertype; do { ordertype = gettype(); pizza = createPizza(ordertype); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } while (true); } private String gettype() { try { BufferedReader strin = new BufferedReader(new InputStreamReader( System.in)); System.out.println("input pizza type:"); String str = strin.readLine(); return str; } catch (IOException e) { e.printStackTrace(); return ""; } } }
public class LDOrderPizza extends OrderPizzaByFactory { @Override Pizza createPizza(String ordertype) { Pizza pizza = null; if (ordertype.equals("cheese")) { pizza = new LDCheesePizza(); } else if (ordertype.equals("greek")) { pizza = new LDGreekPizza(); } return pizza; } public static class LDCheesePizza extends Pizza{ @Override public void prepare() { super.setname("CheesePizza"); System.out.println(name + " preparing;"); } } public static class LDGreekPizza extends Pizza{ @Override public void prepare() { super.setname("LDGreekPizza"); System.out.println(name + " preparing;"); } } public static void main(String [] args) { OrderPizzaByFactory orderPizza = new LDOrderPizza(); } }
public class NYOrderPizza extends OrderPizzaByFactory { @Override Pizza createPizza(String ordertype) { Pizza pizza = null; if (ordertype.equals("cheese")) { pizza = new NYCheesePizza(); } else if (ordertype.equals("greek")) { pizza = new NYGreekPizza(); } return pizza; } public static class NYCheesePizza extends Pizza{ @Override public void prepare() { super.setname("NYCheesePizza"); System.out.println(name + " preparing;"); } } public static class NYGreekPizza extends Pizza{ @Override public void prepare() { super.setname("NYCheesePizza"); System.out.println(name + " preparing;"); } } public static void main(String [] args) { OrderPizzaByFactory orderPizza = new NYOrderPizza(); } }
4、升级抽象工程模式
增加抽象工厂类(AbsFactory)、两个具体的子类工厂(伦敦工厂NYFactory、纽约工厂LDFactory)、披萨订单类(OrderPizzaByAbs)
public interface AbsFactory { public Pizza CreatePizza(String ordertype) ; }
public class NYFactory implements AbsFactory { @Override public Pizza CreatePizza(String ordertype) { Pizza pizza = null; if (ordertype.equals("cheese")) { pizza = new NYCheesePizza(); } else if (ordertype.equals("greek")) { pizza = new NYGreekPizza(); } return pizza; } public static class NYCheesePizza extends Pizza{ @Override public void prepare() { super.setname("NYCheesePizza"); System.out.println(name + " preparing;"); } } public static class NYGreekPizza extends Pizza{ @Override public void prepare() { super.setname("NYCheesePizza"); System.out.println(name + " preparing;"); } } }
public class LDFactory implements AbsFactory { @Override public Pizza CreatePizza(String ordertype) { Pizza pizza = null; if (ordertype.equals("cheese")) { pizza = new LDCheesePizza(); } else if (ordertype.equals("greek")) { pizza = new LDGreekPizza(); } return pizza; } public static class LDCheesePizza extends Pizza{ @Override public void prepare() { super.setname("CheesePizza"); System.out.println(name + " preparing;"); } } public static class LDGreekPizza extends Pizza{ @Override public void prepare() { super.setname("LDGreekPizza"); System.out.println(name + " preparing;"); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class OrderPizzaByAbs { private AbsFactory mFactory; public OrderPizzaByAbs(AbsFactory mFactory) { setFactory(mFactory); } public void setFactory(AbsFactory mFactory) { Pizza pizza = null; String ordertype; this.mFactory = mFactory; do { ordertype = gettype(); pizza = mFactory.CreatePizza(ordertype); if (pizza != null) { pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); } } while (true); } private String gettype() { try { BufferedReader strin = new BufferedReader(new InputStreamReader( System.in)); System.out.println("input pizza type:"); String str = strin.readLine(); return str; } catch (IOException e) { e.printStackTrace(); return ""; } } public static void main(String [] args) { AbsFactory absFactory = new LDFactory(); OrderPizzaByAbs orderPizzaByAbs = new OrderPizzaByAbs(absFactory); } }