工厂模式包含三种模式:简单工厂模式、工厂方法模式和抽象工厂模式。
简单工厂模式
- 定义
简单工厂模式:由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类(这些产品类继承自一个父类或接口)的实例。
要素:工厂(Creator)、抽象产品(Product)、具体产品(Concrete Product)
工厂:负责实现创建所有实例的内部逻辑。工厂类的创建产品类的方法可以被外界直接调用,创建所需的产品对象。
抽象产品:所创建的所有产品对象的父类,它负责描述所有实例共有的公共接口。
具体产品:创建目标。 - 类图
- Class
// 披萨:抽象的产品或抽象的算法
// 此抽象类提供了某些默认的基本做法
public abstract class Pizza {
public static final int CheesePizza = 1;
public static final int ClamPizza = 2;
public String name;
protected void prepare(){
System.out.println("Preparing " + name);
}
protected void bake(){
System.out.println("Bake for 25 minutes.");
}
protected void cut(){
System.out.println("Cutting the pizza into diagonal slices.");
}
protected void box(){
System.out.println("Place pizza in official PizzaStore box.");
}
}
// 奶酪披萨
public class CheesePizza extends Pizza {
public CheesePizza(){
this.name = "Cheese Pizza";
}
}
// 蛤蜊披萨
public class ClamPizza extends Pizza {
public ClamPizza(){
this.name = "Clam Pizza";
}
}
// 工厂类
public class SimplePizzaFactory {
public static Pizza createPizza(int pizzaType){
Pizza pizza = null;
switch (pizzaType){
case Pizza.CheesePizza:
pizza = new CheesePizza();
break;
case Pizza.ClamPizza:
pizza = new ClamPizza();
break;
}
return pizza;
}
}
// 披萨商店 :可看做客户端
// 对客户端来说,去除了与具体产品的依赖。
public class PizzaStore {
public static Pizza orderPizza(int pizzaType){
Pizza pizza;
pizza = SimplePizzaFactory.createPizza(pizzaType);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}
- 测试
// 订购一份奶酪披萨
PizzaStore.orderPizza(Pizza.CheesePizza);
// 订购一份蛤蜊披萨
PizzaStore.orderPizza(Pizza.ClamPizza);
工厂方法模式
- 定义
工厂方法模式:定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类。 - 类图
- Class
// 创建者通常会包含依赖于抽象产品的代码,而具体产品由子类制造,创建者不需要知道在制造哪种具体产品。
public abstract class PizzaStore {
public Pizza orderPizza(int pizzaType){
Pizza pizza;
pizza = createPizza(pizzaType);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
// 工厂方法用来处理对象的创建,并将这样的行为封装在子类中。
public abstract Pizza createPizza(int pizzaType);
}
// 纽约披萨店(具体创建者)
public class NewYorkPizzaStore extends PizzaStore {
// 实现createPizza方法,创建纽约本地风味的披萨。
@Override
public Pizza createPizza(int pizzaType) {
Pizza pizza = null;
switch (pizzaType){
case Pizza.NewYork_CheesePizza:
pizza = new NewYorkCheesePizza();
break;
case Pizza.NewYork_ClamPizza:
pizza = new NewYorkClamPizza();
break;
}
return pizza;
}
}
// 芝加哥披萨店(具体创建者)
public class ChicagoPizzaStore extends PizzaStore {
// 实现createPizza方法,创建芝加哥本地风味的披萨。
@Override
public Pizza createPizza(int pizzaType) {
Pizza pizza = null;
switch (pizzaType){
case Pizza.Chicago_CheesePizza:
pizza = new ChicagoCheesePizza();
break;
case Pizza.Chicago_ClamPizza:
pizza = new ChicagoClamPizza();
break;
}
return pizza;
}
}
// 披萨:抽象的产品或抽象的算法
// 此抽象类提供了某些默认的基本做法
public abstract class Pizza {
public static final int NewYork_CheesePizza = 1;
public static final int NewYork_ClamPizza = 2;
public static final int Chicago_CheesePizza = 3;
public static final int Chicago_ClamPizza = 4;
public String name;
protected void prepare(){
System.out.println("Preparing " + name);
}
protected void bake(){
System.out.println("Bake for 25 minutes.");
}
protected void cut(){
System.out.println("Cutting the pizza into diagonal slices.");
}
protected void box(){
System.out.println("Place pizza in official PizzaStore box.");
}
}
// 芝加哥奶酪披萨(具体产品)
public class ChicagoCheesePizza extends Pizza {
public ChicagoCheesePizza(){
this.name = "Chicago style cheese pizza";
}
}
// 芝加哥蛤蜊披萨(具体产品)
public class ChicagoClamPizza extends Pizza {
public ChicagoClamPizza(){
this.name = "Chicago style clam pizza";
}
}
// 纽约奶酪披萨(具体产品)
public class NewYorkCheesePizza extends Pizza {
public NewYorkCheesePizza(){
this.name = "New York style cheese pizza";
}
}
// 纽约蛤蜊披萨(具体产品)
public class NewYorkClamPizza extends Pizza {
public NewYorkClamPizza() {
this.name = "New York clam pizza";
}
}
- 测试
PizzaStore newYorkPizzaStore = new NewYorkPizzaStore();
PizzaStore chicagoPizzaStore = new ChicagoPizzaStore();
// 订购一份纽约风味的奶酪披萨
newYorkPizzaStore.orderPizza(Pizza.NewYork_CheesePizza);
// 订购一份芝加哥风味的蛤蜊披萨
chicagoPizzaStore.orderPizza(Pizza.Chicago_ClamPizza);
抽象工厂模式
- 定义
抽象工厂:提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
抽象工厂允许客户使用抽象的接口来创建一组相关的产品,而不需要知道(或关心)实际产出的具体产品是什么。这样一来,客户就从具体的产品中被解耦。 - 类图
- Class
// 为原料工厂定义一个接口,这个接口负责创建所有的原料。
public interface PizzaIngredientFactory {
// 生产面团
public Dough createDough();
// 生产酱汁
public Sauce createSauce();
}
// 纽约披萨原料工厂
public class NewYorkPizzaIngredientFactory implements PizzaIngredientFactory {
@Override
public Dough createDough() {
return new ThinCrustDough();
}
@Override
public Sauce createSauce() {
return new MarinaraSauce();
}
}
// 芝加哥披萨原料工厂
public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {
@Override
public Dough createDough() {
return new ThickCrustDough();
}
@Override
public Sauce createSauce() {
return new PlumTomatoSauce();
}
}
// 面团(原料)
public abstract class Dough {
public String name;
}
// 酱汁(原料)
public abstract class Sauce {
public String name;
}
// 薄脆面团
public class ThinCrustDough extends Dough {
public ThinCrustDough(){
this.name = "New york style thin crust dough";
}
}
// 厚饼面团
public class ThickCrustDough extends Dough {
public ThickCrustDough(){
this.name = "Chicago style thick crust dough";
}
}
// 意大利红酱
public class MarinaraSauce extends Sauce {
public MarinaraSauce(){
this.name = "New york style marinara sauce";
}
}
// 番茄酱
public class PlumTomatoSauce extends Sauce {
public PlumTomatoSauce(){
this.name = "Chicago style plum tomato sauce";
}
}
// 披萨:抽象的产品或抽象的算法
// 此抽象类提供了某些默认的基本做法
public abstract class Pizza {
public static final int NewYork_CheesePizza = 1;
public static final int NewYork_ClamPizza = 2;
public static final int Chicago_CheesePizza = 3;
public static final int Chicago_ClamPizza = 4;
public String name;
public Dough dough;
public Sauce sauce;
public void prepare(){
System.out.println("Preparing " + name);
}
public void bake(){
System.out.println("Bake for 25 minutes.");
}
public void cut(){
System.out.println("Cutting the pizza into diagonal slices.");
}
public void box(){
System.out.println("Place pizza in official PizzaStore box.");
}
}
// 芝加哥奶酪披萨
public class ChicagoCheesePizza extends Pizza {
private PizzaIngredientFactory ingredientFactory;
public ChicagoCheesePizza(PizzaIngredientFactory ingredientFactory){
this.name = "Chicago style cheese pizza";
this.ingredientFactory = ingredientFactory;
}
@Override
public void prepare() {
super.prepare();
// 生产面团
this.dough = ingredientFactory.createDough();
// 生产酱汁
this.sauce = ingredientFactory.createSauce();
System.out.println("Preparing " + dough.name);
System.out.println("Preparing " + sauce.name);
}
}
// 纽约奶酪披萨
public class NewYorkCheesePizza extends Pizza {
private PizzaIngredientFactory ingredientFactory;
public NewYorkCheesePizza(PizzaIngredientFactory ingredientFactory){
this.name = "New York style cheese pizza";
this.ingredientFactory = ingredientFactory;
}
@Override
public void prepare() {
super.prepare();
// 生产面团
this.dough = ingredientFactory.createDough();
// 生产酱汁
this.sauce = ingredientFactory.createSauce();
System.out.println("Preparing " + dough.name);
System.out.println("Preparing " + sauce.name);
}
}
- 测试
// 创建纽约原料工厂
PizzaIngredientFactory pizzaIngredientFactory = new NewYorkPizzaIngredientFactory();
// 创建纽约风味披萨
Pizza newYorkCheesePizza = new NewYorkCheesePizza(pizzaIngredientFactory);
newYorkCheesePizza.prepare();
// 创建芝加哥原料工厂
PizzaIngredientFactory ingredientFactory = new ChicagoPizzaIngredientFactory();
// 创建芝加哥风味披萨
Pizza chicagoCheesePizza = new ChicagoCheesePizza(ingredientFactory);
chicagoCheesePizza.prepare();