IT책, 강의/리팩터링
07 - 캡슐화 - 클래스 인라인하기
제로칼로리
2023. 7. 29. 21:39
리팩터링 | 마틴 파울러 | 한빛미디어- 교보ebook
코드 구조를 체계적으로 개선하여 효율적인 리팩터링 구현하기, 20여 년 만에 다시 돌아온 마틴 파울러의 리팩터링 2판 리팩터링 1판은 1999년 출간되었으며, 한국어판은 2002년 한국에 소개되었다
ebook-product.kyobobook.co.kr
개요
클래스 추출하기의 정반대 리팩터링
더 이상 제역할을 못하는 클래스를 인라인한다. 판단 기준은 더 이상 그 클래스 역할이 거의 없을 때이다. 옮기는 위치는 그 클래스를 자주 사용하는 곳
다른 케이스로 두 클래스의 기능을 지금과 다르게 배분하고 싶을 때도 일단 클래스를 인라인한다. 그리고 다시 클래스를 추출한다.
상황에 따라 컨텍스트 요소를 하나씩 옮기는 게 편할 수도, 아니면 일단 클래스를 인라인하고, 다시 클래스 추출하기가 편할 수도 있다.
예시
//현재 제일을 못하는 클래스라 가정 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}`; } } class Shipment{ get trackingInfo(){ return this._trackingInformation.display; } get trackingInformation() {return this._trackingInformation;} set trackingInformation(aTrackingInformation) { this._trackingInformation = aTrackingInformation; } } //클라이언트 aShipment.trackingInformation.shippingCompany = request.vender; |
class Shipment{ get trackingInfo(){ return this._trackingInformation.display; } get shippingCompany(){return this._trackingInformation.shippingCompany;} set shippingCompany(arg){this._trackingInformation.shippingCompany = arg;} get trackingInformation() {return this._trackingInformation;} set trackingInformation(aTrackingInformation) { this._trackingInformation = aTrackingInformation; } } //클라이언트- 호출부분에서 TrackingInformation 제거 let aShipment = new Shipment(); aShipment.shippingCompany = request.vender; |
class Shipment{ get trackingInfo(){ return this._trackingInformation.display; } get display(){ return `${this.shippingCompany} : ${this.trackingNumber}`; } //나머지도 옮긴 후 클래스 TrackingInformation 제거 get shippingCompany(){return this._shippingCompany;} set shippingCompany(arg){this._shippingCompany = arg;} get display(){ return `${this.shippingCompany} : ${this.trackingNumber}`; } get trackingInformation() {return this._trackingInformation;} set trackingInformation(aTrackingInformation) { this._trackingInformation = aTrackingInformation; } } let aShipment = new Shipment(); aShipment.shippingCompany = request.vender; |