js设计模式之建造者模式

$(function () {
   var Person=function (name,work) {
       var _person=new Human()
       _person.name=new Names(name);
       _person.work=new Work(work);
       return  _person
   }
    var person=new Person('xiao  zhang','code');

    console.log(person.skill)
    console.log(person.name.firstName)
    console.log(person.work.desc)
    person.work.changeDesc('请更改描述')
    console.log(person.work.desc)

});

var Human=function (param) {
    this.skill=param&&param.skill||'保密';
    this.hobby=param&&param.hobby||'保密';
}
Human.prototype ={
    getSkill:function () {
        return  this.skill;
    },
    getBobby:function () {
        return  this.hobby;
    }
}

var Names=function (name) {
    var that=this;
    (function (name,that) {
        that.wholeName=name;
        if(that.wholeName.indexOf(' ')>-1){
            that.firstName=that.wholeName.slice(0,name.indexOf(' '));
            that.secondName=that.wholeName.slice(name.indexOf(' '));
        }
    })(name,that);
}


var  Work=function (work) {
    var that=this;
    (function (work,that) {
       switch  (work){
           case 'code':
                that.work='工程师'
               break;
           case 'UI':
               that.work='美工'
               break;
           case 'teach':
               that.work='教师'
               break;
           default:
               that.work='无该岗位描述'
               break;
       }
    })(name,that);
}


Work.prototype.changeWork=function (work) {
    this.work=work;
}
Work.prototype.changeDesc=function (desc) {
    this.desc=desc;
}

建造者模式顾名思义:

   一个建造者,使用不同的工具堆砌出的一种模式

 

建造者模式归根到底可以认为是。通过不同的类加工出不同的东西,然后展示出最终的效果

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值