2 创建型之抽象工厂模式

概念

提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。抽象工厂模式又称为Kit模式。

解释

抽象工厂模式是工厂方法模式的演进。工厂方法模式是创建单个产品,终端消费的是一个个的单个产品。抽象工厂模式是创建多个有关联的产品,终端消费的是"产品族"。所以在产品结构设计未稳定时或者产品之间关联性不强而不能形成"产品族"时,不推荐引入此模式。

例子

定义两类产品水果和盘子,它们的接口和具体的实现。定义果盘(水果和盘子组合)工厂接口与具体的工厂来生产果盘。在定义一个果盘工厂构造类,通过枚举值来生成不同的果盘工厂。

代码示例:

//水果接口与实现
public interface Fruit {
    String description();
}
public class BlackApple implements Fruit{
    @Override
    public String description() {
        return "this is black apple";
    }
}
public class WhitePear implements Fruit{
    @Override
    public String description() {
        return "this is white pear";
    }
}

//盘子接口与实现
public interface Plate {
    String description();
}
public class BlackPlate implements Plate{
    @Override
    public String description() {
        return "this is black plate";
    }
}
public class WhitePlate implements Plate{
    @Override
    public String description() {
        return "this is white plate";
    }
}

//果盘接口与实现
public interface FruitPlateFactory {
    Fruit createFruit();
    Plate createPlate();
}
public class BlackFruitPlateFactory implements FruitPlateFactory{
    @Override
    public Fruit createFruit() {
        return new BlackApple();
    }
    @Override
    public Plate createPlate() {
        return new BlackPlate();
    }
}
public class WhiteFruitPlateFactory implements FruitPlateFactory{
    @Override
    public Fruit createFruit() {
        return new WhitePear();
    }
    @Override
    public Plate createPlate() {
        return new WhitePlate();
    }
}
    //定义枚举类与构造方法,通过不同的枚举值生成不同的果盘工厂
    public static class FruitPlateFactoryMaker {
        public enum FruitPlateFactoryType {
            WHITE, BLACK
        }
        public static FruitPlateFactory makeFactory(FruitPlateFactoryType type) {
            switch (type) {
                case BLACK:
                    return new BlackFruitPlateFactory();
                case WHITE:
                    return new WhiteFruitPlateFactory();
                default:
                    throw new IllegalArgumentException("FruitPlateType not supported.");
            }
        }
    }

    public static void main(String[] args){
        FruitPlateFactory fruitPlateFactory = FruitPlateFactoryMaker.makeFactory(FruitPlateFactoryMaker.FruitPlateFactoryType.BLACK);
        Fruit fruit = fruitPlateFactory.createFruit();
        Plate plate = fruitPlateFactory.createPlate();
        System.out.println(fruit.description());
        System.out.println(plate.description());

        fruitPlateFactory = FruitPlateFactoryMaker.makeFactory(FruitPlateFactoryMaker.FruitPlateFactoryType.WHITE);
        fruit = fruitPlateFactory.createFruit();
        plate = fruitPlateFactory.createPlate();
        System.out.println(fruit.description());
        System.out.println(plate.description());
    }

//结果输出
this is black apple
this is black plate
this is white pear
this is white plate

适用性

  • 系统不应当依赖于产品类实例如何被创建、组合和表达。
  • 系统可以配置成使用多个产品族中的某一个,可以切换配置来使用不同的产品族。
  • 属于同一个产品族的产品需要约束在一起使用,而不能分开。
  • 产品等级结构稳定,设计完成之后,不会向系统中增加新的产品等级结构或者删除已有的产品等级结构。

真实案例

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容