設計模式:3.抽象工廠

抽象工廠

抽象工廠模式屬於創建型模式

定義:爲創建一組相關或相互依賴的對象提供一個接口,而且無須指定它們的具體類

抽象工廠模式是對工廠方法模式的擴展,抽象工廠比工廠模式更爲抽象,工廠方法模式針對產品等級結構,而抽象工廠針對產品族。

產品族與產品等級結構的概念:

​ 產品族,是指位於不同產品等級結構中,功能相關聯的產品組成的家族,比如遊戲工廠生產射擊類和塔防類兩種產品,任天堂的射擊類遊戲和塔防類遊戲爲一個產品族,騰訊的射擊類遊戲和塔防類遊戲爲一個產品族

​ 產品等級結構,一個產品族由多個產品等級結構組成,射擊類遊戲是一個產品等級結構,塔防類遊戲也是一個產品等級結構

模式類圖
以遊戲爲例,定義一個抽象工廠,生產射擊和塔防兩種遊戲,有兩個具體的生產工廠,任天堂和騰訊,兩個工廠生產各自品牌的兩類遊戲產品
在這裏插入圖片描述
角色:

  • 抽象工廠:GameFactory,規定了生成射擊類和塔防類兩種遊戲
  • 具體工廠:NintendoGameFactory,TencentGameFactory,負責生產各自品牌的射擊類和塔防類遊戲
  • 抽象產品:Gameable,ShootGame和TowerDefenceGame是抽象類,實現Gameable
  • 具體產品:NintendoShootGame,NintendoTowerDefenceGame,TencentShootGame,TencentTowerDefenceGame

優點

  • 接口和實現分離,客戶端面向接口編程,不用關心具體實現,從具體的產品實現中解耦
  • 增加新的具體工廠和產品族方便,切換產品族方便

缺點

  • 不易增加新的產品,如果要增加新的產品需要抽象工廠和所有具體工廠

GameFactory
抽象工廠,規定了生產射擊和塔防兩類遊戲

/**
 * @author: linan
 * @date: 2020/5/25 11:29
 * @description: 工廠類
 */
public interface GameFactory {

    /**
     * 創建射擊遊戲
     * @return
     */
    Gameable createShootGame();

    /**
     * 創建塔防遊戲
     * @return
     */
    Gameable createTowerDefenceGame();
}

NintendoGameFactory
具體工廠,負責生產任天堂的射擊類和塔防類遊戲

/**
 * @author: linan
 * @date: 2020/5/25 11:29
 * @description: 工廠類
 */
public class NintendoGameFactory implements GameFactory{

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

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

TencentGameFactory
具體工廠,負責生產騰訊的射擊類和塔防類遊戲

/**
 * @author: linan
 * @date: 2020/5/25 11:29
 * @description: 工廠類
 */
public class TencentGameFactory implements GameFactory {

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

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

Gameable
抽象產品,所有遊戲產品均實現該接口

/**
 * @author: linan
 * @date: 2020/5/25 11:29
 * @description: 工廠類
 */
public interface Gameable {

    /**
     * 校驗賬戶信息
     * @param nickName
     */
    void validateAccount(String nickName);


    /**
     * 遊戲類型
     */
    void getGameType();
}

ShootGame和TowerDefenceGame
抽象類,實現Gameable接口

/**
 * @author: linan
 * @date: 2020/5/25 11:29
 * @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);
    }

}

具體產品
共四款遊戲產品:NintendoShootGame,NintendoTowerDefenceGame,TencentShootGame,TencentTowerDefenceGame

/**
 * @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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章