设计模式(三) 建造者模式

工厂模式关心最终创建的是什么,建造者模式更关心创建对象的过程。

var human = function(hobby){
    this.hobby = hobby;
}
human.prototype = {
    getHobby: function(){
        return this.hobby;
    }
}
//实例化姓名类
var named = function(name){
    var that = this;
    //构造器
    (function(name, that){
        that.wholeName = name;
    })(name, that);
}
named.prototype.xx = function(){}
var person = function(name){
    var _person = new Human();
    _person.name = new named(name);
    //若有其他类可继续添加……
    return _person;
}
var x1 = new person('Bob');
//通过对创建对象的类的模块化,使得其可以得到灵活运用,高度复用,但也会增加结构的复杂度