js設計模式----創建者模式(1)工廠模式

The Abstract Factory builds a family of related classes and the builder creates complicated objects using different strategies. The factory method pattern allows a class to request a new instance of an interface without the class making decisions about which implementation of the interface to use
這裏寫圖片描述
用冰火裏面的各自信仰的神來舉例

let WateryGod = (function () {
    function WateryGod() {
    }
    WateryGod.prototype.prayTo = function () {

    };
    return WateryGod;
})();
Religion.WateryGod = WateryGod;
let AncientGods = (function () {
    function AncientGods() {
    }
    AncientGods.prototype.prayTo = function () {

    };
    return AncientGods;
})();
Religion.AncientGods = AncientGods;

let DefaultGod = (function () {
    function DefaultGod() {
    }
    DefaultGod.prototype.prayTo = function () {
    };
    return DefaultGod;
})();
Religion.DefaultGod = DefaultGod;

let GodFactory = (function () {
    function GodFactory() {
    }
    GodFactory.Build = function (godName) {
        if (godName === "watery")
            return new WateryGod();
        if (godName === "ancient")
            return new AncientGods();
        return new DefaultGod();
    };
    return GodFactory;
})();
let Prayer = (function () {
    function Prayer() {
            }
    Prayer.prototype.pray = function (godName) {
        GodFactory.Build(godName).prayTo();
    }
    return Prayer;
})();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章