工厂模式之简单工厂模式
一个具体的需求:
看一个披萨的项目:
要便于披萨种类的扩展,要便于维护
- 披萨的种类很多(比如 GreekPizz、CheesePizz 等)
- 披萨的制作有 prepare,bake, cut, box
- 完成披萨店订购功能
1.传统方式完成
优点是简单好理解。
缺点是违反了设计模式的OCP原则。在添加新种类的pizza pepperPizza 时,使用者(orderPizza)内部的代码也需要修改,没有对修改关闭。
pizza的基类。
package factory.simplefactory.pizzastore.pizza;
/**
* @Author Worm
* @Date 2020/8/10 10:02
* @Version 1.0
**/
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;
}
}
package factory.simplefactory.pizzastore.pizza;
/**
* @Author Worm
* @Date 2020/8/10 10:06
* @Version 1.0
**/
public class CheesePizza extends Pizza{
@Override
public void prepare() {
System.out.println("奶酪披萨准备原材料");
}
}
package factory.simplefactory.pizzastore.pizza;
/**
* @Author Worm
* @Date 2020/8/10 10:07
* @Version 1.0
**/
public class GreekPizza extends Pizza {
@Override
public void prepare() {
System.out.println("给希腊披萨准备原材料");
}
}
package factory.simplefactory.pizzastore.pizza;
/**
* @Author Worm
* @Date 2020/8/10 10:07
* @Version 1.0
**/
public class GreekPizza extends Pizza {
@Override
public void prepare() {
System.out.println("给希腊披萨准备原材料");
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @Author Worm
* @Date 2020/8/10 10:08
* @Version 1.0
**/
public class OrderPizza {
public OrderPizza() {
Pizza pizza = null;
String orderType;
do {
orderType = gettype();
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 PepperPizza();
pizza.setName("胡椒披萨");
} 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 factory.simplefactory.pizzastore.order;
/**
* @Author Worm
* @Date 2020/8/10 10:19
* @Version 1.0
**/
//相当于一个客户端
public class PizzaStore {
public static void main(String[] args) {
new OrderPizza(); /*不使用工厂模式时*/
System.out.println("退出程序");
}
}
2.简单工厂模式
简单工厂模式属于创建型模式,是工厂模式的一种。定义一个创建对象的类,由该工厂对象来决定创建出哪一种产品类的实例。在工厂内部封装实例化对象的行为代码。在软件开发中当我们需要大量的创建某类对象时,就会用到工厂模式。
定义一个可以实例化Pizza对象的工厂类:
package factory.simplefactory.pizzastore.order;
import factory.simplefactory.pizzastore.pizza.CheesePizza;
import factory.simplefactory.pizzastore.pizza.GreekPizza;
import factory.simplefactory.pizzastore.pizza.PepperPizza;
import factory.simplefactory.pizzastore.pizza.Pizza;
/**
* @Author Worm
* @Date 2020/8/10 10:46
* @Version 1.0
**/
//简单工厂类
public class SimpleFactory {
// 也叫静态工厂模式,可以静态化在使用,效果一样
public Pizza createPizza(String orderType) {
System.out.println("使用简单工厂");
Pizza pizza = null;
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 PepperPizza();
pizza.setName("胡椒披萨");
}
return pizza;
}
}
package factory.simplefactory.pizzastore.order;
import factory.simplefactory.pizzastore.pizza.CheesePizza;
import factory.simplefactory.pizzastore.pizza.GreekPizza;
import factory.simplefactory.pizzastore.pizza.PepperPizza;
import factory.simplefactory.pizzastore.pizza.Pizza;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @Author Worm
* @Date 2020/8/10 10:08
* @Version 1.0
**/
public class OrderPizza {
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 "";
}
}
// 定义一个简单工厂对象
SimpleFactory simpleFactory;
Pizza pizza = null;
public void setFactory(SimpleFactory factory) {
String orderType = "";
this.simpleFactory = factory;
do {
orderType = gettype();
Pizza pizza = simpleFactory.createPizza(orderType);
if (pizza != null) {/*订购成功*/
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
} else {
System.out.println("订购失败");
break;
}
} while (true);
}
public OrderPizza(SimpleFactory simpleFactory) {
setFactory(simpleFactory);
}
}
客户端:
package factory.simplefactory.pizzastore.order;
/**
* @Author Worm
* @Date 2020/8/10 10:19
* @Version 1.0
**/
//相当于一个客户端
public class PizzaStore {
public static void main(String[] args) {
// 使用简单工厂
new OrderPizza(new SimpleFactory());
System.out.println("退出程序");
}
}