Encapsultion(3) - Replace Primitive with Object

Kerem Song·2021년 4월 5일
0

Refactoring - Study

목록 보기
16/22

Replace Primitive with Object(기본형을 객체로 바꾸기)

Create class for data
(데이터를 위한 클래스 만들기)

orders.filter(o => "high" === o.priority || "rush" === o.priority)

to

orders.filter(o => o.priority.higherThan(new Priority("normal")))

Motivation

  1. Encapsulate behaviour with data
    데이터 캡슐화

Procedure

  1. Encapsulate a variable if it is not already encapsulated.
    아직 변수를 캡슐화하지 않았다면 캡슐화한다.

  2. Create a simple value class. The constructor takes and stores the existing value as an argument and adds a getter to return it.
    단순한 값 클래스를 만든다. 생성자는 기존 값을 인수로 받아서 저장하고, 이 값을 반환하는 게터(getter)를 추가한다.

  3. Static inspection is carried out
    정적 검사를 수행한다.

  4. Modify the setter to create a new instance of the value class and store it in the field. If already, change the type of field accordingly.
    값 클래스의 인스턴스를 새로 만들어서 필드에 저장하도록 세터(setter)를 수정한다. 이미 있다면 필드의 타입을 적절히 변경한다.

  5. Modify the getter to return the result of calling the getter of the newly created class.
    새로 만든 클래스의 게터를 호출한 결과를 반환하도록 게터를 수정한다.

  6. Test it.
    테스트한다.

  7. Examine whether renaming a function can better reveal the behavior of the original accessor.
    함수 이름을 바꾸면 원본 접근자의 동작을 더 잘 드러낼 수 있는지 검토한다.

profile
Tea and Dessert

0개의 댓글