From 64dca4f8bcb43194325d502366717bba9a0b10e1 Mon Sep 17 00:00:00 2001 From: victory316 Date: Thu, 6 Aug 2020 23:53:40 +0900 Subject: [PATCH 1/3] Creating new package to practice patterns --- .../java/com/example/algorithmstudy/SortUtil.kt | 17 +++++++++++++++++ .../java/com/example/algorithmstudy/SortTest.kt | 6 ++++++ 2 files changed, 23 insertions(+) diff --git a/app/src/main/java/com/example/algorithmstudy/SortUtil.kt b/app/src/main/java/com/example/algorithmstudy/SortUtil.kt index 08d0c90..b51c3dd 100644 --- a/app/src/main/java/com/example/algorithmstudy/SortUtil.kt +++ b/app/src/main/java/com/example/algorithmstudy/SortUtil.kt @@ -50,4 +50,21 @@ class SortUtil { return arr } + + fun doBubbleSort(arr: IntArray): IntArray { + var tempInt: Int + + for (x in 1 until arr.size) { + + for (y in 0 until arr.size - 1) { + if (arr[x] < arr[y]) { + tempInt = arr[x] + arr[x] = arr[y] + arr[y] = tempInt + } + } + } + + return arr + } } \ No newline at end of file diff --git a/app/src/test/java/com/example/algorithmstudy/SortTest.kt b/app/src/test/java/com/example/algorithmstudy/SortTest.kt index df47887..1158dc9 100644 --- a/app/src/test/java/com/example/algorithmstudy/SortTest.kt +++ b/app/src/test/java/com/example/algorithmstudy/SortTest.kt @@ -29,5 +29,11 @@ class SortTest { SortUtil().doInsertionSort(intArrayOf(9, 4, 3, 104, 1, 2)).forEach { print("[$it]") } + + println() + + SortUtil().doBubbleSort(intArrayOf(9, 4, 3, 104, 1, 2)).forEach { + print("[$it]") + } } } \ No newline at end of file From bf2637a452da03e684e414fd0872915e8dcb3c45 Mon Sep 17 00:00:00 2001 From: victory316 Date: Sun, 18 Oct 2020 16:28:26 +0900 Subject: [PATCH 2/3] Adding simple factories, now is all synced --- .idea/compiler.xml | 6 +++++ .idea/gradle.xml | 1 + .idea/misc.xml | 2 +- .idea/vcs.xml | 1 - .../com/example/algorithmstudy/pattern/Car.kt | 4 +-- .../algorithmstudy/pattern/DynamicFactory.kt | 27 +++++++++++++++++++ .../algorithmstudy/pattern/SimpleFactory.kt | 13 +++++++++ 7 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 .idea/compiler.xml create mode 100644 app/src/main/java/com/example/algorithmstudy/pattern/DynamicFactory.kt create mode 100644 app/src/main/java/com/example/algorithmstudy/pattern/SimpleFactory.kt diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..61a9130 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 5cd135a..9bba60d 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -14,6 +14,7 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index a76efc8..9b4a637 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index fdf1fc8..94a25f7 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,7 +1,6 @@ - \ No newline at end of file diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/Car.kt b/app/src/main/java/com/example/algorithmstudy/pattern/Car.kt index f310359..4c54fd0 100644 --- a/app/src/main/java/com/example/algorithmstudy/pattern/Car.kt +++ b/app/src/main/java/com/example/algorithmstudy/pattern/Car.kt @@ -1,6 +1,4 @@ -package com.example.algorithmstudy.pattern - -import kotlin.properties.Delegates +package com.example.algorithmstudy.pattern.creation /** * Builder 패턴 (Creational pattern) diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/DynamicFactory.kt b/app/src/main/java/com/example/algorithmstudy/pattern/DynamicFactory.kt new file mode 100644 index 0000000..c9f0367 --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/DynamicFactory.kt @@ -0,0 +1,27 @@ +package com.example.algorithmstudy.pattern.creation + +/** + * 동적 생성이 가능한 Factory로 newInstance의 파라미터에 따라 다른 객체를 생성해 반환. + */ +class DynamicFactory { + + fun getInstance(type: String): Any { + return when (type) { + "Korean" -> Korean() + "English" -> English() + else -> Korean() + } + } + + class Korean() { + fun getLanuage(): String { + return "korean" + } + } + + class English() { + fun getLanuage(): String { + return "english" + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/SimpleFactory.kt b/app/src/main/java/com/example/algorithmstudy/pattern/SimpleFactory.kt new file mode 100644 index 0000000..8cbcefe --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/SimpleFactory.kt @@ -0,0 +1,13 @@ +package com.example.algorithmstudy.pattern.creation + +/** + * 가장 단순한 Factory 패턴 + * + * - getInstance() 로 생성을 위임하고, 생성한 객체를 넘겨 + */ +class SimpleFactory { + + fun getInstance(): SimpleFactory { + return SimpleFactory() + } +} \ No newline at end of file From 8dc75f6f8fb01a4a4aafea01a456b1cdee676fa2 Mon Sep 17 00:00:00 2001 From: victory316 Date: Sun, 18 Oct 2020 16:44:04 +0900 Subject: [PATCH 3/3] New Factories --- .idea/codeStyles/Project.xml | 16 +++++++++ .../{ => algorithm}/SortUtil.kt | 2 +- .../pattern/{ => creation}/Car.kt | 0 .../pattern/{ => creation}/DynamicFactory.kt | 0 .../pattern/creation/FactoryMethod.kt | 33 +++++++++++++++++++ .../pattern/creation/Product.kt | 5 +++ .../pattern/{ => creation}/SimpleFactory.kt | 0 .../com/example/algorithmstudy/PatternTest.kt | 2 +- 8 files changed, 56 insertions(+), 2 deletions(-) rename app/src/main/java/com/example/algorithmstudy/{ => algorithm}/SortUtil.kt (97%) rename app/src/main/java/com/example/algorithmstudy/pattern/{ => creation}/Car.kt (100%) rename app/src/main/java/com/example/algorithmstudy/pattern/{ => creation}/DynamicFactory.kt (100%) create mode 100644 app/src/main/java/com/example/algorithmstudy/pattern/creation/FactoryMethod.kt create mode 100644 app/src/main/java/com/example/algorithmstudy/pattern/creation/Product.kt rename app/src/main/java/com/example/algorithmstudy/pattern/{ => creation}/SimpleFactory.kt (100%) diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 88ea3aa..3cc336b 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -1,6 +1,22 @@ + + diff --git a/app/src/main/java/com/example/algorithmstudy/SortUtil.kt b/app/src/main/java/com/example/algorithmstudy/algorithm/SortUtil.kt similarity index 97% rename from app/src/main/java/com/example/algorithmstudy/SortUtil.kt rename to app/src/main/java/com/example/algorithmstudy/algorithm/SortUtil.kt index b51c3dd..ba302ab 100644 --- a/app/src/main/java/com/example/algorithmstudy/SortUtil.kt +++ b/app/src/main/java/com/example/algorithmstudy/algorithm/SortUtil.kt @@ -1,4 +1,4 @@ -package com.example.algorithmstudy +package com.example.algorithmstudy.algorithm import java.util.* diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/Car.kt b/app/src/main/java/com/example/algorithmstudy/pattern/creation/Car.kt similarity index 100% rename from app/src/main/java/com/example/algorithmstudy/pattern/Car.kt rename to app/src/main/java/com/example/algorithmstudy/pattern/creation/Car.kt diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/DynamicFactory.kt b/app/src/main/java/com/example/algorithmstudy/pattern/creation/DynamicFactory.kt similarity index 100% rename from app/src/main/java/com/example/algorithmstudy/pattern/DynamicFactory.kt rename to app/src/main/java/com/example/algorithmstudy/pattern/creation/DynamicFactory.kt diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/creation/FactoryMethod.kt b/app/src/main/java/com/example/algorithmstudy/pattern/creation/FactoryMethod.kt new file mode 100644 index 0000000..6dc7b13 --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/creation/FactoryMethod.kt @@ -0,0 +1,33 @@ +package com.example.algorithmstudy.pattern.creation + +/** + * 매개변수 팩토리 메소드 패턴 + * + * - create에 별도로 parameter를 지정하지 않는 경우 단순 팩토리 메소드 패턴이 됨. + * - create에 파라미터를 지정함으로써 원하는 제품을 생성해 반환하도록 함. + */ +abstract class FactoryMethod { + enum class ProductModel { + SAMSUMG, + LG + } + + fun create(model: ProductModel): Product { + return when (model) { + ProductModel.SAMSUMG -> SamsumgProduct() + ProductModel.LG -> LgProduct() + } + } +} + +class LgProduct : Product { + override fun createProduct() { + println("Created LG Product") + } +} + +class SamsumgProduct : Product { + override fun createProduct() { + println("Created Samsung Product") + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/creation/Product.kt b/app/src/main/java/com/example/algorithmstudy/pattern/creation/Product.kt new file mode 100644 index 0000000..6b07694 --- /dev/null +++ b/app/src/main/java/com/example/algorithmstudy/pattern/creation/Product.kt @@ -0,0 +1,5 @@ +package com.example.algorithmstudy.pattern.creation + +interface Product { + fun createProduct() +} \ No newline at end of file diff --git a/app/src/main/java/com/example/algorithmstudy/pattern/SimpleFactory.kt b/app/src/main/java/com/example/algorithmstudy/pattern/creation/SimpleFactory.kt similarity index 100% rename from app/src/main/java/com/example/algorithmstudy/pattern/SimpleFactory.kt rename to app/src/main/java/com/example/algorithmstudy/pattern/creation/SimpleFactory.kt diff --git a/app/src/test/java/com/example/algorithmstudy/PatternTest.kt b/app/src/test/java/com/example/algorithmstudy/PatternTest.kt index ef71011..eb775a4 100644 --- a/app/src/test/java/com/example/algorithmstudy/PatternTest.kt +++ b/app/src/test/java/com/example/algorithmstudy/PatternTest.kt @@ -1,6 +1,6 @@ package com.example.algorithmstudy -import com.example.algorithmstudy.pattern.Car +import com.example.algorithmstudy.pattern.creation.Car import org.junit.Test class PatternTest {