模式作用:
1.分步创建一个复杂的对象
2.解耦封装过程和具体创建组件
3.无需关心组件如何组装
//1.产出东西是房子
//2.包工头调用工人进行开工 而且他要很清楚工人们具体的某一个大项
//3.工人是盖房子的 工人可以建卧室 建客厅 建厨房
//4.包工头只是一个接口而已 他不干活 他只对外说我能建房子
function Fangzi() { //
if (! (this instanceof Fangzi)) {
return new Fangzi();//强制调用new
}
this.woshi = "";
this.keting = "";
this.chufang = "";
}
function Baogongtou() {
if (! (this instanceof Baogongtou)) {
return new Baogongtou();
}
this.jianfangzi = function(gongren) {
gongren.jian_chufang();
gongren.jian_keting();
gongren.jian_woshi();
}
}
function Gongren() {
if (! (this instanceof Gongren)) {
return new Gongren();
}
this.jian_woshi = function() {
console.log("建卧室");
}
this.jian_keting = function() {
console.log("建客厅");
}
this.jian_chufang = function() {
console.log("建厨房");
}
this.jiaofang = function() {
var _fangzi = new Fangzi();
_fangzi.woshi = "ok";
_fangzi.keting = "ok";
_fangzi.chufang = "ok";
return _fangzi;
}
}
var gongren = new Gongren();
var baogongtou = new Baogongtou();
baogongtou.jianfangzi(gongren);
var myfangzi = gongren.jiaofang();
console.log(myfangzi);