-
Notifications
You must be signed in to change notification settings - Fork 0
feat(builder): 빌더 패턴 추가 #16
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
Open
im-Lily
wants to merge
2
commits into
ria
Choose a base branch
from
ria#builder
base: ria
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } |
48 changes: 48 additions & 0 deletions
48
ria/gof/src/main/kotlin/com/gof/pattern/builder/after/DefaultTourBuilder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
|
|
||
| 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) | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
ria/gof/src/main/kotlin/com/gof/pattern/builder/after/TourDirector.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
ria/gof/src/main/kotlin/com/gof/pattern/builder/after/TourPlanBuilder.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
22
ria/gof/src/main/kotlin/com/gof/pattern/builder/before/App.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, "체크아웃") | ||
| } |
3 changes: 3 additions & 0 deletions
3
ria/gof/src/main/kotlin/com/gof/pattern/builder/before/DetailPlan.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
38
ria/gof/src/main/kotlin/com/gof/pattern/builder/before/TourPlan.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
19
ria/gof/src/test/kotlin/com/gof/pattern/builder/after/AppKtTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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("저녁 식사") | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
ria/gof/src/test/kotlin/com/gof/pattern/builder/before/AppKtTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
29
ria/java/gof/src/main/java/com/gof/pattern/builder/after/App.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| } | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
ria/java/gof/src/main/java/com/gof/pattern/builder/after/DefaultTourBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
ria/java/gof/src/main/java/com/gof/pattern/builder/after/TourDirector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
ria/java/gof/src/main/java/com/gof/pattern/builder/after/TourPlanBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
24
ria/java/gof/src/main/java/com/gof/pattern/builder/before/App.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, "체크아웃"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 로 구현하면 어떻게 될까요?