简单工厂是一种编程习惯,把变化的代码(new 对象)封装在一个工厂里
用户需求:客户想要在pizza店orderPizza
需求分析:
类图设计
代码示例
PizzaStore代码
package Factory.SimpleFactory;
public class PizzaStore {
SimplePizzaFactory factory;
public PizzaStore(SimplePizzaFactory factory) {
this.factory = factory;
}
public Pizza orderPizza(String type) {
Pizza pizza;
pizza = factory.createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}
SimplerPizzaFactory代码
package Factory.SimpleFactory;
public class SimplePizzaFactory {
public Pizza createPizza(String type) {
Pizza pizza = null;
switch (type) {
case "cheese":
pizza = new CheesePizza();
break;
case "clam":
pizza = new ClamPizza();
break;
case "veggie":
pizza = new VeggiePizza();
break;
default:
break;
}
return pizza;
}
}
这就是简单工厂,用一个新的对象(SimplePizzaFactory)封装创建对象的代码,这个新的对象就是“工厂”。
工厂方法模式定义了一个创建对象的接口,但由于子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类
依然是上面的场景
用户需求:不只有一个pizza店,有很多家,不同客户order不同种类的pizza
需求分析:
类图设计:
代码示例:
Pizza代码
package Factory.FactoryMethod;
import java.util.ArrayList;
public abstract class Pizza {
String name;
String dough;
String sauce;
ArrayList toppings = new ArrayList();
void prepare() {
System.out.println("Preparing " + name);
System.out.println("Tossing dough...");
System.out.println("Adding sauce...");
System.out.println("Adding topping: ");
for (int i = 0; i < toppings.size(); i++) {
System.out.println(" " + toppings.get(i));
}
}
void bake() {
System.out.println("Bake for 25 mins at 350");
}
void cut() {
System.out.println("Cutting the pizza into diagonal slices");
}
void box() {
System.out.println("Place pizza in official PizzaStore box");
}
public String getName() {
return name;
}
}
NYStyleCheesePizza代码
package Factory.FactoryMethod;
public class NYStyleCheesePizza extends Pizza {
public NYStyleCheesePizza() {
name = "NY style Sauce and Cheese Pizza";
dough = "Thin Crust Dough";
sauce = "Marinara Sauce";
toppings.add("Grated Reggiano Cheese");
}
}
ChicagoStyleCheesePizza代码,重写了cut方法
package Factory.FactoryMethod;
public class ChicagoStyleCheesePizza extends Pizza {
public ChicagoStyleCheesePizza() {
name = "Chicago Style Deep Dish Cheese Pizza";
dough = "Extra Thick Crust Dough";
sauce = "Plum Tomato Sauce";
toppings.add("Shredded Mozzarella Cheese");
}
@Override
public void cut() {
System.out.println("Cutting the Pizza into square slices");
}
}
PizzaStore 代码
package Factory.FactoryMethod;
public abstract class PizzaStore {
public Pizza orderPizza(String type) {
Pizza pizza;
pizza = createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
protected abstract Pizza createPizza(String type);
}
ChicagoPizzaStore 代码
package Factory.FactoryMethod;
public class ChicagoPizzaStore extends PizzaStore {
@Override
protected Pizza createPizza(String type) {
Pizza pizza = null;
switch (type) {
case "cheese":
pizza = new ChicagoStyleCheesePizza();
break;
default:
pizza = null;
}
return pizza;
}
}
NYPizzaStore代码
package Factory.FactoryMethod;
public class NYPizzaStore extends PizzaStore {
@Override
protected Pizza createPizza(String type) {
Pizza pizza=null;
switch (type){
case "cheese":
pizza=new NYStyleCheesePizza();
break;
default:
pizza=null;
}
return pizza;
}
}
测试类代码
package Factory.FactoryMethod;
public class PizzaTestDrive {
public static void main(String[] args) {
PizzaStore nyStore = new NYPizzaStore();
PizzaStore chicagoStore = new ChicagoPizzaStore();
Pizza pizza = nyStore.orderPizza("cheese");
System.out.println("wpp ordered a " + pizza.getName());
pizza = chicagoStore.orderPizza("cheese");
System.out.println("ypp ordered a " + pizza.getName());
}
}
小结:
抽象工厂模式提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类
类图设计:
代码示例:
package Factory.AbstractFactory;
public interface AbstractFactory {
public AbstractProductA ProductA();
public AbstractProductB ProductB();
}
package Factory.AbstractFactory;
public class ConcreteFactory1 implements AbstractFactory {
@Override
public AbstractProductA ProductA() {
return new ProductA1();
}
@Override
public AbstractProductB ProductB() {
return new ProductB1();
}
}
具体应用场景:
依然是上面的pizza店,要生产pizza产品就需要原料,有面团、酱料、芝士、海鲜作料,这每一种原料都可以看做是工厂方法模式中的一种产品对象(如,不同的面料就会有不同的面团,番茄酱和芝麻酱就是不同的酱料,也就是不同的具体对象),而这些产品对象组合起来才是一个抽象工厂模式下的一个完整产品pizza。抽象工厂模式更关注的是多维度的产品家族,是对象组合为一个完整的产品,这里就不贴具体代码了,感兴趣的同学可以自己搞一搞。
小结:
总结: