一个披萨的项目:要便于披萨种类的扩展,要便于维护
public abstract class Pizza {
/**
* 名字
*/
protected String name;
// 准备原材料,不同的披萨不一样,因此我们做成抽象方法
public abstract void prepare();
public void back() {
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() {
System.out.println("给奶酪披萨准备原材料");
}
}
public class GreekPizza extends Pizza {
@Override
public void prepare() {
System.out.println("给希腊披萨准备原材料");
}
}
public class OrderPizza {
/**
* 构造器
*/
public OrderPizza() {
Pizza pizza = null;
// 订购披萨的类型
String orderType;
do {
orderType = this.getType();
if (orderType.equals("greek")) {
pizza = new GreekPizza();
pizza.setName("希腊披萨");
} else if (orderType.equals("cheese")) {
pizza = new CheesePizza();
pizza.setName("奶酪披萨");
} else {
break;
}
// 输出披萨的制作过程
pizza.prepare();
pizza.back();
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 PizzaStore {
public static void main(String[] args) {
new OrderPizza();
}
}
对扩展开放,对修改关闭
。即当我们给类增加新功能时,尽量不修改代码,或者尽可能少修改代码。其他有创建到pizza对象的代码就不需要修改了
,即简单工厂模式简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例
。简单工厂模式是工厂模式家族中最简单实用的模式简单工厂模式的设计方案:定义一个可以实例化Pizza对象的类,封装创建对象的代码
// 创建简单工厂类
public class SimpleFactory {
// 增加orderType,返回对应的pizza对象
public Pizza createPizza(String orderType) {
Pizza pizza = null;
System.out.println("使用简单工厂模式");
if (orderType.equals("greek")) {
pizza = new GreekPizza();
pizza.setName("希腊披萨");
} else if (orderType.equals("cheese")) {
pizza = new CheesePizza();
pizza.setName("奶酪披萨");
}else if (orderType.equals("pepper")) {
pizza = new CheesePizza();
pizza.setName("胡椒披萨");
}
return pizza;
}
}
public class OrderPizza {
// 定义一个简单工厂对象
SimpleFactory simpleFactory;
Pizza pizza = null;
// 构造器
public OrderPizza(SimpleFactory simpleFactory) {
setFactory(simpleFactory);
}
public void setFactory(SimpleFactory simpleFactory) {
String orderType = "";
// 设置简单工厂模式
this.simpleFactory = simpleFactory;
do {
orderType = getType();
pizza = this.simpleFactory.createPizza(orderType);
// 输出pizza
if(pizza != null) {
// 订购成功
pizza.prepare();
pizza.back();
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 "";
}
}
}
public class PizzaStore {
public static void main(String[] args) {
// new OrderPizza();
// 使用简单工厂模式
new OrderPizza(new SimpleFactory());
}
}
// 将Pizza类做成抽象
public abstract class Pizza {
/**
* 名字
*/
protected String name;
// 准备原材料,不同的披萨不一样,因此我们做成抽象方法
public abstract void prepare();
public void back() {
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 LDCheesePizza extends Pizza {
@Override
public void prepare() {
setName("伦敦的奶酪pizza");
System.out.println("伦敦的胡奶酪pizza 准备原材料");
}
}
public class LDpepperPizza extends Pizza {
@Override
public void prepare() {
setName("伦敦的胡椒pizza");
System.out.println("伦敦的胡椒pizza 准备原材料");
}
}
public class BJCheesePizza extends Pizza {
@Override
public void prepare() {
setName("北京的奶酪pizza");
System.out.println("北京的奶酪pizza 准备原材料");
}
}
public class BJpepperPizza extends Pizza {
@Override
public void prepare() {
setName("北京的胡椒pizza");
System.out.println("北京的胡椒pizza 准备原材料");
}
}
public abstract class OrderPizza {
// 定义一个抽象方法,createPizza,让各个工厂子类自己实现
abstract Pizza createPizza(String orderType);
// 构造器
public OrderPizza() {
Pizza pizza = null;
// 订购披萨类型
String orderType;
do {
orderType = getType();
// 抽象方法,由工厂子类完成
pizza = createPizza(orderType);
// 输出pizza制作过程
pizza.prepare();
pizza.back();
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 BJOrderPizza extends OrderPizza {
@Override
Pizza createPizza(String orderType) {
Pizza pizza = null;
if(orderType.equals("cheese")) {
pizza = new BJCheesePizza();
}else if(orderType.equals("pepper")) {
pizza = new BJpepperPizza();
}
return pizza;
}
}
public class LDOrderPizza extends OrderPizza {
@Override
Pizza createPizza(String orderType) {
Pizza pizza = null;
if(orderType.equals("cheese")) {
pizza = new LDCheesePizza();
}else if(orderType.equals("pepper")) {
pizza = new LDpepperPizza();
}
return pizza;
}
}
public class PizzaStore {
public static void main(String[] args) {
// 创建北京的各种口味披萨
// new BJOrderPizza();
// 创建伦敦的各种口味披萨
new LDOrderPizza();
}
}
// 一个抽象工厂模式的抽象层(接口)
public interface AbsFactory {
// 让下面的工厂子类来具体实现
public Pizza createPizza(String orderType);
}
public class BJFactory implements AbsFactory {
@Override
public Pizza createPizza(String orderType) {
Pizza pizza = null;
if(orderType.equals("cheese")) {
pizza = new BJCheesePizza();
}else if(orderType.equals("pepper")) {
pizza = new BJpepperPizza();
}
return pizza;
}
}
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("pepper")) {
pizza = new LDpepperPizza();
}
return pizza;
}
}
public class OrderPizza {
AbsFactory factory;
// 构造器
public OrderPizza(AbsFactory factory) {
setFactory(factory);
}
private void setFactory(AbsFactory factory) {
Pizza pizza = null;
String orderType = "";
this.factory = factory;
do {
orderType = getType();
pizza = factory.createPizza(orderType);
if(pizza != null) {
pizza.prepare();
pizza.back();
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 "";
}
}
}
public class PizzaStore {
public static void main(String[] args) {
// 北京
// new OrderPizza(new BJFactory());
// 伦敦
new OrderPizza(new LDFactory());
}
}
将实例化对象的代码提取出来,放到一个类中统一管理和维护,达到和主项目的依赖关系的解耦。从而提高项目的扩展和维护性。
简单工厂模式、工厂方法模式、抽象工厂模式