특징 | 내용 |
---|---|
비용과 모듈 관계 | 모듈 수가 증가하면 인터페이스 비용 증가 |
정보은폐 | 어렵거나 변경 가능성이 있다면 다른 모듈로부터 은폐 |
자료추상화 | 자료구조 엑세스 함수 내 자료구조 표현 내역 은폐 |
모듈 독립성 | 낮은 결합도와 높은 응집도 |
구분 | 설명 |
---|---|
관리 측면 | 효율적인 관리 및 성능향상 요구 |
개발 측면 | 복잡도 감소로 개발, 테스트, 통합이 용이 |
유지보수 측면 | 프로그램 재사용으로 유지보수 용이성 요구 |
성능 비용 측면 | 오류 파급효과 최소화 요구 단위당 프로그램 개발 노력/비용 최소화 요구 |
(낮음)자료 결합도 < 스탬프 결합도 < 제어 결합도 < 외부 결합도 < 공통 결합도 < 내용 결합도(높음)
// 아래 리터럴 객체는 O객체에 대해 stamp coupled 되어있다.
O.makeBread({ type: wheat, size: 99, name: "foo" });//3개 데이터
O.prototype.makeBread = function(args) {
return new Bread(args.type, args.size);//3개중 2개만 사용
};
var absFactory = new AbstractFactory({ env: "TEST" });
//싱글톤 객체를 만들며 env라는 플래그를 넘기는 아래 예제를 통해 확인할 수 있다.
var Global = "global";
function A() {
Global = "A";
}
function B() {
Global = "B";
}
//두 객체(함수)가 global 변수를 공유하고 있다.
// O 객체의 상태를 직접 변경
O.property = "blah";
// O 객체의 내부를 변경
O.method = function() {
/* something else */
};
// O 객체 전체를 변경
O.prototype.method = function() {
/* switcheroo */
};
//O 객체에 대해 내용 결합도로 되어있다.
function setTable() {
var cloth = new TableCloth(),
dishes = new Dishes();
this.placeTableCloth(cloth);
this.placeDishes(dishes);
}
// 함수 안 인스턴스 생성은 "강한 결합도"의 코드 생성
// 단위 테스트 시 setTable동작만 테스트해야하지만 cloth, dishes객체가 두개나 필요하다.
function setTable(cloth, dishes) {
this.placeTableCloth(cloth);
this.placeDishes(dishes);
}
//cloth, dishes 객체를 Mock/Stub하여 테스트를 수행할 수 있다.
function dinnerParty(guests) {
var table = new Table(),
invitations = new Intivations(),//초대
ingredients = new Ingredients(),//재료
chef = new Chef(),//주방장
staff = new Staff(),//스태프
cloth = new FancyTableClothWithFringes(),//식탁보?
dishes = new ChinaWithBlueBorders(),//중국도자기
dinner;
intivations.invite(guests);//초대
table.setTable(cloth, dishes);//테이블 세팅
dinner = chef.cook(ingredients);// 요리// 요리
staff.serve(dinner);
}
//정적 타입 언어의 패턴 중 하나인 "팩토리 패턴"을 사용하면 좋다.
//객체간 의존성이 있지만 팩토리 자체에 의존성이 모인다는 점, 테스트 코드에서 Mock/Stub할 수 있게 된다는 장점이 있다.(추상팩토리로 테스트 가능)
//팩토리 패턴으로 cloth만 결합도를 낮춰보겠다.
var TableClothFactory = {
getTableCloth: function(color) {
return Object.create(TableCloth, { color: { value: color } });
},
};
//팩토리를 추가한다면 이런 형식일 것이다.
var ChefFactory = {
callChef: function(chefName) {
return Object.create(chef, { name: { value: chefName } });
},
};
function dinnerParty(guests) {
var table = new Table(),
invitations = new Intivations(),//초대
ingredients = new Ingredients(),//재료
staff = new Staff(),//스태프
//테이블 cloth의 변경 모습. 다른 객체들도 팩토리를 이용해 만들 수 있다.
var tc = TableClothFactory.getTableCloth("purple");
//셰프 호출의 변경 모습
var chef = ChefFactory.callChef("sam")
dishes = new ChinaWithBlueBorders(),//중국도자기
dinner;
intivations.invite(guests);//초대
table.setTable(cloth, dishes);//테이블 세팅
dinner = chef.cook(ingredients);// 요리// 요리
staff.serve(dinner);
}