您的位置:首页 > Web前端 > JavaScript

js设计模式-建造者模式

2016-03-29 15:05 417 查看
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>functionModule</title>
</head>
<body>

<script type="text/javascript">

//建造者模式
//1.产出的东西是房子
//2.baogongtou调用工人进行开工 而且他要清楚工人们具体的某一个大项目
//3.gongren是盖房子的 工人可以建卧室 客厅 厨房
//4.包工头只是一个接口而已,他不干活 他只对外说我能盖房子

//房子
function Fangzi(){
this.woshi="";
this.keting="";
this.chufang="";
}

//包工头
function Baogongtou(){
this.gaifangzi=function(gongren){
gongren.jian_woshi();
gongren.jian_keting();
gongren.jian_chufang();
}
}

//工人
function Gongren(){
this.jian_woshi=function(){
console.log("卧室盖好了");
};
this.jian_keting=function(){
console.log("客厅盖好了");
};
this.jian_chufang=function(){
console.log("厨房盖好了");
};

this.jiaogong=function(){

var __fangzi= new Fangzi();

__fangzi.woshi="ok";
__fangzi.keting="ok";
__fangzi.chufang="ok";

return __fangzi;

};
}

var gongren= new Gongren();
var baogongrou= new Baogongtou();
baogongrou.gaifangzi(gongren);

var myfangzi =gongren.jiaogong();

console.log(myfangzi);

</script>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: