Skip to content

Commit d205087

Browse files
authored
Merge pull request #20 from lee-ji-hoon/yangsooplus
[3장+4장_양수진]
2 parents 3bbd3e1 + 691cb70 commit d205087

File tree

10 files changed

+194
-0
lines changed

10 files changed

+194
-0
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
### 객체지향의 본질은??
2+
- 협력하는 객체들의 공동체를 창조하는 것
3+
- 역할, 책임, 협력
4+
- 협력: 객체들이 수행하는 상호작용
5+
- 책임: 객체가 협력에 참여하기 위해 수행하는 로직
6+
- 역할: 객체들이 협력 안에서 수행하는 책임들을 통튼 것
7+
8+
## 협력
9+
- 두 객체가 협력하기 위한 수단은? 메시지 전송
10+
- 객체는 다른 객체의 내부에 직접 접근하지 못하니까!
11+
- 메세지를 받은 객체는? 메서드를 실행해 요청에 응답
12+
- 내부 구현을 캡슐화 -> 객체를 자율화 -> 메세지 전송으로 협력
13+
14+
## 책임
15+
- 하는 것 vs 아는 것
16+
- 하는 것
17+
- 객체를 생성하거나 계산을 수행하는 등의 스스로 하는 것
18+
- 다른 객체의 행동을 시작시키는 것
19+
- 다른 객체의 활동을 제어하고 조절하는 것
20+
- 아는 것
21+
- 사적인 정보에 관해 아는 것
22+
- 관련된 객체에 관해 아는 것
23+
- 자신이 유도하거나 계산할 수 있는 것에 관해 아는 것
24+
- CRC 카드
25+
- 협력에 참여하는 *후보*를 표현하는 카드
26+
- 이름, 책임, 협력자들을 나열하여 표현하는 설계 기법
27+
- 책임 할당하는 법
28+
- 정보 전문가 패턴: 책임을 수행하는 데 필요한 정보를 가장 잘 알고 있는 전문가에게 책임 할당
29+
-> 책임 주도 설계
30+
31+
## 역할
32+
- 객체가 어떤 특정한 협력 안에서 수행하는 책임의 집합
33+
- 특정 객체라고 생각하기 보다는 **추상화**된 슬롯으로 생각하자.
34+
- 책임 메서드들을 총집한한 인터페이스 -> 역할!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package yangsooplus.ch04
2+
3+
class Customer(
4+
val name: String,
5+
val id: String
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package yangsooplus.ch04
2+
3+
import java.lang.IllegalArgumentException
4+
import java.time.DayOfWeek
5+
import java.time.LocalTime
6+
7+
class DiscountCondition(
8+
val type: DiscountConditionType,
9+
private val sequence: Int,
10+
private val dayOfWeek: DayOfWeek,
11+
private val startTime: LocalTime,
12+
private val endTime: LocalTime
13+
) {
14+
fun isDiscountable(dayOfWeek: DayOfWeek, time: LocalTime): Boolean {
15+
if (type != DiscountConditionType.PERIOD) throw IllegalArgumentException()
16+
return this.dayOfWeek == dayOfWeek && startTime <= time && endTime >= time
17+
}
18+
19+
fun isDiscountable(sequence: Int): Boolean {
20+
if (type != DiscountConditionType.SEQUENCE) throw IllegalArgumentException()
21+
return this.sequence == sequence
22+
}
23+
}
24+
25+
enum class DiscountConditionType{
26+
SEQUENCE, PERIOD
27+
}
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package yangsooplus.ch04
2+
3+
import yangsooplus.ch02.Money
4+
import java.time.Duration
5+
import java.time.LocalDateTime
6+
7+
class Movie(
8+
val title: String,
9+
val runningTime: Duration,
10+
private val fee: Money,
11+
val discountConditions: List<DiscountCondition>,
12+
13+
val movieType: MovieType,
14+
private val discountAmount: Money,
15+
private val discountPercent: Double
16+
) {
17+
fun calculateAmountDiscountedFee(): Money {
18+
return fee.minus(discountAmount)
19+
}
20+
21+
fun calculatePercentDiscountedFee(): Money {
22+
return fee.minus(fee.times(discountPercent))
23+
}
24+
25+
fun calculateNoneDiscountedFee(): Money = fee
26+
27+
fun isDiscountable(whenScreened: LocalDateTime, sequence: Int): Boolean {
28+
discountConditions.forEach { condition ->
29+
when (condition.type) {
30+
DiscountConditionType.PERIOD -> {
31+
if (condition.isDiscountable(whenScreened.dayOfWeek, whenScreened.toLocalTime()))
32+
return true
33+
}
34+
35+
DiscountConditionType.SEQUENCE -> {
36+
if (condition.isDiscountable(sequence))
37+
return true
38+
}
39+
}
40+
}
41+
return false
42+
}
43+
44+
}
45+
46+
enum class MovieType {
47+
AMOUNT_DISCOUNT, PERCENT_DISCOUNT, NONE_DISCOUNT
48+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### 응집도
2+
- 모듈에 포함된 내부 요소들이 연관되어 있는 정도
3+
- 하나의 목적을 위해 협력할수록 ⬆
4+
- 서로 다른 목적을 추구한다면 ⬇
5+
6+
### 결합도
7+
- 의존성의 정도. 다른 모듈에 대한 지식을 얼마나 갖고 있는ㄴ지
8+
- 다른 모듈의 너무 자세한 부분까지 알고있으면 ⬆
9+
10+
### 🤔 이론적으로는 납득이 되는데, 실제 코드가 응집도와 결합도가 높은지 낮은지 어떻게 알지?
11+
12+
#### 변경 발생 시 모듈 내부에서 발생하는 변경의 정도로 측정 가능
13+
14+
- 하나의 변경으로 모듈 전체가 함께 변경? -> 응집도 높음
15+
16+
- A모듈의 변경으로 A 모듈만이 아닌 B, C 모듈도 함께 변경? -> 응집도 낮음
17+
(B, C의 변경된 부분이 사실은 A 모듈에 있어야 하는 코드 = 책임이 흩어져버렸다!)
18+
- 하나의 모듈이 변경되기 위해 다른 모듈이 많이 변경됨 -> 결합도 높음
19+
20+
단, 꼭 결합도가 높다고 해서 무조건 나쁜 코드는 아님.
21+
22+
ex) 표준 라이브러리, 성숙 단계의 프레임워크에 의존하는 경우. (변경될리가 없음)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package yangsooplus.ch04
2+
3+
import yangsooplus.ch02.Money
4+
5+
class Reservation(
6+
val customer: Customer,
7+
val screening: Screening,
8+
val fee: Money,
9+
val audienceCount: Int
10+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package yangsooplus.ch04
2+
3+
4+
5+
class ReservationAgency {
6+
7+
fun reserve(screening: Screening, customer: Customer, audienceCnt: Int): Reservation {
8+
return Reservation(customer, screening, screening.calculateFee(audienceCnt), audienceCnt)
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package yangsooplus.ch04
2+
3+
import yangsooplus.ch02.Money
4+
import java.time.LocalDateTime
5+
6+
class Screening(
7+
private val movie: Movie,
8+
val sequence: Int,
9+
val whenScreened: LocalDateTime
10+
) {
11+
12+
fun calculateFee(audienceCnt: Int): Money {
13+
return when (movie.movieType) {
14+
MovieType.AMOUNT_DISCOUNT -> movie.calculateAmountDiscountedFee()
15+
MovieType.PERCENT_DISCOUNT -> movie.calculatePercentDiscountedFee()
16+
MovieType.NONE_DISCOUNT -> movie.calculateNoneDiscountedFee()
17+
}.times(audienceCnt)
18+
}
19+
20+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package yangsooplus
2+
3+
import org.junit.jupiter.api.Assertions
4+
import org.junit.jupiter.api.Assertions.*
5+
import org.junit.jupiter.api.DisplayName
6+
import org.junit.jupiter.api.Test
7+
8+
class MainKtTest {
9+
10+
@Test
11+
@DisplayName("이거 잘 됨?")
12+
fun `이거 잘 됨?`() {
13+
assertTrue(true)
14+
}
15+
}

src/test/kotlin/MovieTest.kt renamed to src/test/kotlin/yangsooplus/MovieTest.kt

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package yangsooplus
2+
13
import yangsooplus.ch02.Customer
24
import yangsooplus.ch02.Money
35
import yangsooplus.ch02.Movie

0 commit comments

Comments
 (0)