Encapsultion(5) - Extract Class

Kerem Song·2021년 4월 6일
0

Refactoring - Study

목록 보기
18/22

Extract Class

(클래스 추출하기)
Extract class base on a subset of data and a subset of methods
(데이터의 부분집합과 메소드들의 부분집합들을 클래스로 추출하기)

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

to

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 }
}

Motivation

  1. Smaller class is easier to understand
    클래스를 작게 만듦으로 인해 이해하기 편리하다.
  2. Separate class's responsibility
    클래스의 의존(책임)성을 분리가능하다.

Procedure
1. fix the way to seperate class' role.
클래스의 역할을 분리할 방법을 정한다.

  1. Make the class to play a role in separation.
    분리될 역할을 담당할 클래스를 새로 만든다.

  2. Make an instance of a new class from constructor in original class and save in field.
    원래 클래스의 생성자에서 새로운 클래스의 인스턴스를 생성하여 필드에 저장해둔다.

  3. Move fields to play a role in separation to anew class. Test it when you move them every time
    분리될 역할에 필요한 필드들을 새 클래스로 옮긴다(필드 옮기기). 하나씩 옮길 때마다 테스트한다.

  4. Move methods to a new class. At this time, the low-level method, i.e., the method that is often called, rather than calling another method, is moved first. Test each move one by one.
    메소드들도 새 클래스로 옮긴다.(함수 옮기기). 이때 저수준 메소드, 즉 다른 메소드를 호출하기 보다는 호출을 당하는 일이 많은 메소드부터 옮긴다. 하나씩 옮길 때마다 테스트한다.

  5. By looking at the interfaces of both classes, Remove unnecessary methods and rename them to fit the new environment.
    양쪽 클래스의 인터페이스를 살펴보면서 불필요한 메소드를 제거하고, 이름도 새로운 환경에 맞게 바꾼다.

  6. Specifies whether to expose the new class to the outside world. If you want to expose it, consider whether to apply a replacement reference to a new class.
    새 클래스를 외부로 노출할지 정한다. 노출하려거든 새 클래스에 참조를 값으로 바꾸기를 적용할 지 고민해본다.

profile
Tea and Dessert

0개의 댓글