console.log('桥接模式');
//调用父类实验--------------------------------------------
class A {
constructor() {
this.a = 0;
}
exe() {
console.log('A');
}
}
class B extends A {
constructor() {
super();
}
exe() {
super.exe();
//console.log('B');
this.append();
}
append() {
console.log('B');
}
}
//var b = new B();
//b.exe();
//坦克大战开始--------------------------------------------
// 抽象层
class Tank {
exe() {
}
}
class Decorator extends Tank {
constructor(compoment) {
super();
this.compoment = compoment;
this.appendStr = "";
}
exe() {
this.compoment.exe();
this.append();
}
append() {
console.log(this.appendStr);
}
}
class B70Tank extends Tank{
exe() {
console.log('sort:70');
}
}
// 实际层
class B50Tank extends Tank {
exe() {
console.log('sort:50');
}
}
class HongwaiDecorator extends Decorator {
constructor(compoment) {
super(compoment);
this.compoment = compoment;
this.appendStr = "红外扫描";
}
}
class LiangxieDecorator extends Decorator {
constructor(compoment) {
super(compoment);
this.appendStr = "水陆两栖";
}
}
class DingweiDecorator extends Decorator {
constructor(compoment) {
super(compoment);
this.appendStr = "卫星";
}
}
// 客户端
class Client {
main() {
console.log("坦克装饰前------------")
var b7 = new B70Tank();
b7.exe();
console.log("坦克装饰后------------")
var hw = new HongwaiDecorator(b7);
var lx = new LiangxieDecorator(hw);
var dw = new DingweiDecorator(lx);
dw.exe();
}
}
var client = new Client();
client.main();
装饰模式-坦克大战-js
最新推荐文章于 2024-05-29 19:24:14 发布