1 Star 1 Fork 1

MrMriacle/ts_design_mode

forked from 驯鹿者/ts_design_mode 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
bridge.ts 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
404_already_found 提交于 2019-11-26 15:26 +08:00 . update
enum ClothesTypeEnum {
Vest,
Shirt,
Jacket,
Hoodies,
Sweater
}
enum ColorEnum {
White,
Red,
Yellow,
Black
}
// 颜色的接口
interface ColorInterface {
getColor: () => void;
}
// 类型的抽象类
abstract class AbstractClothesClass {
protected color: ColorInterface;
public setColor(color: ColorInterface) {
this.color = color;
}
public abstract getClothes(): void;
}
//类型的扩充抽象类
class JacketProducer extends AbstractClothesClass {
public getClothes() {
let color = this.color.getColor();
console.log(color + "jacket");
}
}
class ShirtProducer extends AbstractClothesClass {
public getClothes() {
let color = this.color.getColor();
console.log(color + "shirt");
}
}
// 颜色的实现类;
class BlackColor implements ColorInterface {
public getColor() {
return "black";
}
}
class WhiteColor implements ColorInterface {
public getColor() {
return "white";
}
}
// 桥接具体实现类;
class CloseShop {
private static _instance: CloseShop;
public static getInstance() {
if (!this._instance) {
this._instance = new CloseShop();
}
return this._instance;
}
public getClothes(type: ClothesTypeEnum, color: ColorEnum) {
let clothes: AbstractClothesClass;
switch (type) {
case ClothesTypeEnum.Jacket:
clothes = new JacketProducer();
break;
case ClothesTypeEnum.Shirt:
clothes = new ShirtProducer();
break;
default:
throw new Error("not support type" + type);
}
switch (color) {
case ColorEnum.White:
clothes.setColor(new WhiteColor());
break;
case ColorEnum.Black:
clothes.setColor(new BlackColor());
break;
default:
throw new Error("not support color" + color);
}
return clothes;
}
}
const clothes = CloseShop.getInstance().getClothes(
ClothesTypeEnum.Shirt,
ColorEnum.Black
);
console.log(clothes);
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mrmriacle/ts_design_mode.git
git@gitee.com:mrmriacle/ts_design_mode.git
mrmriacle
ts_design_mode
ts_design_mode
master

搜索帮助