티스토리 뷰
* 요청 내역을 객체로 캡슐화하여 클라이언트를 서로 다른 요청 내역에 따라 매개 변수화 할 수 있습니다.
요청을 큐에 저장하거나 로그로 기록 할 수 도 있고 작업취소 기능을 지원 할 수 도 있습니다.
요청을 큐에 저장하거나 로그로 기록 할 수 도 있고 작업취소 기능을 지원 할 수 도 있습니다.
var oSimpleRemote = new SimpleRemoteControl();
var oLight = new Light();
var oLightCommand = new LightOnCommand(oLight);
oSimpleRemote.setCommand(oLightCommand);
oSimpleRemote.buttonWasPressed();
oSimpleRemote.buttonUndoWasPressed();
// Light
var Light = function(){
this.bOn = false;
};
Light.prototype.on = function(){
this.bOn = true;
console.log("Light is on");
};
Light.prototype.off = function(){
this.bOn = false;
console.log("Light is off");
};
// Command
var Command = function(){
};
Command.prototype.execute = function(){
throw new Error("This method must be overwritten!");
};
Command.prototype.undo = function(){
throw new Error("This method must be overwritten!");
};
// LightOnCommand
var LightOnCommand = function(oLight){
Command.apply(this);
this.oLight = oLight;
};
LightOnCommand.prototype = new Command();
LightOnCommand.prototype.execute = function(){
this.oLight.on();
};
LightOnCommand.prototype.undo = function(){
this.oLight.off();
};
// SimpleRemoteControl
var SimpleRemoteControl = function(){
this.oCommand = null;
};
SimpleRemoteControl.prototype.setCommand = function(oCommand){
this.oCommand = oCommand;
};
SimpleRemoteControl.prototype.buttonWasPressed = function(){
this.oCommand.execute();
};
SimpleRemoteControl.prototype.buttonUndoWasPressed = function(){
this.oCommand.undo();
};
'웹개발 > Javascript' 카테고리의 다른 글
| Javascript Literals 표기 비교 (0) | 2011.10.21 |
|---|---|
| 의존성 역전 원칙 ( Dependency Inversion Principle ) (0) | 2011.10.21 |
| Javascript Observer Pattern (0) | 2011.09.30 |
| 자바스크립트 위젯 만들기 (0) | 2011.09.27 |
| 정규표현식 정후방 탐색 (0) | 2011.07.18 |
댓글