public abstract class abcls{
public action(){
}
public deafult_action(){
}
}
public class concls extends abcls{
@override
public deafult_action(){
}
<!doctype html>
<html lang="en">
<head>
<title>builder pattern</title>
</head>
<body>
<script>
var director = function(product){ //무엇을 빌딩하는지 정확히 알지 못한채 빌딩
product.concrete_builder_action1() //director의 지시에 따라 product를 빌딩된다;
product.concrete_builder_action2()
product.concrete_builder_action3()
}
function Builder(){ //공통 인터페이스
var common_value1 = this.common_value1 = 1;
var common_value2 = this.common_value2 = 2;
var common_value3 = this.common_value3 = 3;
this.concrete_builder_interface1 = function(){ //구현 인터페이스
this.concrete_builder_action1 = function(){
console.log("interface1:"+common_value1);
}
this.concrete_builder_action2 = function(){
console.log("interface1:"+common_value2);
}
this.concrete_builder_action3 = function(){
console.log("interface1:"+common_value3);
}
};
this.concrete_builder_interface2 = function(){
this.concrete_builder_action1 = function(){
console.log("interface2:"+common_value1);
}
this.concrete_builder_action2 = function(){
console.log("interface2:"+common_value2);
}
this.concrete_builder_action3 = function(){
console.log("interface2:"+common_value3);
}
}
}
var build = (new Builder); //공통 인테페이스 생성
var concrete_build1 = new build.concrete_builder_interface1; //구현 인터페이스 생성
var concrete_build2 = new build.concrete_builder_interface2;
var product1 = function(){
director(concrete_build1);
};
var product2 = function(){
director(concrete_build2);
};
product1();
product2();
</script>
</body>
</html>
// 자바스크립트로 구현
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
</head>
<body>
<script>
// 커맨드 패턴
// 리모컨 만들기
// 1. 리시버에서 해당 기능 구현
// 2. command 에 interface 생성
// 3. command에 recevier에서 받은 객체를 실행하는 동작에 대해 execute에 정의
// 4. invoker 에서 setcommand 명령에 대해 저장하는 처리
// 5. invoker에 목적에 맞는 custom메서드를 생성 (ex> 리모컨켜기, 리모컨 끄기 등)
function r(msg){
return function(){
document.body.innerHTML += (msg+"<br />");
}
}
var client, recevier, invoker, command;
function Light(){} //recevier
Light.prototype = {
"on" : r("on"),
"off" : r("off")
};
//(new Light).on();
function lightOnCommand(receiver_obj){ //command
this.execute = function(){
receiver_obj.on();
};
this.undo = function(){
receiver_obj.off();
};
}
function lightOffCommand(receiver_obj){ //command
this.execute = function(){
receiver_obj.off();
};
this.undo = function(){
receiver_obj.on();
};
}
function RemoteControl(){ //invoker
this.setCommand = function(cmd){
this.obj = cmd;
}
this.press = function(){
this.obj.execute();
this.undocommand = this.obj;
}
this.undo = function(){
this.undocommand.undo();
}
}
client = function(){
var light = new Light;
var lightOn = new lightOnCommand(light);
var lightOff = new lightOffCommand(light);
var remoteControl = new RemoteControl;
remoteControl.setCommand(lightOn);
remoteControl.press();
remoteControl.undo();
remoteControl.setCommand(lightOff);
remoteControl.press();
remoteControl.undo();
}();
</script>
</body>
</html>