背景:我们造pizza ,造不同的pizza ,这里举例子,我们造奶酪pizza 和 希腊pizza
思路:我们先创建一个抽象的pizza类,来描述造pizza的过程。类如下:
public abstract class Pizza {
protected String name;
//不同步pizza材料不一样 因此写成抽象的方法
public abstract void prepare();
//准备烘烤 不同步pizza材料不一样 因此写成抽象的方法
public void bake(){
System.out.println(name+"baking...");
}
//准备切
public void cut(){
System.out.println(name+"cuting...");
}
//准备打包
public void box(){
System.out.println(name+"boxing...");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
接着,我们分别创建奶酪pizza和希腊pizza的类。
public class CheesePizza extends Pizza{
@Override
public void prepare() {
System.out.println("准备制作奶酪pizza的材料");
}
}
public class GreekPizza extends Pizza{
@Override
public void prepare() {
System.out.println("给希腊pizza 准备材料");
}
}
然后我们将订pizza和制作pizza的类单独拿出来
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 {
break;
}
//输出pizza制作的过程
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}while(true);
}
private String getType(){
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入pizza类型: ");
String str = null;
try {
str = bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
测试类如下:
public class PizzaStore {
public static void main(String[] args) {
OrderPizza orderPizza = new OrderPizza();
}
}
测试结果如下:
请输入pizza类型:
cheese
准备制作奶酪pizza的材料
奶酪披萨baking...
奶酪披萨cuting...
奶酪披萨boxing...
请输入pizza类型:
======================================================================================================================================================================
以上是我们正常的思维开发。如何改造成简单工厂呢?
首先pizza类,CheesePizza类,GreekPizza类 不变,我们增加一个工厂类
//简单功能工厂类
public class SimpleFactory {
//根据orderType 返回对应的pizza对象
public Pizza cretePizza(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("奶酪披萨");
}
return pizza;
}
}
然后将这个工厂类组合订购pizza的类(我们新写一个订购pizza的类)
public class OrderPizza02 {
//
public OrderPizza02(SimpleFactory simpleFactory) {
setSimpleFactory(simpleFactory);
}
SimpleFactory simpleFactory;
Pizza pizza = null;
public void setSimpleFactory(SimpleFactory simpleFactory) {
String orderType = "";
this.simpleFactory = simpleFactory;
do {
orderType = getType();
pizza = this.simpleFactory.cretePizza(orderType);
if (pizza != null){
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else{
System.out.println("订购pizza 失败");
break;
}
}while(true);
}
private String getType(){
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入pizza类型: ");
String str = null;
try {
str = bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
测试类如下:
public class PizzaStoreFactory {
public static void main(String[] args) {
new OrderPizza02(new SimpleFactory());
System.out.println("退出程序");
}
}
测试结果如下:
请输入pizza类型:
greek
使用简单工厂模式
给希腊pizza 准备材料
希腊披萨baking...
希腊披萨cuting...
希腊披萨boxing...
请输入pizza类型:
这就是简单工厂模式的写法。