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;
})();