class Person{
get officeAreaCode() {return this._telephoneNumber.areaCode;}
get officeNumber(arg) {this._telephoneNumber.number = arg;}
}
class TelephoneNumber {
get areaCode() {return this._areaCode;}
get number() {return this_number;}
}
리팩터링 후
class Person {
get officeAreaCode() {return this._officeAreaCode;}
get officeNumber() {return this._officeNumber;}
}
🧷 배경
클래스 추출하기를 거꾸로 돌리는 리펙터링이다.
제 역할을 못 해서 그대로 두면 안 되는 클래스가 대상이며 역할을 옮기는 리패터링 후 특정 클래스에 남은 역할이 거의 없을 때 주로 진행한다.
이 클래스의 역할을 가장 많이 사용하는 클래스로 흡수한다.
두 클래스의 기능을 지금과 다르게 배분하고 싶을 때에도 사용하는데, 두 클래스를 인라인 해서 하나로 합친 후, 다시 새로운 클래스로 추출할 수 있다.
🧷 절차
소스 클래스의 각 public 메서드에 대응하는 메서드들을 타깃 클래스에 생성한다. 이 메서드들은 단순히 작업을 소스 클래스로 위임해야 한다.
소스 클래스의 메서드를 사용하는 코드를 모두 타깃 클래스의 위임 메서드를 사용하도록 바꾼다. 하나씩 바꿀 때마다 테스트한다.
소스 클래스의 메서드와 필드를 모두 타깃 클래스로 옮긴다. 하나씩 옮길 때마다 테스트한다.
소스 클래스를 삭제하고 조의를 표한다.
🧷 예시
class TrackingInformation{
get shippingCompany() {return this._shippingCompany;} //배송회사
set shippingCompany(arg) {this._shippingCompany = arg;}
get trackingNumber() {return this.trackingNumber;} //추적번호
set trackingNumber(arg) {this.trackingNumber = arg;} //추적번호
get display(){
return `${this.shippingCompany}: ${this.trackingNumber}`;
}
}
이 클래스는 아래의 shipment 클래스의 일부처럼 사용되고 있다.
// Shipment 클래스...
get trackingInfo() {
return this._trackingInformation.display;
}
get trackingInformation() {return this._trackingInformation;}
set trackingInformation(aTrackingInformation) {
this._trackingInformation = aTrackingInformation
}
TrackingInformation클래스를 Shipment클래스로 인라인할 것이다. 먼저 TrackingInformation클래스를 호출하는 메서드를 찾자.