✏️12.11 슈퍼클래스를 위임으로 바꾸기
class List {...}
class Stack extends List {...}class Stack {
constructor() {
this._storage = new List();
}
}
class List {...}🧷 배경
🧷 절차
🧷 예시
🧷 리팩터링 전
🧷 리팩터링 후
Last updated
class List {...}
class Stack extends List {...}class Stack {
constructor() {
this._storage = new List();
}
}
class List {...}Last updated
class CatalogItem {
constructor(id, title, tags) {
this._id = id;
this._title = title;
this._tags = tags;
}
get id() {return this._id;}
get title() {return this._title;}
hasTag(arg) {return this._tags.includes(arg);}
}
// Scroll 클래스 (CatalogItem을 상속함)
class Scroll extends CatalogItem {
constructor(id, title, tags, dateLastCleaned) {
super(id, title, tags);
this._lastCleaned = dateLastCleaned;
}
needsCleaning(targetDate) {
const threshold = this.hasTag("revered") ? 700 : 1500;
return this.daysSinceLastCleaning(targetDate) > threshold;
}
daysSinceLastCleaning(targetDate) {
return this._lastCleaned.until(targetDate, ChronoUnit.DAYS);
}
}class CatalogItem {
constructor(id, title, tags) {
this._id = id;
this._title = title;
this._tags = tags;
}
// 2. 전달 메서드 만들기
get id() {return this._catalogItem._id;}
get title() {return this._catalogItem._title;}
hasTag(aString) {return this._catalogItem.hasTag(aString);}
}
// 3. 상속 관계 끊기
class Scroll {
constructor(id, title, tags, dateLastCleaned) {
// 1. 카탈로그 아이템을 참조하는 속성을 만들고 슈퍼클래스의 인스턴스를 새로 하나 만들어 대입한다.
this._catalogItem = new CatalogItem(id, title, tags);
this._lastCleaned = dateLastCleaned;
}
needsCleaning(targetDate) {
const threshold = this.hasTag("revered") ? 700 : 1500;
return this.daysSinceLastCleaning(targetDate) > threshold;
}
daysSinceLastCleaning(targetDate) {
return this._lastCleaned.until(targetDate, ChronoUnit.DAYS);
}
}