-
Notifications
You must be signed in to change notification settings - Fork 0
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
Implement abstract faotory example #35
Merged
Merged
Changes from all commits
Commits
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 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,26 @@ | ||
interface Button { | ||
val backgroundColor: Color | ||
val text: String | ||
|
||
fun onClick() | ||
} | ||
|
||
class LightThemeButton( | ||
override val text: String | ||
): Button { | ||
override val backgroundColor: Color = Color.WHITE | ||
|
||
override fun onClick() { | ||
println("Light theme button clicked: $text, $backgroundColor") | ||
} | ||
} | ||
|
||
class DarkThemeButton( | ||
override val text: String | ||
): Button { | ||
override val backgroundColor: Color = Color.BLACK | ||
|
||
override fun onClick() { | ||
println("Dark theme button clicked: $text, $backgroundColor") | ||
} | ||
} |
This file contains 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 @@ | ||
fun main(args: Array<String>) { | ||
// 사용자 입력에 따라서 라이트 or 다크 테마 UI 컴포넌트 생성 | ||
println("light or dark?") | ||
val factory = when (readLine()!!) { | ||
"light" -> LightThemeUIComponentFactory() | ||
"dark" -> DarkThemeUIComponentFactory() | ||
else -> throw IllegalArgumentException("light 또는 dark만 입력") | ||
} | ||
|
||
val button = factory.createButton("버튼!") | ||
val textView = factory.createTextView("텍스트뷰!") | ||
|
||
button.onClick() | ||
textView.printText() | ||
|
||
// 실행 예시 | ||
|
||
/* | ||
light or dark? | ||
>> light | ||
Light theme button clicked: 버튼!, java.awt.Color[r=255,g=255,b=255] | ||
Light theme text view: 텍스트뷰! | ||
*/ | ||
|
||
/* | ||
light or dark? | ||
>> dark | ||
Dark theme button clicked: 버튼!, java.awt.Color[r=0,g=0,b=0] | ||
Dark theme text view: 텍스트뷰! | ||
*/ | ||
|
||
/* | ||
light or dark? | ||
>> ㅁㄴㅇㄹ | ||
Exception in thread "main" java.lang.IllegalArgumentException: light 또는 dark만 입력 | ||
at MainKt.main(Main.kt:9) | ||
*/ | ||
} |
This file contains 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 @@ | ||
- 구성 요소(product)의 테마(factory)를 한꺼번에 바꾼다는 개념으로 이해했습니다. | ||
|
||
#### 예시 1 (Claud의 예시) | ||
|
||
- 제조사: 스마트폰, 노트북 | ||
- 애플: 아이폰, 맥북 | ||
- 삼성: 갤럭시, 갤럭시북 | ||
|
||
#### 예시 2 (Nike의 예시) | ||
|
||
- 테마: 버튼, 텍스트뷰 | ||
- 라이트 테마: 흰색 버튼, 흰색 텍스트뷰 | ||
- 다크 테마: 검은색 버튼, 검은색 텍스트뷰 | ||
|
||
#### 예시 3 (Nike의 예시 2, 예제 코드는 없음) | ||
|
||
- 세트 아이템: 모자, 의상, 무기, 신발 | ||
- 갑옷 세트: 투구, 체인메일, 한손검, 부츠 | ||
- 광인의 천옷세트: 두건, 개량한복, 단소, 지압 슬리퍼 | ||
This file contains 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 @@ | ||
interface TextView { | ||
val backgroundColor: Color | ||
val textColor: Color | ||
val text: String | ||
|
||
fun printText() | ||
} | ||
|
||
class LightThemeTextView( | ||
override val text: String | ||
): TextView { | ||
override val backgroundColor: Color = Color.WHITE | ||
override val textColor: Color = Color.BLACK | ||
|
||
override fun printText() { | ||
println("Light theme text view: $text") | ||
} | ||
} | ||
|
||
class DarkThemeTextView( | ||
override val text: String | ||
): TextView { | ||
override val backgroundColor: Color = Color.BLACK | ||
override val textColor: Color = Color.WHITE | ||
|
||
override fun printText() { | ||
println("Dark theme text view: $text") | ||
} | ||
} |
This file contains 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 @@ | ||
interface UiComponentFactory { | ||
fun createButton(text: String): Button | ||
fun createTextView(text: String): TextView | ||
} | ||
|
||
class LightThemeUIComponentFactory: UiComponentFactory { | ||
override fun createButton(text: String): Button { | ||
return LightThemeButton(text) | ||
} | ||
|
||
override fun createTextView(text: String): TextView { | ||
return LightThemeTextView(text) | ||
} | ||
} | ||
|
||
class DarkThemeUIComponentFactory: UiComponentFactory { | ||
override fun createButton(text: String): Button { | ||
return DarkThemeButton(text) | ||
} | ||
|
||
override fun createTextView(text: String): TextView { | ||
return DarkThemeTextView(text) | ||
} | ||
} |
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.
예제 코드를 보고 싶은 예시네요 ㅋㅋ
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.
예제가 재미있네요 ㅋㅋ