From 131d291bb413174225ec8d5acef1cbfe0fff1255 Mon Sep 17 00:00:00 2001 From: Haeseong Jeon Date: Wed, 8 Sep 2021 21:51:19 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=98=88=EC=A0=9C=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\355\225\264\354\204\261/Chapter05.md" | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git "a/chapter05/\355\225\264\354\204\261/Chapter05.md" "b/chapter05/\355\225\264\354\204\261/Chapter05.md" index 13aff35..c3d4a06 100644 --- "a/chapter05/\355\225\264\354\204\261/Chapter05.md" +++ "b/chapter05/\355\225\264\354\204\261/Chapter05.md" @@ -134,4 +134,81 @@ https://johngrib.github.io/wiki/law-of-demeter/ - 연관관계의 주인은 외래키의 위치와 관련해서 정해야지 비즈니스 중요도로 접근하면 안된다. - 양방향 매핑시에는 무한루프에 빠지지 않게 조심해야한다. +## 예제 개선해보기 +```kotlin +class Team ( + private val members: MutableList +) { + 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() + } +} +```