04 Javascript创建型设计模式之 - 建造者模式

待兔
• 阅读 953

通过前面2个小节的学习,我们知道,设计模式的创建对象相关的设计模式有三种

  • 单例模式 这个是最简单的,当全局只需要一个对象的时候,可以用单例模式

  • 工厂模式 通过对象工厂去创建不同的对象,当创建大量的对象的时候 ,可以用工厂模式

  • 建造者模式 也就是今天我们要学习的最后一个创建型模式了,建造者模式什么情况下使用呢? 这里先说一下:当创建一个对象,这个对象比较复杂的时候,用建造者模式

先看一下定义,定义基本上没有什么用,太枯燥,看一下就行了 就望文生义就行了

什么是建造者模式

直接望文生义吧,建造者,建造者,就是建造东西用的嘛,在设计模式里面 就是创建对象的人,建造者模式就是用来创建对象的

建造者模式使用场景

这个就很重要了,如果学了某个东西,不知道能解决什么样的问题,你说能有什么用? 当一个类的构造函数参数个数超过4个,而且这些参数有些是可选的参数,考虑使用构造者模式。

说白了,就是创建的对象比较复杂,什么是复杂呢,就是属性比较多,有的属性还不是必选的,这种情况下就可以用建造者模式

比如我们想要组装一个电脑Computer,需要有 cpu, 内存硬盘主板显卡

显卡是可选的,其它的都是必选的

下面我们用代码来揭示一下组装一个电脑,用建造者模式如何实现

首页,我们先定义一个 电脑类 Computer,如下

class Computer {
    constructor(hdd,cpu,memory,mainboard,graphics){
        this.mHdd = hdd                 //硬盘
        this.mCpu = cpu                 //cpu
        this.mMemory = memory           //内存
        this.mMainboard = mainboard     //主板
        this.mGraphics = graphics       //显卡
    }

    //打印电脑的配置
    show(){
        console.log('我组装了一台电脑,配置如下:' + '  hdd=' + this.mHdd + '  cpu=' + this.mCpu + '  memory=' + this.mMemory + '  mainboard=' + this.mMainboard + '  graphics=' + this.mGraphics)
    }
}

现在有了 Computer 类,既然用建造者模式来创建Computer的对象 ,那么肯定不会直接new 一个Computer,所以接下来我们来定义一个创建Computer的类,代码如下

//电脑的创建者类
class ComputerBuilder {

    //和Computer的属性是一样的
    constructor(){
        this.mHdd = ''           //硬盘
        this.mCpu = ''           //cpu
        this.mMemory = ''        //内存
        this.mMainboard = ''     //主板
        this.mGraphics = ''      //显卡
    }

    //下面一堆set函数,并且返回this,方便以后链式调用
    setHdd(hdd){
        this.mHdd = hdd
        return this
    }

    setCpu(cpu){
        this.mCpu = cpu
        return this
    }

    setMemory(memory){
        this.mMemory = memory
        return this
    }

    setMainboard(mainboard){
        this.mMainboard = mainboard
        return this
    }

    setGraphics(graphics){
        this.mGraphics = graphics
        return this
    }

    // 主要是在这一步,在最后一步调用此方法
    // 在build方法中,创建一个Computer对象
    // 并把builder对象中的属性传过去,这样就创建了一个Computer对象
    build(){
        return new Computer(this.mHdd,this.mCpu,this.mMemory,this.mainboard,this.mGraphics)
    }
}

看上面的注释就知道,创建Computer对象 ,是在ComputerBuilder类的build()方法中,创建Computer对象的

下面写一段测试代码


//测试代码
let computer = new ComputerBuilder()
    .setCpu('intel i7')
    .setMemory('16G')
    .setHdd('512G')
    .setMainboard('技嘉主板')
    .setGraphics('七彩虹4G显存加强版')
    .build()

computer.show()    

打印出下:

我组装了一台电脑,配置如下:  hdd=512G  cpu=intel i7  memory=16G  mainboard=undefined  graphics=七彩虹4G显存加强版

完成的代码如下:

'use strict';


class Computer {
    constructor(hdd,cpu,memory,mainboard,graphics){
        this.mHdd = hdd                 //硬盘
        this.mCpu = cpu                 //cpu
        this.mMemory = memory           //内存
        this.mMainboard = mainboard     //主板
        this.mGraphics = graphics       //显卡
    }

    show(){
        console.log('我组装了一台电脑,配置如下:' + '  hdd=' + this.mHdd + '  cpu=' + this.mCpu + '  memory=' + this.mMemory + '  mainboard=' + this.mMainboard + '  graphics=' + this.mGraphics)
    }
}

//电脑的创建者类
class ComputerBuilder {

    //和Computer的属性是一样的
    constructor(){
        this.mHdd = ''           //硬盘
        this.mCpu = ''           //cpu
        this.mMemory = ''        //内存
        this.mMainboard = ''     //主板
        this.mGraphics = ''      //显卡
    }

    //下面一堆set函数,并且返回this,方便以后链式调用
    setHdd(hdd){
        this.mHdd = hdd
        return this
    }

    setCpu(cpu){
        this.mCpu = cpu
        return this
    }

    setMemory(memory){
        this.mMemory = memory
        return this
    }

    setMainboard(mainboard){
        this.mMainboard = mainboard
        return this
    }

    setGraphics(graphics){
        this.mGraphics = graphics
        return this
    }

    // 主要是在这一步,在最后一步调用此方法
    // 在build方法中,创建一个Computer对象
    // 并把builder对象中的属性传过去,这样就创建了一个Computer对象
    build(){
        return new Computer(this.mHdd,this.mCpu,this.mMemory,this.mainboard,this.mGraphics)
    }
}



//测试代码
let computer = new ComputerBuilder()
    .setCpu('intel i7')
    .setMemory('16G')
    .setHdd('512G')
    .setMainboard('技嘉主板')
    .setGraphics('七彩虹4G显存加强版')
    .build()

computer.show()    

可能还有的同学有疑问,这样做有啥用?还多写子好多代码

  • 首先,代码的可扩展性,可维护性,代码的结构好不好,和代码的数量是没有必然关系的
  • 建造者模式,如果创建多个复杂的对象,是可以复用builder对象的

比如

let builder = new ComputerBuilder()
    .setCpu('intel i7')
    .setMemory('16G')
    .setHdd('512G')
    .setMainboard('技嘉主板')
    .setGraphics('七彩虹4G显存加强版')

通过这个builder,是可以创建很多个 Computer 对象的,可能有的内存不一样,有的cpu不一样,只需要再 setXX 一下就可以创建不同的对象,方便,复用

好了,创建型模式就讲完了,接下来我们讲其它的模式

点赞
收藏
评论区
推荐文章

暂无数据