23種設計模式之抽象工廠

我的網站:https://chenmingyu.top/design/html

抽象工廠

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

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

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

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

抽象工廠模式類圖

以遊戲爲例,定義一個抽象工廠,生產射擊和塔防兩種遊戲,有兩個具體的生產工廠,任天堂和騰訊,兩個工廠生產各自品牌的兩類遊戲產品設計模式

角色ide

  1. 抽象工廠:GameFactory,規定了生成射擊類和塔防類兩種遊戲
  2. 具體工廠:NintendoGameFactoryTencentGameFactory,負責生產各自品牌的射擊類和塔防類遊戲
  3. 抽象產品:GameableShootGameTowerDefenceGame是抽象類,實現Gameable
  4. 具體產品:NintendoShootGameNintendoTowerDefenceGameTencentShootGameTencentTowerDefenceGame

優勢

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

缺點

不易增長新的產品,若是要增長新的產品須要抽象工廠和全部具體工廠網站

模式代碼實現

源碼:https://github.com/mingyuHub/design-patterns/tree/master/src/main/java/com/example/design設計

GameFactory

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

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

相關文章
相關標籤/搜索