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(builder): 빌더 패턴 #17

Open
wants to merge 5 commits into
base: zeri
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -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<DetailPlan>? = 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())
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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 }"
}
}
Original file line number Diff line number Diff line change
@@ -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<DetailPlan>,
) {
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)
}
}
Original file line number Diff line number Diff line change
@@ -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])
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}