抽象工厂

抽象工厂模式属于创建型模式

定义:为创建一组相关或相互依赖的对象提供一个接口,而且无须指定它们的具体类

抽象工厂模式是对工厂方法模式的扩展,抽象工厂比工厂模式更为抽象,工厂方法模式针对产品等级结构,而抽象工厂针对产品族。

产品族与产品等级结构的概念:

​ 产品族,是指位于不同产品等级结构中,功能相关联的产品组成的家族,比如游戏工厂生产射击类和塔防类两种产品,任天堂的射击类游戏和塔防类游戏为一个产品族,腾讯的射击类游戏和塔防类游戏为一个产品族

​ 产品等级结构,一个产品族由多个产品等级结构组成,射击类游戏是一个产品等级结构,塔防类游戏也是一个产品等级结构

模式类图

以游戏为例,定义一个抽象工厂,生产射击和塔防两种游戏,有两个具体的生产工厂,任天堂和腾讯,两个工厂生产各自品牌的两类游戏产品

角色

  1. 抽象工厂:GameFactory,规定了生成射击类和塔防类两种游戏
  2. 具体工厂:NintendoGameFactoryTencentGameFactory,负责生产各自品牌的射击类和塔防类游戏
  3. 抽象产品:GameableShootGameTowerDefenceGame是抽象类,实现Gameable
  4. 具体产品:NintendoShootGameNintendoTowerDefenceGameTencentShootGameTencentTowerDefenceGame

优点

  1. 接口和实现分离,客户端面向接口编程,不用关心具体实现,从具体的产品实现中解耦
  2. 增加新的具体工厂和产品族方便,切换产品族方便

缺点

不易增加新的产品,如果要增加新的产品需要抽象工厂和所有具体工厂

模式代码实现

源码地址:https://github.com/mingyuHub/design-patterns

GameFactory

抽象工厂,规定了生产射击和塔防两类游戏

/**
 * @author: chenmingyu
 * @date: 2019/2/14 11:29
 * @description: 工厂类
 */
public interface GameFactory {

    /**
     * 创建射击游戏
     * @return
     */
    Gameable createShootGame();

    /**
     * 创建塔防游戏
     * @return
     */
    Gameable createTowerDefenceGame();
}
NintendoGameFactory

具体工厂,负责生产任天堂的射击类和塔防类游戏

/**
 * @author: chenmingyu
 * @date: 2019/2/14 18:20
 * @description: 任天堂游戏制造厂
 */
public class NintendoGameFactory implements GameFactory{

    @Override
    public Gameable createShootGame() {
        return new NintendoShootGame();
    }

    @Override
    public Gameable createTowerDefenceGame() {
        return new NintendoTowerDefenceGame();
    }
}
TencentGameFactory

具体工厂,负责生产腾讯的射击类和塔防类游戏

/**
 * @author: chenmingyu
 * @date: 2019/2/14 18:20
 * @description: 腾讯游戏制造厂
 */
public class TencentGameFactory implements GameFactory {

    @Override
    public Gameable createShootGame() {
        return new TencentShootGame();
    }

    @Override
    public Gameable createTowerDefenceGame() {
        return new TencentTowerDefenceGame();
    }
}
Gameable

抽象产品,所有游戏产品均实现该接口

/**
 * @author: chenmingyu
 * @date: 2019/2/14 11:19
 * @description: 游戏接口
 */
public interface Gameable {

    /**
     * 校验账户信息
     * @param nickName
     */
    void validateAccount(String nickName);


    /**
     * 游戏类型
     */
    void getGameType();
}
ShootGame和TowerDefenceGame

抽象类,实现Gameable接口

/**
 * @auther: chenmingyu
 * @date: 2019/2/14 11:26
 * @description: 射击类游戏
 */
public abstract class ShootGame implements Gameable{

    @Override
    public void validateAccount(String nickName) {
        System.out.println("射击游戏校验昵称:"+nickName);
    }

}
/**
 * @auther: chenmingyu
 * @date: 2019/2/14 11:28
 * @description: 塔防类游戏
 */
public abstract class TowerDefenceGame implements Gameable{

    @Override
    public void validateAccount(String nickName) {
        System.out.println("塔防游戏校验昵称:"+nickName);
    }

}
具体产品

共四款游戏产品:NintendoShootGameNintendoTowerDefenceGameTencentShootGameTencentTowerDefenceGame

/**
 * @author: chenmingyu
 * @date: 2019/2/15 16:57
 * @description: 任天堂射击游戏
 */
public class NintendoShootGame extends ShootGame{

    @Override
    public void getGameType() {
        System.out.println("任天堂射击游戏");
    }
}
/**
 * @author: chenmingyu
 * @date: 2019/2/15 17:18
 * @description: 任天堂塔防游戏
 */
public class NintendoTowerDefenceGame extends TowerDefenceGame{

    @Override
    public void getGameType() {
        System.out.println("任天堂塔防游戏");
    }
}
/**
 * @author: chenmingyu
 * @date: 2019/2/15 16:55
 * @description: 腾讯射击游戏
 */
public class TencentShootGame extends ShootGame {

    @Override
    public void getGameType() {
        System.out.println("腾讯射击游戏");
    }
}
/**
 * @author: chenmingyu
 * @date: 2019/2/15 17:17
 * @description: 腾讯塔防游戏
 */
public class TencentTowerDefenceGame extends TowerDefenceGame{

    @Override
    public void getGameType() {
        System.out.println("腾讯塔防游戏");
    }
}
验证
public static void main(String[] args) throws Exception{

    NintendoGameFactory nintendoGameFactory = new NintendoGameFactory();
    nintendoGameFactory.createShootGame().getGameType();
    nintendoGameFactory.createTowerDefenceGame().getGameType();

    TencentGameFactory tencentGameFactory = new TencentGameFactory();
    tencentGameFactory.createShootGame().getGameType();
    tencentGameFactory.createTowerDefenceGame().getGameType();
}

输出

任天堂射击游戏
任天堂塔防游戏
腾讯射击游戏
腾讯塔防游戏

参考:

​ 菜鸟教程:http://www.runoob.com/design-pattern/abstract-factory-pattern.html

​ 图说设计模式:https://design-patterns.readthedocs.io/zh_CN/latest/creational_patterns/abstract_factory.html

推荐阅读:23种设计模式详解