首先由下面一个需求来引入:
披萨项目:便于披萨种类的扩展,便于维护
- 披萨的种类很多,比如GreekPizz、CheesePizz 等
- 披萨的制作有 prepare、bake、cut、box
- 完成披萨店订购功能
使用传统方式实现
代码实现:
package com.pizzastore.pizza;
public abstract class Pizza {
protected String name;
public abstract void prepare();
public void bake(){
System.out.println("bake");
}
public void cut(){
System.out.println("cut");
}
public void box(){
System.out.println("box");
}
public void setName(String name){
this.name = name;
}
}
package com.pizzastore.pizza;
public class CheesePizza extends Pizza {
@Override
public void prepare() {
setName("奶酪Pizza");
System.out.println(name +"preparing");
}
}
package com.pizzastore.pizza;
public class GreekPizza extends Pizza {
@Override
public void prepare() {
setName("GreekPizza");
System.out.println(name +"preparing");
}
}
package com.pizzastore.order;
import com.pizzastore.pizza.CheesePizza;
import com.pizzastore.pizza.GreekPizza;
import com.pizzastore.pizza.Pizza;
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("greek")) {
pizza = new GreekPizza();
} else if (orderType.equals("cheese")) {
pizza = new CheesePizza();
}else {
break;
}
//输出Pizza制作过程
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 "";
}
}
}
package com.pizzastore.order;
//相当于一个客户端,发出订购
public abstract class PizzaStore {
public static void main(String[] args) {
new OrderPizza();
}
}
UML图:
优缺点:
- 优点是比较好理解,简单易操作
- 缺点是违反了设计模式的ocp原则,即对扩展开放,对修改关闭。即当我们给类增加新的功能的时候,尽量不修改代码,或者尽可能少修改代码。
- 比如上面代码中,我们新增加一个PepperPizza时,需要做两处修改
1)新建PepperPizza类
public class PepperPizzaextends Pizza {
2) OrderPizza类中添加如下修改
} else if (orderType.equals("pepper")) {
pizza = new PepperPizza();
}
简单工厂模式改进
- 简单工厂模式属于创建型模式,是工厂模式的一种。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式。
- 简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为(代码)
- 在软件开发中,当我们会用到大量的创建某种、某类或者某批对象时,就会用到工厂模式。
我们使用简单工厂模式对上述代码做出调整,此处指列出所修改的类的代码:
package com.pizzastore.order;
import com.pizzastore.pizza.CheesePizza;
import com.pizzastore.pizza.GreekPizza;
import com.pizzastore.pizza.Pizza;
public class SampleFactory {
public Pizza createPizza(String orderType){
Pizza pizza = null;
if (orderType.equals("greek")) {
pizza = new GreekPizza();
} else if (orderType.equals("cheese")) {
pizza = new CheesePizza();
}
return pizza;
}
}
package com.pizzastore.order;
import com.pizzastore.pizza.Pizza;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class OrderPizza {
SampleFactory sampleFactory;
Pizza pizza = null;
public OrderPizza(SampleFactory sampleFactory) {
setFactory(sampleFactory);
}
public void setFactory(SampleFactory sampleFactory) {
String orderType = "";
this.sampleFactory = sampleFactory;
do {
orderType = getType();
pizza = this.sampleFactory.createPizza(orderType);
if (pizza != null) {
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
} else {
System.out.println("订购披萨失败");
break;
}
} 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 "";
}
}
}
package com.pizzastore.order;
//相当于一个客户端,发出订购
public abstract class PizzaStore {
public static void main(String[] args) {
new OrderPizza(new SampleFactory());
System.out.println("推出程序。。。");
}
}
UML图修改如下: