1.先参看简单工厂模式
https://blog.csdn.net/u013294097/article/details/100299807
2.案例详解:
1)看一个新的需求:
披萨项目不仅仅是有多品种,还有不同的地域,也就是有不同口味,和不同的地域
2)思路分析:
①使用简单工厂模式,创建不同的工厂类,比如BJPizzaSimpleFactory、LDPizzaSimpleFactory等等,从上一个案例来说,也是可以的。但是考虑到项目的规模,以及软件的可维护性,可扩展性并不是特别好。
②使用工厂方法模式
3)工厂方法模式介绍:
①工厂方法模式设计方案:将披萨项目的实例化功能抽象成抽象方法,在不同的口味点餐子类中具体实现
②工厂方法模式:定义了一个创建对象的抽象方法,由子类决定要实例化的类。工厂方法模式将对象的实例化推迟到子类。
4)应用案例:
UML类图如下:
代码结构如下:
上代码:
Pizza
package factory.factorymethod.pizzastore;
public abstract class Pizza {
protected String name;
public abstract void prepare();
public void bake(){
System.err.println(name + "baking;");
}
public void cut(){
System.err.println(name + "cutting;");
}
public void box(){
System.err.println(name + "boxing;");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
BJCheesePizza
package factory.factorymethod.pizzastore;
public class BJCheessPizza extends Pizza {
@Override
public void prepare() {
setName("BJCheessPizza");
System.err.println(name + "preparing;");
}
}
BJGreekPizza
package factory.factorymethod.pizzastore;
public class BJGreekPizza extends Pizza {
@Override
public void prepare() {
setName("BJGreekPizza");
System.err.println(name + "preparing;");
}
}
LDCheesePizza
package factory.factorymethod.pizzastore;
public class LDCheessPizza extends Pizza {
@Override
public void prepare() {
setName("LDCheessPizza");
System.err.println(name + "preparing;");
}
}
LDGreekPizza
package factory.factorymethod.pizzastore;
public class LDGreekPizza extends Pizza {
@Override
public void prepare() {
setName("LDGreekPizza");
System.err.println(name + "preparing;");
}
}
OrderPizza
package factory.factorymethod.pizzastore.Order;
import factory.factorymethod.pizzastore.Pizza;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public abstract class OrderPizza {
//定义一个抽象方法,createPizza,让各个工厂子类自己实现
abstract Pizza createPizza(String orderType);
Pizza pizza = null;
//构造器
public OrderPizza(){
setS();
}
public void setS() {
String orderType = "";//用户输入的
do{
orderType = getType();
Pizza pizza = createPizza(orderType);//抽象方法,由工厂子类完成
if(pizza != null) {
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else{
System.err.println("订购pizza失败,没有这种pizza!");
}
}while (true);
}
private String getType(){
try{
BufferedReader string = new BufferedReader(new InputStreamReader(System.in));
System.err.println("input pizza type:");
String str = string.readLine();
return str;
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
}
BJOrderPizza
package factory.factorymethod.pizzastore.Order;
import factory.factorymethod.pizzastore.BJCheessPizza;
import factory.factorymethod.pizzastore.BJGreekPizza;
import factory.factorymethod.pizzastore.Pizza;
public class BJOrderPizza extends OrderPizza {
@Override
Pizza createPizza(String orderType) {
Pizza pizza = null;
if(orderType.equals("cheese")){
pizza = new BJCheessPizza();
}else if(orderType.equals("greek")){
pizza = new BJGreekPizza();
}
return pizza;
}
}
LDOrderPizza
package factory.factorymethod.pizzastore.Order;
import factory.factorymethod.pizzastore.*;
public class LDOrderPizza extends OrderPizza {
@Override
Pizza createPizza(String orderType) {
Pizza pizza = null;
if(orderType.equals("cheese")){
pizza = new LDCheessPizza();
}else if(orderType.equals("greek")){
pizza = new LDGreekPizza();
}
return pizza;
}
}
测试方法:有两种方式,一种是直接订购,一种是根据地域订购
package factory.factorymethod.pizzastore;
import factory.factorymethod.pizzastore.Order.BJOrderPizza;
import factory.factorymethod.pizzastore.Order.LDOrderPizza;
public class Main {
// public static void main(String[] args) {
// //创建北京口味的各种Pizza
// new BJOrderPizza();
// }
//使用统一方法
public static void main(String[] args) {
String local = "bj";
if(local.equals("bj")){
new BJOrderPizza();
}else{
new LDOrderPizza();
}
}
}