console.log("创建者模式")
// 抽象层
class Builder{
buildSort(tank){
tank.sort = this.sort;
}
buildRun(tank){
tank.run = this.run;
}
}
class Tank{
constructor(){
this.sort = "";
this.run = "";
}
exe(){
console.log(this.sort);
console.log(this.run);
}
}
class Director{
constructor(builder){
this.builder = builder;
}
constuct(tank){
this.builder.buildSort(tank);
this.builder.buildRun(tank);
}
}
// 实现层
class B70Builder extends Builder{
constructor(){
super();
this.sort = "发射70";
this.run = "速度70";
}
}
class B50Builder extends Builder{
constructor(){
super();
this.sort = "发射50";
this.run = "发射50";
}
}
// 客户端
class Client{
main(){
var builder = new B70Builder();
var director = new Director(builder);
var tank = new Tank();
director.constuct(tank);
tank.exe();
}
}
var client = new Client();
client.main();
坦克大战-创建者模式实现-js
最新推荐文章于 2024-06-07 15:17:39 发布