Skip to content
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

feat: 예제 코드 추가 #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
77 changes: 77 additions & 0 deletions chapter05/해성/Chapter05.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,81 @@ https://johngrib.github.io/wiki/law-of-demeter/
- 연관관계의 주인은 외래키의 위치와 관련해서 정해야지 비즈니스 중요도로 접근하면 안된다.
- 양방향 매핑시에는 무한루프에 빠지지 않게 조심해야한다.

## 예제 개선해보기
```kotlin
class Team (
private val members: MutableList<Member>
) {
internal fun add(member: Member) {
if (!members.contains(member)) {
members.add(member)
}
}
internal fun remove(member: Member) {
members.remove(member)
}
}

class Member(
var team: Team?
) {
internal fun hasTeam(): Boolean {
return team != null
}
internal fun isIn(team: Team): Boolean {
return this.team?.let { it == team } ?: false
}
internal fun isInOtherTeam(team: Team): Boolean {
return hasTeam() && !isIn(team)
}
internal fun join(team: Team) {
this.team = team
}
internal fun withdraw() {
this.team = null
}
}

class JoinService {
fun join(
member: Member,
team: Team
) {
if (member.hasTeam()) {
throw IllegalStateException()
}
if (member.isIn(team)) {
throw IllegalStateException()
}
member.join(team)
team.add(member)
}
}

class ChangeService(
private val joinService: JoinService,
private val withdrawalService: WithdrawalService
) {
fun change(
member: Member,
team: Team
) {
if (member.isInOtherTeam(team)) {
withdrawalService.withdraw(member)
}
joinService.join(member, team)
}
}

class WithdrawalService {
fun withdraw(
member: Member
) {
if (!member.hasTeam()) {
throw IllegalStateException()
}
member.team?.remove(member)
member.withdraw()
}
}
```