diff --git a/zeri/gof/src/main/kotlin/com/gof/pattern/builder/after/DefaultTourBuilder.kt b/zeri/gof/src/main/kotlin/com/gof/pattern/builder/after/DefaultTourBuilder.kt new file mode 100644 index 0000000..4beef43 --- /dev/null +++ b/zeri/gof/src/main/kotlin/com/gof/pattern/builder/after/DefaultTourBuilder.kt @@ -0,0 +1,48 @@ +package com.gof.pattern.builder.after + +import com.gof.pattern.builder.before.DetailPlan +import com.gof.pattern.builder.before.TourPlan +import java.time.LocalDate + +class DefaultTourBuilder : TourPlanBuilder { + private var title: String = "" + private var nights: Int = 0 + private var days: Int = 0 + private var startDate: LocalDate = LocalDate.now() + private var whereToStay: String = "" + private var plans: MutableList? = null + + override fun nightsAndDays(nights: Int, days: Int): TourPlanBuilder { + this.nights = nights + this.days = days + return this + } + + override fun title(title: String): TourPlanBuilder { + this.title = title + return this + } + + override fun startDate(localDate: LocalDate): TourPlanBuilder { + this.startDate = localDate + return this + } + + override fun whereToStay(whereToStay: String): TourPlanBuilder { + this.whereToStay = whereToStay + return this + } + + override fun addPlan(day: Int, plan: String): TourPlanBuilder { + if (plans == null) { + plans = mutableListOf() + } + + plans?.add(DetailPlan(day, plan)) + return this + } + + override fun getPlan(): TourPlan { + return TourPlan(title, nights, days, startDate, whereToStay, plans ?: emptyList()) + } +} diff --git a/zeri/gof/src/main/kotlin/com/gof/pattern/builder/after/TourDirector.kt b/zeri/gof/src/main/kotlin/com/gof/pattern/builder/after/TourDirector.kt new file mode 100644 index 0000000..5e796e8 --- /dev/null +++ b/zeri/gof/src/main/kotlin/com/gof/pattern/builder/after/TourDirector.kt @@ -0,0 +1,22 @@ +package com.gof.pattern.builder.after + +import com.gof.pattern.builder.before.TourPlan +import java.time.LocalDate + +class TourDirector(private val tourPlanBuilder: TourPlanBuilder) { + fun cancunTrip(): TourPlan { + return tourPlanBuilder.title("칸쿤 여행") + .nightsAndDays(2, 3) + .startDate(LocalDate.of(2020, 12, 9)) + .whereToStay("리조트") + .addPlan(0, "체크인하고 짐 풀기") + .addPlan(0, "저녁 식사") + .getPlan() + } + + fun longBeachTrip(): TourPlan { + return tourPlanBuilder.title("롱비치") + .startDate(LocalDate.of(2021, 7, 15)) + .getPlan() + } +} diff --git a/zeri/gof/src/main/kotlin/com/gof/pattern/builder/after/TourPlanBuilder.kt b/zeri/gof/src/main/kotlin/com/gof/pattern/builder/after/TourPlanBuilder.kt new file mode 100644 index 0000000..03dd10a --- /dev/null +++ b/zeri/gof/src/main/kotlin/com/gof/pattern/builder/after/TourPlanBuilder.kt @@ -0,0 +1,13 @@ +package com.gof.pattern.builder.after + +import com.gof.pattern.builder.before.TourPlan +import java.time.LocalDate + +interface TourPlanBuilder { + fun nightsAndDays(nights: Int, days: Int): TourPlanBuilder + fun title(title: String): TourPlanBuilder + fun startDate(localDate: LocalDate): TourPlanBuilder + fun whereToStay(whereToStay: String): TourPlanBuilder + fun addPlan(day: Int, plan: String): TourPlanBuilder + fun getPlan(): TourPlan +} diff --git a/zeri/gof/src/main/kotlin/com/gof/pattern/builder/before/DetailPlan.kt b/zeri/gof/src/main/kotlin/com/gof/pattern/builder/before/DetailPlan.kt new file mode 100644 index 0000000..662cfc7 --- /dev/null +++ b/zeri/gof/src/main/kotlin/com/gof/pattern/builder/before/DetailPlan.kt @@ -0,0 +1,8 @@ +package com.gof.pattern.builder.before + +data class DetailPlan(var day: Int, var plan: String) { + + override fun toString(): String { + return "DetailPlan{ day= $day , plan = $plan }" + } +} diff --git a/zeri/gof/src/main/kotlin/com/gof/pattern/builder/before/TourPlan.kt b/zeri/gof/src/main/kotlin/com/gof/pattern/builder/before/TourPlan.kt new file mode 100644 index 0000000..60672f2 --- /dev/null +++ b/zeri/gof/src/main/kotlin/com/gof/pattern/builder/before/TourPlan.kt @@ -0,0 +1,22 @@ +package com.gof.pattern.builder.before + +import java.time.LocalDate + +data class TourPlan( + var title: String, + var nights: Int, + var days: Int, + var startDate: LocalDate, + var whereToStay: String, + var plans: List, +) { + constructor() : this("", 0, 0, LocalDate.now(), "", listOf()) + + override fun toString(): String { + return "TourPlan{ title= $title , nights = $nights, days = $days , startDate = $startDate , whereToStay = $whereToStay , plans = $plans }" + } + + fun addPlan(day: Int, plan: String) { + plans = plans + DetailPlan(day, plan) + } +} diff --git a/zeri/gof/src/test/kotlin/com/gof/pattern/builder/after/TourDirectorTest.kt b/zeri/gof/src/test/kotlin/com/gof/pattern/builder/after/TourDirectorTest.kt new file mode 100644 index 0000000..8404817 --- /dev/null +++ b/zeri/gof/src/test/kotlin/com/gof/pattern/builder/after/TourDirectorTest.kt @@ -0,0 +1,34 @@ +package com.gof.pattern.builder.after + +import com.gof.pattern.builder.before.DetailPlan +import com.gof.pattern.builder.before.TourPlan +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test +import java.time.LocalDate + +class TourDirectorTest { + @Test + @DisplayName("빌더 패턴이 정상적으로 작동한다") + fun builderPatternAfterTest() { + // given + val director = TourDirector(DefaultTourBuilder()) + + // when + val cancunPlan: TourPlan = director.cancunTrip() + val longBeachTrip: TourPlan = director.longBeachTrip() + + // then + assertEquals("롱비치", longBeachTrip.title) + assertEquals(LocalDate.of(2021, 7, 15), longBeachTrip.startDate) + + assertEquals("칸쿤 여행", cancunPlan.title) + assertEquals(2, cancunPlan.nights) + assertEquals(3, cancunPlan.days) + assertEquals(LocalDate.of(2020, 12, 9), cancunPlan.startDate) + assertEquals("리조트", cancunPlan.whereToStay) + assertEquals(2, cancunPlan.plans.size) + assertEquals(DetailPlan(0, "체크인하고 짐 풀기"), cancunPlan.plans[0]) + assertEquals(DetailPlan(0, "저녁 식사"), cancunPlan.plans[1]) + } +} diff --git a/zeri/gof/src/test/kotlin/com/gof/pattern/builder/before/TourPlanTest.kt b/zeri/gof/src/test/kotlin/com/gof/pattern/builder/before/TourPlanTest.kt new file mode 100644 index 0000000..6d82520 --- /dev/null +++ b/zeri/gof/src/test/kotlin/com/gof/pattern/builder/before/TourPlanTest.kt @@ -0,0 +1,46 @@ +package com.gof.pattern.builder.before + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.DisplayName +import org.junit.jupiter.api.Test +import java.time.LocalDate + +class TourPlanTest { + @Test + @DisplayName("빌더패턴 적용 전 테스트가 정상 작동한다.") + fun builderPatternBeforeTest() { + // given + val shortPlan = TourPlan() + val tourPlan = TourPlan() + + // when + shortPlan.title = "오레곤 롱비치 여행" + shortPlan.startDate = LocalDate.of(2021, 7, 15) + + tourPlan.title = "칸쿤 여행" + tourPlan.nights = 2 + tourPlan.days = 3 + tourPlan.startDate = LocalDate.of(2020, 12, 9) + tourPlan.whereToStay = "리조트" + tourPlan.addPlan(0, "체크인 이후 짐풀기") + tourPlan.addPlan(0, "저녁 식사") + tourPlan.addPlan(1, "조식 뷔페에서 식사") + tourPlan.addPlan(1, "해변가 산책") + tourPlan.addPlan(1, "점심은 수영장 근처에서 먹기") + tourPlan.addPlan(1, "리조트 수영장에서 놀기") + tourPlan.addPlan(1, "저녁은 BBQ 식당에서 스테이크") + tourPlan.addPlan(2, "조식 부페에서 식사") + tourPlan.addPlan(2, "체크아웃") + + // then + assertEquals("오레곤 롱비치 여행", shortPlan.title) + assertEquals(LocalDate.of(2021, 7, 15), shortPlan.startDate) + + assertEquals("칸쿤 여행", tourPlan.title) + assertEquals(2, tourPlan.nights) + assertEquals(3, tourPlan.days) + assertEquals(LocalDate.of(2020, 12, 9), tourPlan.startDate) + assertEquals("리조트", tourPlan.whereToStay) + assertEquals(9, tourPlan.plans.size) + } +}