Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 함수형프로그래밍
- 조영호
- 스터디
- 도메인 주도 개발 시작하기
- 개발방법론
- 아키텍처
- Java
- TDD
- FP
- 계층형아키텍처
- 유지보수
- DDD
- Boot Legacy 차이점
- template
- 코틀린
- 만들면서배우는클린아키텍처
- Thymeleaf
- 테스트주도개발
- GrokkingFunctionalProgramming
- Kotlin
- 이펙티브코틀린
- Spring
- 테스트
- web
- 클린아키텍처
- 헥사고날아키텍처
- 개발서적
- 추상화 설계
- 객체지향의사실과오해
- 책스터디
Archives
- Today
- Total
김동형수 개발기
테스트 주도 개발 - 1부 3장 본문
3장 모두를 위한 평등
Dollar 객체같이 객체를 값처럼 쓸 수 있는데 이것을 값 객체 패턴 (value object pattern)이라고 한다.
값 객체의 대한 제약사항 중 하나는 객체의 인스턴스 변수가 생성자를 통해서 일단 설정된 후에는 결코 변하지 않는다는 것이다.
값 객체를 사용하면 별칭 문제에 대해 걱정할 필요가 없는 아주 큰 장점이 있다.
값 객체가 암시하는 것 중 하나는 2장에서와 같이 모든 연산은 새 객체를 반환해야 한다는 것이다.
또 다른 암시는 값 객체는 equals()를 구현해야 한다.
할일목록
- $5 + 10CHF = $10(환율이 2:1일 경우)
$5 X 2 = $10- amount를 private로 만들기
Dollar 부작용?- Money 반올림?
- equals()
- hashCode()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void testEquality() { | |
assertTrue(new Dollar(5).equals(new Dollar(5))); | |
} |
위 코드는 테스트에 실패한다. ( 컴파일 오류 )
그래서 가짜로 구현하기를 이용해서 아래처럼 코드를 작성한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dollar { | |
int amount; | |
public Dollar(int amount) { | |
this.amount = amount; | |
} | |
Dollar times(int multiplier) { | |
return new Dollar(amount * multiplier); | |
} | |
public boolean equals(Object object) { | |
return true; | |
} | |
} |
삼각측량을 이용한 코드의 일반화
두 개 이상의 예제가 있어야 코드를 일반화할 수 있다.
1. 5 == 5
2. 5 != 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void testEquality() { | |
assertTrue(new Dollar(5).equals(new Dollar(5))); | |
assertFalse(new Dollar(5).equals(new Dollar(6))); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dollar { | |
int amount; | |
public Dollar(int amount) { | |
this.amount = amount; | |
} | |
Dollar times(int multiplier) { | |
return Dollar(amount * multiplier); | |
} | |
public Boolean equals(Object object) { | |
Dollar dollar = (Dollar) object; | |
return amount == dollar.amount; | |
} | |
} |
할일목록
- $5 + 10CHF = $10(환율이 2:1일 경우)
$5 X 2 = $10- amount를 private로 만들기
Dollar 부작용?- Money 반올림?
equals()- hashCode()
times()를 일반화할 때도 삼각측량을 이용할 수 있었다
- $5 X 2 = $10
- $5 X 3 = $15
설계의 감이 오지 않을 때 저자는 삼각측량방법을 사용한다.
할일목록
- $5 + 10CHF = $10(환율이 2:1일 경우)
$5 X 2 = $10- amount를 private로 만들기
Dollar 부작용?- Money 반올림?
equals()- hashCode()
- Equal null
- Equal object
이번 장 내용정리
- 우리의 디자인 패턴(값 객체)이 하나의 또 다른 오퍼레이션을 암시한다는 걸 알아챘다.
- 해당 오퍼레이션을 테스트했다.
- 해당 오퍼레이션을 간단히 구현했다.
- 곧장 리펙토링하는 대신 테스트를 조금 더 했다.
- 두 경우를 모두 수용할 수 있도록 리펙토링했다.
'책 스터디 > [완료] 테스트 주도 개발' 카테고리의 다른 글
테스트 주도 개발 - 1부 6장 (0) | 2022.08.30 |
---|---|
테스트 주도 개발 - 1부 5장 (0) | 2022.08.30 |
테스트 주도 개발 - 1부 4장 (0) | 2022.08.30 |
테스트 주도 개발 - 1부 2장 (0) | 2022.08.29 |
테스트 주도 개발 - 1부 1장 (0) | 2022.08.29 |
Comments