티스토리 뷰
정의
A. 고차원의 모듈은 저차원의 모듈에 의존하면 안된다. 이 두 모듈 모두 다른 추상화된 것에 의존해야 한다.
B. 추상화 된 것은 구체적인 것에 의존하면 안 된다. 구체적인 것이 추상화 된 것에 의존해야 한다.
ㅁ -- ㅁ
ㅁ -- ㅁ
| |
ㅁ ㅁ
function buttonClient() {};
buttonClient.prototype.turnOn = function(){
throw new Error("This method must be overwritten!");
};
buttonClient.prototype.turnOff = function(){
throw new Error("This method must be overwritten!");
};
function lamp() {
buttonClient.apply(this);
}
lamp.prototype = new buttonClient();
lamp.prototype.turnOn = function() {
console.log( ' Lamp trun On' );
};
lamp.prototype.turnOff = function() {
console.log( ' Lamp trun Off ' );
};
// 전통적인 구현 방식 .. 버튼이 램프를 바로 의존.
function oldButton(lamp) {
this.lamp = lamp
}
oldButton.prototype.detect = function( oBtn) {
var btnOn = this.getState( oBtn );
if ( btnOn ) {
this.lamp.turnOn();
} else {
this.lamp.turnOff();
}
};
oldButton.prototype.getState = function( oBtn ) {
var isPressed = false;
//장치 검사.
$(oBtn).toggleClass('toggle');
if ( $(oBtn).hasClass('toggle') ) {
isPressed = true;
}
return isPressed;
};
// 의존성 주입 .. 둘다 추상적인 것에 의존합니다.
function button( client ){
this.buttonClient = client;
}
button.prototype.detect = function( oBtn )
{
var btnOn = this.getState( oBtn );
if ( btnOn ) {
this.buttonClient.turnOn();
} else {
this.buttonClient.turnOff();
}
}
button.prototype.getState = function( oBtn ) {
throw new Error("This method must be overwritten!");
};
function buttonImpl( client ) {
button.apply(this);
this.buttonClient = client;
}
buttonImpl.prototype = new button();
buttonImpl.prototype.getState = function( oBtn ){
var isPressed = false;
//장치 검사.
$(oBtn).toggleClass('toggle');
if ( $(oBtn).hasClass('toggle') ) {
isPressed = true;
}
return isPressed;
};
var l = new lamp();
var btn = new buttonImpl(l);
$("#btnCheck").click(function(){
btn.detect( this );
});
$("#oldBtnCheck").click(function(){
var oldBtn = new oldButton(l);
oldBtn.detect( this);
});
'웹개발 > Javascript' 카테고리의 다른 글
| javascript 상속 이해하기 관련 사이트 (0) | 2012.02.08 |
|---|---|
| Javascript Literals 표기 비교 (0) | 2011.10.21 |
| Command 패턴 정리 (0) | 2011.10.18 |
| Javascript Observer Pattern (0) | 2011.09.30 |
| 자바스크립트 위젯 만들기 (0) | 2011.09.27 |
댓글