介绍
是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
应用场景
(1)系统需要为用户提供多个对象,但不希望用户直接用new运算符实例化这些对象,即希望用户和创建对象脱耦。
(2)系统需要为用户提供一系列对象,但只需要知道这些对象有哪些方法可用,不需要用户知道这些对象的创建过程。
模式结构
(1)抽象产品(Product):一个抽象类或接口,负责定义具体产品必须实现的方法;
(2)具体产品(ConcreteProduct):具体产品是一个类,实现抽象产品的方法。
(3)抽象工厂(AbstractFactory):一个接口或抽象类,负责定义若干个抽象方法;
(4)具体工厂(ConcreteFactory):具体工厂重写抽象工厂中的抽象方法。
代码案例
(1)抽象产品(裤子和上衣接口)
public interface UpperClothes {
public abstract int getChestSize();
public abstract int getHeight();
public abstract String getName();
}
public interface Trousers {
public abstract int getWaistSize();
public abstract int getHeight();
public abstract String getName();
}
(2)具体产品
分别是西服上衣、牛仔上衣、西装裤子和牛仔裤。
public class WesternUpperClothes implements UpperClothes {
private int chestSize;
private int height;
private String name;
WesternUpperClothes(String name , int chestSize , int height){
this.name = name;
this.chestSize = chestSize;
this.height = height;
}
@Override
public int getChestSize() {
return chestSize;
}
@Override
public int getHeight() {
return height;
}
@Override
public String getName() {
return name;
}
}
public class CowboyUpperClothes implements UpperClothes {
private int chestSize;
private int height;
private String name;
CowboyUpperClothes(String name , int chestSize , int height){
this.name = name;
this.chestSize = chestSize;
this.height = height;
}
@Override
public int getChestSize() {
return chestSize;
}
@Override
public int getHeight() {
return height;
}
@Override
public String getName() {
return name;
}
}
public class WesternTrousers implements Trousers {
private int waistSize;
private int height;
private String name;
WesternTrousers(String name , int waistSize , int height){
this.name = name;
this.waistSize = waistSize;
this.height = height;
}
@Override
public int getWaistSize() {
return waistSize;
}
@Override
public int getHeight() {
return height;
}
@Override
public String getName() {
return name;
}
}
public class CowboyTrousers implements Trousers {
private int waistSize;
private int height;
private String name;
CowboyTrousers(String name , int waistSize , int height){
this.name = name;
this.waistSize = waistSize;
this.height = height;
}
@Override
public int getWaistSize() {
return waistSize;
}
@Override
public int getHeight() {
return height;
}
@Override
public String getName() {
return name;
}
}
(3)抽象工厂
public abstract class ClothesFactory {
public abstract UpperClothes createUpperClothes(int chestSize , int height);
public abstract Trousers createTrousers(int waistSize , int height);
}
(4)具体工厂(北京工厂和上海工厂)
public class BeijingClothesFactory extends ClothesFactory {
@Override
public UpperClothes createUpperClothes(int chestSize, int height) {
return new WesternUpperClothes("北京牌西服上衣",chestSize,height);
}
@Override
public Trousers createTrousers(int waistSize, int height) {
return new WesternTrousers("北京牌西服裤子",waistSize,height);
}
}
public class ShanghaiClothesFactory extends ClothesFactory {
@Override
public UpperClothes createUpperClothes(int chestSize, int height) {
return new CowboyUpperClothes("上海牌牛仔上衣",chestSize,height);
}
@Override
public Trousers createTrousers(int waistSize, int height) {
return new CowboyTrousers("上海牌牛仔裤子",waistSize,height);
}
}
(5)模式的使用(店铺类)
public class Shop {
private UpperClothes cloth;
private Trousers trouser;
public void giveSuit(ClothesFactory factory , int chestSize , int waistSize , int height){
cloth = factory.createUpperClothes(chestSize, height);
trouser = factory.createTrousers(waistSize, height);
showMess();
}
private void showMess(){
System.out.println("<套装信息>");
System.out.println(cloth.getName()+":");
System.out.println("胸围:"+cloth.getChestSize());
System.out.println("身高:"+cloth.getHeight());
System.out.println(trouser.getName()+":");
System.out.println("腰围:"+trouser.getWaistSize());
System.out.println("身高:"+trouser.getHeight());
}
}
main方法:
public static void main(String args[]){
Shop shop = new Shop();
ClothesFactory factory = new BeijingClothesFactory();
shop.giveSuit(factory, 110,82,170);
factory = new ShanghaiClothesFactory();
shop.giveSuit(factory, 120, 88, 180);
}
优点
当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。
抽象工厂可以为用户创建一系列相关的对象,使用户和创建这些对象的类脱耦。
在抽象工厂模式中,可以随时增加“具体工厂”为用户提供一组相关的对象。
缺点
产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。