Skip to content
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
9 changes: 9 additions & 0 deletions ria/gof/src/main/kotlin/com/gof/pattern/builder/after/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.gof.pattern.builder.after

class App

fun main() {
var director = TourDirector(DefaultTourBuilder())
val cancunTrip = director.cancunTrip()
val longBeachTrip = director.longBeachTrip()
}
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p1
코틀린에서 data class를 사용하면 getter, setter, constructor 를 만들어준다고 알고 있습니다.
data class 로 구현하면 어떻게 될까요?


var title: String? = null
var nights: Int = 0
var days: Int = 0
var startDate: LocalDate? = null
var whereToStay: String? = null
var plans: MutableList<DetailPlan> = mutableListOf()

override fun title(title: String): TourPlanBuilder {
this.title = title
return this
}

override fun nightsAndDays(nights: Int, days: Int): TourPlanBuilder {
this.nights = nights
this.days = days
return this
}

override fun startDate(localDate: LocalDate): TourPlanBuilder {
this.startDate = startDate
return this
}

override fun whereToStay(whereToStay: String): TourPlanBuilder {
this.whereToStay = whereToStay
return this
}

override fun addPlan(day: Int, plan: String): TourPlanBuilder {
if (this.plans == null) {
this.plans = mutableListOf()
}
plans.add(DetailPlan(day, plan))
return this
}

override fun getPlan(): TourPlan {
return TourPlan(title, nights, days, startDate, whereToStay, plans)
}
}
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(2024, 1, 3))
.whereToStay("리조트")
.addPlan(0, "체크인하고 짐 풀기")
.addPlan(0, "저녁 식사")
.getPlan()
}

fun longBeachTrip(): TourPlan {
return tourPlanBuilder.title("롱비치")
.startDate(LocalDate.of(2024, 3, 4))
.getPlan()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.gof.pattern.builder.after

import com.gof.pattern.builder.before.TourPlan
import java.time.LocalDate

interface TourPlanBuilder {

fun title(title: String): TourPlanBuilder
fun nightsAndDays(nights: Int, days: Int): TourPlanBuilder
fun startDate(localDate: LocalDate): TourPlanBuilder
fun whereToStay(whereToStay: String): TourPlanBuilder
fun addPlan(day: Int, plan: String): TourPlanBuilder
fun getPlan(): TourPlan
}
22 changes: 22 additions & 0 deletions ria/gof/src/main/kotlin/com/gof/pattern/builder/before/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.gof.pattern.builder.before

import java.time.LocalDate

class App

fun main() {
var shortTrip = TourPlan()
shortTrip.title = "세노떼 여행"
shortTrip.startDate = LocalDate.of(2023, 10, 11)

var tourPlan = TourPlan()
tourPlan.title = "칸쿤 여행"
tourPlan.nights = 2
tourPlan.days = 3
tourPlan.startDate = LocalDate.of(2024, 3, 2)
tourPlan.whereToStay = "리조트"
tourPlan.addPlan(0, "체크인 이후 짐풀기")
tourPlan.addPlan(1, "조식 뷔페에서 식사")
tourPlan.addPlan(1, "수영하기")
tourPlan.addPlan(2, "체크아웃")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.gof.pattern.builder.before

class DetailPlan(var day: Int, var plan: String)
38 changes: 38 additions & 0 deletions ria/gof/src/main/kotlin/com/gof/pattern/builder/before/TourPlan.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.gof.pattern.builder.before

import java.time.LocalDate

class TourPlan {

var title: String? = null
var nights: Int = 0
var days: Int = 0
var startDate: LocalDate? = null
var whereToStay: String? = null
var plans: MutableList<DetailPlan> = mutableListOf()

constructor()

constructor(
title: String?,
nights: Int,
days: Int,
startDate: LocalDate?,
whereToStay: String?,
plans: MutableList<DetailPlan>
) {
this.title = title
this.nights = nights
this.days = days
this.startDate = startDate
this.whereToStay = whereToStay
this.plans = plans
}

fun addPlan(day: Int, plan: String) {
if (this.plans == null) {
this.plans = mutableListOf()
}
this.plans.add(DetailPlan(day, plan))
}
}
19 changes: 19 additions & 0 deletions ria/gof/src/test/kotlin/com/gof/pattern/builder/after/AppKtTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.gof.pattern.builder.after

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class AppKtTest {

@Test
fun `빌더패턴 적용 후 성공 테스트`() {
// given
var director = TourDirector(DefaultTourBuilder())
// when
val cancunTrip = director.cancunTrip()
// then
assertThat(cancunTrip.title).isEqualTo("칸쿤 여행")
assertThat(cancunTrip.whereToStay).isEqualTo("리조트")
assertThat(cancunTrip.plans.map { it.plan }).contains("저녁 식사")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.gof.pattern.builder.before

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import java.time.LocalDate

class AppKtTest {

@Test
@DisplayName("빌더 패턴 적용 전 성공 테스트")
fun beforeBuilderSuccessTest() {
// given
val plan = TourPlan()
// when
plan.title = "세노떼 여행"
plan.startDate = LocalDate.of(2023, 11, 4)
plan.whereToStay = "리조트"
// then
assertThat(plan.title).isEqualTo("세노떼 여행")
assertThat(plan.whereToStay).isEqualTo("리조트")
}

@Test
@DisplayName("빌더 패턴 적용 전 실패 테스트")
fun beforeBuilderFailTest() {
// given
var title: String = "세노떼 여행"
var nights: Int = 2
var days: Int = 3
var startDate: LocalDate = LocalDate.of(2024, 7, 2)
var whereToStay: String = "리조트"
var plans: MutableList<DetailPlan> = mutableListOf()
// then
var plan = TourPlan(whereToStay, days, nights, startDate, title, plans)
// then
assertThat(plan.title).isEqualTo("세노떼 여행")
assertThat(plan.whereToStay).isEqualTo("리조트")
}
}
29 changes: 29 additions & 0 deletions ria/java/gof/src/main/java/com/gof/pattern/builder/after/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.gof.pattern.builder.after;

import com.gof.pattern.builder.before.TourPlan;

/**
* 빌더패턴
* 장점) 구체 로직 은닉, 다양한 빌더를 통해 다양한 로직 구현 가능
* 단점) 구조가 복잡해짐, 빌더 사용을 위한 인스턴스 생성
*/
public class App {
public static void main(String[] args) {
// DefaultTourBuilder builder = new DefaultTourBuilder();
// TourPlan plan = builder.title("칸쿤 여행")
// .nightsAndDays(2, 3)
// .startDate(LocalDate.of(2023, 8, 29))
// .whereToStay("리조트")
// .addPlan(0, "체크인하고 짐 풀기")
// .addPlan(0, "저녁 식사")
// .getPlan();
//
// TourPlan longBeachTrip = builder.title("롱비치")
// .startDate(LocalDate.of(2023, 10, 5))
// .getPlan();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p5
주석한 곳은 필요없으면 지워도 될 듯합니다.

TourDirector director = new TourDirector(new DefaultTourBuilder());
TourPlan cancunTrip = director.cancunTrip();
TourPlan longBeachTrip = director.longBeachTrip();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.gof.pattern.builder.after;

import com.gof.pattern.builder.before.DetailPlan;
import com.gof.pattern.builder.before.TourPlan;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class DefaultTourBuilder implements TourPlanBuilder {

private String title;
private int nights;
private int days;
private LocalDate startDate;
private String whereToStay;
private List<DetailPlan> plans;

@Override
public TourPlanBuilder title(String title) {
this.title = title;
return this;
}

@Override
public TourPlanBuilder nightsAndDays(int nights, int days) {
this.nights = nights;
this.days = days;
return this;
}

@Override
public TourPlanBuilder startDate(LocalDate startDate) {
this.startDate = startDate;
return this;
}

@Override
public TourPlanBuilder whereToStay(String whereToStay) {
this.whereToStay = whereToStay;
return this;
}

@Override
public TourPlanBuilder addPlan(int day, String plan) {
if (this.plans == null) {
this.plans = new ArrayList<>();
}
this.plans.add(new DetailPlan(day, plan));
return this;
}

@Override
public TourPlan getPlan() {
return new TourPlan(title, nights, days, startDate, whereToStay, plans);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.gof.pattern.builder.after;

import com.gof.pattern.builder.before.TourPlan;

import java.time.LocalDate;

public class TourDirector {

private TourPlanBuilder tourPlanBuilder;

public TourDirector(TourPlanBuilder tourPlanBuilder) {
this.tourPlanBuilder = tourPlanBuilder;
}

public TourPlan cancunTrip() {
return tourPlanBuilder.title("칸쿤 여행")
.nightsAndDays(2, 3)
.startDate(LocalDate.of(2023, 8, 29))
.whereToStay("리조트")
.addPlan(0, "체크인하고 짐 풀기")
.addPlan(0, "저녁 식사")
.getPlan();
}

public TourPlan longBeachTrip() {
return tourPlanBuilder.title("롱비치")
.startDate(LocalDate.of(2023, 10, 5))
.getPlan();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.gof.pattern.builder.after;

import com.gof.pattern.builder.before.TourPlan;

import java.time.LocalDate;

public interface TourPlanBuilder {

TourPlanBuilder title(String title);

TourPlanBuilder nightsAndDays(int nights, int days);

TourPlanBuilder startDate(LocalDate localDate);

TourPlanBuilder whereToStay(String whereToStay);

TourPlanBuilder addPlan(int day, String plan);

TourPlan getPlan();
}
24 changes: 24 additions & 0 deletions ria/java/gof/src/main/java/com/gof/pattern/builder/before/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.gof.pattern.builder.before;

import java.time.LocalDate;

public class App {

public static void main(String[] args) {
TourPlan shortTrip = new TourPlan();
shortTrip.setTitle("세노떼 여행");
shortTrip.setStartDate(LocalDate.of(2023, 11, 12));

TourPlan tourPlan = new TourPlan();
tourPlan.setTitle("칸쿤 여행");
tourPlan.setNights(2);
tourPlan.setDays(3);
tourPlan.setStartDate(LocalDate.of(2023, 11, 29));
tourPlan.setWhereToStay("리조트");
tourPlan.addPlan(0, "체크인 이후 짐풀기");
tourPlan.addPlan(0, "저녁 식사");
tourPlan.addPlan(1, "조식 뷔페에서 식사");
tourPlan.addPlan(1, "해변가 산책");
tourPlan.addPlan(2, "체크아웃");
}
}
Loading