前言
建造者模式属于创建型模式,这个类型的设计模式是将 对象的创建和使用解耦了,花式的去创建对象。
建造者模式
应用场景:有顺序的使用多个步骤来创建一个类的对象,每一个步骤都负责创建部分的对象。
理解:复杂对象的创建和使用解耦了,但是创建对象的步骤有时前后是关联的,比如skillList技能列表中数据,如果步骤顺序不对,那么最终构建的内容就会偏离期望值,甚至会构建失败。创建对象的步骤必须得全,不然这个对象得构建会失败,比如以上的代码案例,如果没有setBody一定会报错。不过我觉得建造者模式是一种分而治之、再汇总处理的思想,不一定所有步骤都必须有,这是看你如何去设计,步骤可以分为必须和非必须两种,非必须的步骤就算没有,也可以构建成功,就像并非所有的英雄都得牺牲才算英雄,只要他们做了利国利民的壮举就算英雄。
namespace creative_mode_03 {
// 产品类
class SuperMan {
private name: string = ''
private body: string = ''
private woman: string = ''
private arms: string = ''
private power: string = ''
private skillList: Array<string> = []
constructor (name: string, body: string, woman: string, arms: string, power: string, skillList: string[]) {
this.name = name
this.body = body
this.woman = woman
this.arms = arms
this.power = power
this.skillList = skillList
}
toResultString () {
console.log(`我是${this.name},拥有${this.body},我的女人叫${this.woman},我持有${this.arms},我擅长${this.power}。`)
console.log(`开始放大招: \r\n====================\r\n${this.skillList.join('\r\n')}\r\n=====================`)
}
}
// 建造者接口
interface IBuilderabler {
setName(name: string): IBuilderabler
setBody(body: string): IBuilderabler
setWoman(woman: string): IBuilderabler
setArms(arms: string): IBuilderabler
setPower(power: string): IBuilderabler
setSkill (skill: string): IBuilderabler
build (): SuperMan
}
// 建造者
class Builer implements IBuilderabler {
private name: string = ''
private body: string = ''
private woman: string = ''
private arms: string = ''
private power: string = ''
private skillList: Array<string> = []
build(): SuperMan {
// 只有所有步骤全部执行完毕才能创建出对象来
if (this.name && this.body && this.woman && this.arms && this.power && this.skillList) {
return new SuperMan(this.name, this.body, this.woman, this.arms, this.power, this.skillList)
}
throw new Error("build fail. name、body、woman、arms、power、skillList is reqired.")
}
setName(name: string): IBuilderabler {
if (!name) {
throw new Error("name is reqired.")
}
this.name = name
return this
}
setBody(body: string): IBuilderabler {
if (!body) {
throw new Error("body is reqired.")
}
this.body = body
return this
}
setWoman(woman: string): IBuilderabler {
if (!woman) {
throw new Error("woman is reqired.")
}
this.woman = woman
return this
}
setArms(arms: string): IBuilderabler {
if (!arms) {
throw new Error("arms is reqired.")
}
this.arms = arms
return this
}
setPower(power: string): IBuilderabler {
if (!power) {
throw new Error("power is reqired.")
}
this.power = power
return this
}
setSkill(skill: string): IBuilderabler {
if (!skill) {
throw new Error("skill is reqired.")
}
if (Array.isArray(skill)) {
this.skillList = skill
} else {
this.skillList.push(skill)
}
return this
}
}
// 使用 1
const ultraManBuilder = new Builer()
const ultraman = ultraManBuilder
.setName('奥特曼之父')
.setBody('奥特曼的皮套')
.setWoman('奥特之母')
.setArms('奥特之剑')
.setPower('召唤奥特国所有的奥特曼出来作战')
.setSkill('泰罗奥特曼出列')
.setSkill('雷欧奥特曼出列')
.setSkill('雷欧奥特曼出列')
.setSkill('迪迦奥特曼出列')
.build()
ultraman.toResultString()
// 使用2
const soulLandBuilder = new Builer()
const tangsan = soulLandBuilder
.setName('唐三')
.setBody('八蛛魂骨')
.setWoman('十万年魂兽小舞')
.setArms('昊天锤')
.setPower('九九八十一式乱劈风吹法')
.setSkill('蓝银领域')
.setSkill('杀神领域')
.setSkill('蓝银霸王枪 去')
.setSkill('无敌金身')
.setSkill('虚无')
.setSkill('九九归一乱劈风')
.build()
tangsan.toResultString()
}