js建造者模式

来源:互联网 发布:sql删除语句 编辑:程序博客网 时间:2024/06/09 23:55
   <h1>第六章 分即是合——建造者模式</h1>   <p>建造者模式(builder):将一个复杂对象的构建层与其表示层相互分离,同样的构建过程可采用不同的表示。</p>   <p>工厂模式主要是为了创建对象实例或者类簇,关心的是最终产出的是什么。不关心你创建的整个过程,仅仅需要知道最终创建的结果。所以通过工厂模式我们得到的都是对象实例或者类簇。</p>   <p>然而建造者模式在创建对象是更为复杂一些,虽然其目的也是为了创建对象,但他更多关心的是创建这个对象的整个过程,甚至于创建对象的每一个细节。</p>   <script>       //创建一位人类       var Human = function( param ){           //技能           this.skill = param && param.skill || "保密";           //爱好           this.hobby = param && param.hobby || "保密";       };       //r人类原型方法       Human.prototype = {           getSkill: function(){               return this.skill;           },           getHobby: function(){               return this.hobby;           }       };       //实例化姓名类       var Named = function( name ){           //姓           this.firstName = "";           //名           this.lastName = "";           var _this = this;           //函数自调用解析姓名的姓与名           ( function(){               if( name.indexOf(" ") > -1 ){                    _this.firstName = name.slice( 0, name.indexOf( " " ) );                    _this.lastName = name.slice( name.indexOf( " " ) );               }           } )()       };       //实例化职位对象       var Work = function( work ){           this.work = "";           this.workDescript = "";           var _this = this;           //函数自调用通过传入的职位特征来设置相应职位以及描述           ( function(){               switch( work ){                   case "code":                       _this.work = "工程师";                       _this.workDescript = "每天沉醉于编程";                       break;                   case "UI":                   case "UE":                       _this.work = "设计师";                       _this.workDescript = "设计更似一种艺术";                       break;                   case "teach":                       _this.work = "教师";                       _this.workDescript = "分享也是一种快乐";                       break;                   default:                       _this.work = work;                       _this.workDescript = "对不起,我们还不清楚您所选的职位的相关描述";               }           } )()       };       //更换期望的职位       Work.prototype.changeWork = function( work ){           this.work = work;       };       //添加对职位的描述       Work.prototype.changeDescript = function( sentence){           this.workDescript = sentence;       };       //创建一位应聘者       /****        * 应聘者建造者        *参数name: 姓名(全名)        * 啊参数work:期望职业        */       var Person = function( name, work ){           //创建应聘这缓存对象           var _person = new Human();           //应聘者姓名解析           _person.name = new Named( name );           //创建应聘这职位期望           _person.work = new Work( work );           //将应聘对象返回           return _person;       };       var person = new Person( "xie xialing", "code" );       console.log( person.name.firstName );       console.log( person.skill );       console.log( person.work.work );       console.log( person.work.workDescript );       person.work.changeDescript( "更改一下职位描述" );       console.log( person.work.workDescript);   </script>

0 0