美文网首页
创建--建造者

创建--建造者

作者: 掐指化梦 | 来源:发表于2020-05-11 09:17 被阅读0次

    定义:将一个复杂对象的构建和表示分离,使同一个构建过程可以创建不同的表示
    分步构建一个复杂对象,复杂对象的各个部分经常变化

    //我要开网店买手机   需求:需要手机 
    //手机品牌商   接口:我能提供手机
    //生成车间   手机具体生产者
    function Mobile(){
        this.screen=''
        this.isSmart=''
    }
    function Brand(){
        this.produceMobile = function(workshop){
            workshop._produce_screen()
            workshop._produce_shell()
            workshop._produce_PCB()
        }
    }
    function Workshop(){}
    Workshop.prototype._produce_screen=function(){
        console.log('生产手机屏幕')
    }
    Workshop.prototype._produce_shell=function(){
        console.log('生产手机壳')
    }
    Workshop.prototype._produce_PCB=function(){
        console.log('生产手机电路板')
    }
    Workshop.prototype.deliver=function(){
        this._produce_screen()
        this._produce_shell()
        this._produce_PCB()
        var mobile = new Mobile()
        mobile.screen='大屏'
        mobile.isSmart='是'
        return mobile
    }
    
    var workshop = new Workshop()
    var brand = new Brand()
    brand.produceMobile(brand)
    var mobile = workshop.deliver()
    console.log(mobile)
    

    相关文章

      网友评论

          本文标题:创建--建造者

          本文链接:https://www.haomeiwen.com/subject/fffwghtx.html