Encapsulation(6) - Inline Class

Kerem Song·2021년 4월 6일
0

Refactoring - Study

목록 보기
19/22

Inline Class

Merge class if class isn't doing very much. Move its feature to another class then delete it.
(클래스가 뚜렷한 역할이 없을 경우 클래스를 합치기.)

class Person {
  get officeAreaCode() { return this._telephoneNumber.areaCode }
  get officeNumber() { return this._telephoneNumber.number }
}
class TelephoneNumber {
  get areaCode() { return this._areaCode }
  get number() { return this._number }
}

to

class Person {
  get officeAreaCode() { return this._officeAreaCode }
  get officeNumber() { return this._officeNumber }
}

Motivation

  1. Class is no longer pulling its weight and shouldn’t be around any more
    클래스의 간결화

  2. When want to refactor pair of classes. First Inline Class -> Extract Class to make new separation
    리팩터링을 할때 첫번째, 인라인 클래스들을 추출하여 새로 분리한다.(?)

Procedure
1. Create methods in the target class that correspond to each public method in the source class. These methods should simply delegate the task to the source class.
소스 클래스의 각 public 메소드에 대응하는 메소드들을 타겟 클래스에 생성한다. 이 메소드들은 단순히 작업을 소스 클래스로 위임해야 한다.

  1. Replace all code that uses the source class' method with the target class' delegation method. Test each time you change one by one.
    소스 클래스의 메소드를 사용하는 코드를 모두 타겟 클래스의 위임 메소드를 사용하도록 바꾼다. 하나씩 바꿀때마다 테스트한다.

  2. Move both methods and fields of the source class to the target class. Test each time you move one by one.
    소스 클래스의 메소드와 필드를 모두 타겟 클래스로 옮긴다. 하나씩 옮길 때마다 테스트한다.

  3. Delete source class
    소스 클래스를 삭제한다.

profile
Tea and Dessert

0개의 댓글