[AJAX 강의 5-7장]에 이어서 이번장에서는 이어서 은닉(Encapsulatioin) 에 대해서 소개한다.
객체지향 언어에는 은닉이라는 개념이 있다. 표현이 좀 어려울지도 모르겠으나 외부로 공개하면 안되는 무엇인가 중요한 것을 감추고 있다는 뜻이다.
객체지향 언어의 클래스의 가장 큰 장점 중 하나는 클래스를 인스턴스화한 객체에 데이터를 저장할 수 있다는 것이다. 하지만 보안의 측면에서 데이터를 보호할 필요성도 있기 때문에 그 수단으로써 은닉을 사용한다고 생각하면 된다. 데이터를 감춘다고 외부에서 전혀 사용하지 못하게 하면 쓸모가 없으므로 접근할 수 있는 통로는 최소한으로 제한한다.
데이터를 감추기 위한 방법으로 클래스의 멤버에 private 키워드를 사용한다. 외부에서 데이터에 접근하기 위해서는 public 접근자를 가지고 있는 setXxx(), getXxx() 메소드를 사용한다.
객체지향 언어에서의 간단한 예를 살펴보자.
public class Vehicle {
private int wheelCount;//바퀴수
private int curbWeightInPounds;//무게
public Vehicle() {//생성자
}
public int getCurbWeightInPounds() {
return curbWeightInPounds;
}
public void setCurbWeightInPounds(int curbWeightInPounds) {
this.curbWeightInPounds = curbWeightInPounds;
}
public int getWheelCount() {
return wheelCount;
}
public void setWheelCount(int wheelCount) {
this.wheelCount = wheelCount;
}
}
위 Vehicle 클래스에는 wheelCount, curbWeightInPounds 라는 두개의 멤버와 setXxx(), getXxx() 메소드가 존재한다. 외부에서는 Vehicle 클래스의 멤버에 직접적으로 접근할 수 없고 setXxx(), getXxx() 메소드를 사용해야만 한다.
이제 자바스크립트에서는 은닉을 어떻게 사용할 수 있는지 소개한다.
www.crockford.com 의 Douglas Crockford 는 자바스크립트에서 private 멤버와 public 접근자 메소드 방법론을 아래와 같이 증명하고 있다.
첫째, private 멤버를 생성하기 위해서는 생성자 안에서 정의해야 한다. var 키워드는 사용해도 되고 안 해도 상관없다.
둘째, public 접근자 메소드를 생성하기 위해서는 생성자 안에서 정의해야 한다. 이때 반드시 this 키워드를 사용해야 한다.
예제를 살펴보자.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript Encapsulation</title>
<script type="text/javaScript">
function createVehicle() {
var vehicle = new Vehicle();
alert(vehicle.wheelCount);
alert("wheelCount : "+vehicle.getWheelCount()+", curbWeightInPounds : "+vehicle.getCurbWeightInPounds());
vehicle.setWheelCount(6);
vehicle.setCurbWeightInPounds(5000);
alert("wheelCount : "+vehicle.getWheelCount()+", curbWeightInPounds : "+vehicle.getCurbWeightInPounds());
}
function Vehicle() {//Vehicle 클래스 생성자
var wheelCount = 4;//private 멤버정의
var curbWeightInPounds = 4000;//private 멤버 정의
this.getWheelCount = function(){//pubic 메소드 정의
return wheelCount;
}
this.setWheelCount = function(count){//pubic 메소드 정의
wheelCount = count;
}
this.getCurbWeightInPounds = function(){//pubic 메소드 정의
return curbWeightInPounds;
}
this.setCurbWeightInPounds = function(weight){//pubic 메소드 정의
curbWeightInPounds = weight;
}
}
</script>
</head>
<body>
<h1>Examples of JavaScript Encapsulation</h1>
<br/><br/>
<button onclick="createVehicle();">Create an instance of Vehicle</button>
</body>
</html>
<encapsulation.html 의 전체 소스 코드>
위 예제에서는 Vehicle 클래스를 정의할때 생성자 안에서 wheelCount, curbWeightIPounds 를 정의하고 있으므로 두 멤버는 모두 private 성격을 가지게 된다. 따라서 외부에서는 직접적으로 이 멤버에 접근할 수 없다.
또한 두 멤버와 관련된 getter 및 setter 메소드들도 생성자 안에서 this 키워드로 정의하고 있으므로 public 성격을 띄게 된다. 외부에서는 이 메소드를 통해서만 private 멤버의 데이터에 접근할 수 있다.
자바스크립트에서도 객체지향 언어의 은닉에 해당하는 유사한 형태의 구현이 가능하다는 것을 보여주는 샘플이다.
위 예제의 테스트 결과를 살펴보자.
위 그림은 getWheelCount() 및 getCurbWeightInPounds() 메소드를 사용해서 private 멤버의 데이터를 출력한 모습이다.
위 그림은 setWheelCount() 및 setCurbWeightInPounds() 메소드를 이용해서 private 멤버의 데이터를 수정한 결과를 출력한 모습이다.
위 예제는 다운받아 테스트 해 볼 수 있다.
'WEB > AJAX' 카테고리의 다른 글
| AJAX 강의 6-1장 - JsUnit 활용/시작하기 (0) | 2007/06/07 |
|---|---|
| AJAX 강의 5-9장 - 자바스크립트 심화학습/객체지향 (0) | 2007/06/06 |
| AJAX 강의 5-8장 - 자바스크립트 심화학습/객체지향 (0) | 2007/06/06 |
| AJAX 강의 5-7장 - 자바스크립트 심화학습/객체지향(상 (0) | 2007/06/06 |
| ASP.NET Spiced: AJAX (0) | 2007/06/06 |
| 자바 개발자를 위한 Ajax: 동적 자바 애플리케이션 구현 (0) | 2007/06/06 |




encapsulation-jinoxst.html
최근에 달린 댓글
링크
최근에 받은 트랙백
태그목록