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

Implement abstract faotory example #35

Merged
merged 1 commit into from
Feb 15, 2022
Merged
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
26 changes: 26 additions & 0 deletions examples/Abstract_factory/Button.kt
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")
}
}
38 changes: 38 additions & 0 deletions examples/Abstract_factory/Main.kt
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)
*/
}
19 changes: 19 additions & 0 deletions examples/Abstract_factory/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- 구성 요소(product)의 테마(factory)를 한꺼번에 바꾼다는 개념으로 이해했습니다.

#### 예시 1 (Claud의 예시)

- 제조사: 스마트폰, 노트북
- 애플: 아이폰, 맥북
- 삼성: 갤럭시, 갤럭시북

#### 예시 2 (Nike의 예시)

- 테마: 버튼, 텍스트뷰
- 라이트 테마: 흰색 버튼, 흰색 텍스트뷰
- 다크 테마: 검은색 버튼, 검은색 텍스트뷰

#### 예시 3 (Nike의 예시 2, 예제 코드는 없음)

- 세트 아이템: 모자, 의상, 무기, 신발
- 갑옷 세트: 투구, 체인메일, 한손검, 부츠
- 광인의 천옷세트: 두건, 개량한복, 단소, 지압 슬리퍼
Copy link
Contributor

Choose a reason for hiding this comment

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

예제 코드를 보고 싶은 예시네요 ㅋㅋ

Copy link
Contributor

Choose a reason for hiding this comment

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

예제가 재미있네요 ㅋㅋ

29 changes: 29 additions & 0 deletions examples/Abstract_factory/TextView.kt
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")
}
}
24 changes: 24 additions & 0 deletions examples/Abstract_factory/UiComponentFactory.kt
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)
}
}