创建型-抽象工厂
角色
- 抽象工厂: 声明创建一个族产品对象的方法,每个方法对应一中产品,抽象工厂可以是接口,也可以是抽象类;
- 具体工厂: 实现抽象工厂接口,复杂创建具体的一族产品;
- 抽象产品:定义一类产品的公共接口,描述一类产品的特定行为或者属性;
- 具体产品:实现抽象产品接口,定义具体产品的特定行为和属性;
其中,一族的概念可根据业务情况进行分族。比如:形状,颜色可分为两个族,再比如,植物, 动物也可以分为两个族。族的概念根据区分维度不同可分为不同的族;
图解
实现案例
这个案例中按照年龄将人分为小孩,老年人,中年人三族,产品分为上衣、裤子、鞋子三个种类;其中实现了老年人和小孩两个族衣服的抽象工厂。
抽象产品类:
java
/** 上衣抽象类*/
public abstract class Jacket {
public abstract void wearJacket();
}
/** 鞋子抽象类*/
public abstract class Shoes {
public abstract void wearShoes();
}
/** 裤子抽象类*/
public abstract class Trousers {
public abstract void wearTrousers();
}
具体产品类:
java
/** 小孩衣服的具体类*/
public class ChildishJacket extends Jacket {
@Override
public void wearJacket() {
System.out.println("小孩的上衣");
}
}
public class ChildishShoes extends Shoes {
@Override
public void wearShoes() {
System.out.println("小孩的鞋子");
}
}
public class ChildishTrousers extends Trousers {
@Override
public void wearTrousers() {
System.out.println("小孩的裤子");
}
}
/** 老年人衣服的具体类*/
public class OldJacket extends Jacket {
@Override
public void wearJacket() {
System.out.println("老年人上衣");
}
}
public class OldTrousers extends Trousers {
@Override
public void wearTrousers() {
System.out.println("老年人的裤子");
}
}
public class OldShoes extends Shoes {
@Override
public void wearShoes() {
System.out.println("老年人的鞋子");
}
}
抽象工厂类:
java
/** 生产衣服的抽象工厂*/
public abstract class AbstractClothesFactory {
public abstract Jacket createJacket();
public abstract Shoes createShoes();
public abstract Trousers createTrousers();
}
具体工厂类:
java
/** 儿童衣服生产工厂*/
public class ChildishClothesFactory extends AbstractClothesFactory {
private static ChildishClothesFactory INSTANCE = new ChildishClothesFactory();
public static ChildishClothesFactory getInstance(){
return INSTANCE;
}
@Override
public Jacket createJacket() {
return new ChildishJacket();
}
@Override
public Shoes createShoes() {
return new ChildishShoes();
}
@Override
public Trousers createTrousers() {
return new ChildishTrousers();
}
}
/** 老年人衣服生产工厂*/
public class OldClothesFactory extends AbstractClothesFactory{
private static OldClothesFactory INSTANCE = new OldClothesFactory();
public static OldClothesFactory getInstance(){
return INSTANCE;
}
@Override
public Jacket createJacket() {
return new OldJacket();
}
@Override
public Shoes createShoes() {
return new OldShoes();
}
@Override
public Trousers createTrousers() {
return new OldTrousers();
}
}
使用:
java
public class Main {
public static void main(String[] args) {
//生产儿童一族的衣服
ChildishClothesFactory childishClothesFactory = ChildishClothesFactory.getInstance();
Jacket childishJacket = childishClothesFactory.createJacket();
Shoes childishShoes = childishClothesFactory.createShoes();
Trousers childishTrousers = childishClothesFactory.createTrousers();
//生产老年人一族的衣服
OldClothesFactory oldClothesFactory = OldClothesFactory.getInstance();
Jacket oldJacket = oldClothesFactory.createJacket();
Shoes oldShoes = oldClothesFactory.createShoes();
Trousers oldTrousers = oldClothesFactory.createTrousers();
childishJacket.wearJacket();
oldJacket.wearJacket();
}
}
总结
- 抽象工厂是生产一族的产品,族的概念可根据不同的业务规则区分。
- 扩展产品族非常困难。增加一个新的产品族需要修改抽象工厂和所有具体工厂的代码。
- 对于新增族来说,满足开闭原则,而对于新增产品来说,不满足开闭原则。