Skip to content

[3장+4장_양수진] #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/kotlin/yangsooplus/ch03/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
### 객체지향의 본질은??
- 협력하는 객체들의 공동체를 창조하는 것
- 역할, 책임, 협력
- 협력: 객체들이 수행하는 상호작용
- 책임: 객체가 협력에 참여하기 위해 수행하는 로직
- 역할: 객체들이 협력 안에서 수행하는 책임들을 통튼 것

## 협력
- 두 객체가 협력하기 위한 수단은? 메시지 전송
- 객체는 다른 객체의 내부에 직접 접근하지 못하니까!
- 메세지를 받은 객체는? 메서드를 실행해 요청에 응답
- 내부 구현을 캡슐화 -> 객체를 자율화 -> 메세지 전송으로 협력

## 책임
- 하는 것 vs 아는 것
- 하는 것
- 객체를 생성하거나 계산을 수행하는 등의 스스로 하는 것
- 다른 객체의 행동을 시작시키는 것
- 다른 객체의 활동을 제어하고 조절하는 것
- 아는 것
- 사적인 정보에 관해 아는 것
- 관련된 객체에 관해 아는 것
- 자신이 유도하거나 계산할 수 있는 것에 관해 아는 것
- CRC 카드
- 협력에 참여하는 *후보*를 표현하는 카드
- 이름, 책임, 협력자들을 나열하여 표현하는 설계 기법
- 책임 할당하는 법
- 정보 전문가 패턴: 책임을 수행하는 데 필요한 정보를 가장 잘 알고 있는 전문가에게 책임 할당
-> 책임 주도 설계

## 역할
- 객체가 어떤 특정한 협력 안에서 수행하는 책임의 집합
- 특정 객체라고 생각하기 보다는 **추상화**된 슬롯으로 생각하자.
- 책임 메서드들을 총집한한 인터페이스 -> 역할!
6 changes: 6 additions & 0 deletions src/main/kotlin/yangsooplus/ch04/Customer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package yangsooplus.ch04

class Customer(
val name: String,
val id: String
)
27 changes: 27 additions & 0 deletions src/main/kotlin/yangsooplus/ch04/DiscountCondition.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package yangsooplus.ch04

import java.lang.IllegalArgumentException
import java.time.DayOfWeek
import java.time.LocalTime

class DiscountCondition(
val type: DiscountConditionType,
private val sequence: Int,
private val dayOfWeek: DayOfWeek,
private val startTime: LocalTime,
private val endTime: LocalTime
) {
fun isDiscountable(dayOfWeek: DayOfWeek, time: LocalTime): Boolean {
if (type != DiscountConditionType.PERIOD) throw IllegalArgumentException()
return this.dayOfWeek == dayOfWeek && startTime <= time && endTime >= time
}

fun isDiscountable(sequence: Int): Boolean {
if (type != DiscountConditionType.SEQUENCE) throw IllegalArgumentException()
return this.sequence == sequence
}
}

enum class DiscountConditionType{
SEQUENCE, PERIOD
}
48 changes: 48 additions & 0 deletions src/main/kotlin/yangsooplus/ch04/Movie.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package yangsooplus.ch04

import yangsooplus.ch02.Money
import java.time.Duration
import java.time.LocalDateTime

class Movie(
val title: String,
val runningTime: Duration,
private val fee: Money,
val discountConditions: List<DiscountCondition>,

val movieType: MovieType,
private val discountAmount: Money,
private val discountPercent: Double
) {
fun calculateAmountDiscountedFee(): Money {
return fee.minus(discountAmount)
}

fun calculatePercentDiscountedFee(): Money {
return fee.minus(fee.times(discountPercent))
}

fun calculateNoneDiscountedFee(): Money = fee

fun isDiscountable(whenScreened: LocalDateTime, sequence: Int): Boolean {
discountConditions.forEach { condition ->
when (condition.type) {
DiscountConditionType.PERIOD -> {
if (condition.isDiscountable(whenScreened.dayOfWeek, whenScreened.toLocalTime()))
return true
}

DiscountConditionType.SEQUENCE -> {
if (condition.isDiscountable(sequence))
return true
}
}
}
return false
}

}

enum class MovieType {
AMOUNT_DISCOUNT, PERCENT_DISCOUNT, NONE_DISCOUNT
}
22 changes: 22 additions & 0 deletions src/main/kotlin/yangsooplus/ch04/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### 응집도
- 모듈에 포함된 내부 요소들이 연관되어 있는 정도
- 하나의 목적을 위해 협력할수록 ⬆
- 서로 다른 목적을 추구한다면 ⬇

### 결합도
- 의존성의 정도. 다른 모듈에 대한 지식을 얼마나 갖고 있는ㄴ지
- 다른 모듈의 너무 자세한 부분까지 알고있으면 ⬆

### 🤔 이론적으로는 납득이 되는데, 실제 코드가 응집도와 결합도가 높은지 낮은지 어떻게 알지?

#### 변경 발생 시 모듈 내부에서 발생하는 변경의 정도로 측정 가능

- 하나의 변경으로 모듈 전체가 함께 변경? -> 응집도 높음

- A모듈의 변경으로 A 모듈만이 아닌 B, C 모듈도 함께 변경? -> 응집도 낮음
(B, C의 변경된 부분이 사실은 A 모듈에 있어야 하는 코드 = 책임이 흩어져버렸다!)
- 하나의 모듈이 변경되기 위해 다른 모듈이 많이 변경됨 -> 결합도 높음

단, 꼭 결합도가 높다고 해서 무조건 나쁜 코드는 아님.

ex) 표준 라이브러리, 성숙 단계의 프레임워크에 의존하는 경우. (변경될리가 없음)
10 changes: 10 additions & 0 deletions src/main/kotlin/yangsooplus/ch04/Reservation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package yangsooplus.ch04

import yangsooplus.ch02.Money

class Reservation(
val customer: Customer,
val screening: Screening,
val fee: Money,
val audienceCount: Int
)
10 changes: 10 additions & 0 deletions src/main/kotlin/yangsooplus/ch04/ReservationAgency.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package yangsooplus.ch04



class ReservationAgency {

fun reserve(screening: Screening, customer: Customer, audienceCnt: Int): Reservation {
return Reservation(customer, screening, screening.calculateFee(audienceCnt), audienceCnt)
}
}
20 changes: 20 additions & 0 deletions src/main/kotlin/yangsooplus/ch04/Screening.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package yangsooplus.ch04

import yangsooplus.ch02.Money
import java.time.LocalDateTime

class Screening(
private val movie: Movie,
val sequence: Int,
val whenScreened: LocalDateTime
) {

fun calculateFee(audienceCnt: Int): Money {
return when (movie.movieType) {
MovieType.AMOUNT_DISCOUNT -> movie.calculateAmountDiscountedFee()
MovieType.PERCENT_DISCOUNT -> movie.calculatePercentDiscountedFee()
MovieType.NONE_DISCOUNT -> movie.calculateNoneDiscountedFee()
}.times(audienceCnt)
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package yangsooplus

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.DisplayName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package yangsooplus

import yangsooplus.ch02.Customer
import yangsooplus.ch02.Money
import yangsooplus.ch02.Movie
Expand Down