1 Star 0 Fork 1

驯鹿者/ts_design_mode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
simpleFactory.ts 724 Bytes
一键复制 编辑 原始数据 按行查看 历史
404_already_found 提交于 2019-05-23 20:44 +08:00 . update
enum GunType {
AK,
M4A1
}
interface Shootable {
shoot: () => void;
}
abstract class Gun implements Shootable {
// 抽象产品 - 枪
public abstract shoot(): void;
}
class AK47 extends Gun {
//具体产品 - AK47
shoot() {
console.log("ak47 shoot.");
}
}
class M4A1 extends Gun {
//具体产品 - M4A1
shoot() {
console.log("m4a1 shoot.");
}
}
class GunFactory {
static createGun(type: GunType): Gun {
switch (type) {
case GunType.AK:
return new AK47();
case GunType.M4A1:
return new M4A1();
default:
throw Error("not support this gun yet");
}
}
}
GunFactory.createGun(GunType.AK).shoot();
GunFactory.createGun(GunType.M4A1).shoot();
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/chasonhuangs/ts_design_mode.git
git@gitee.com:chasonhuangs/ts_design_mode.git
chasonhuangs
ts_design_mode
ts_design_mode
master

搜索帮助