feat : Design Pattern - 컴포지트(Composite) 패턴 구현

This commit is contained in:
banjjoknim
2022-11-14 09:38:01 +09:00
parent 6b2c3eced2
commit c93ab002c0
8 changed files with 122 additions and 0 deletions

View File

@@ -143,6 +143,13 @@
### 컴포지트(Composite) 패턴
#### 구현 요구사항
- 프랜차이즈 기업의 정산은 가맹점으로 가입된 모든 가게들의 정산을 포함한다.
- 프랜차이즈 기업은 다른 프랜차이즈 기업을 포함할 수 있다.
- 프랜차이즈 기업은 이름을 갖는다.
- 프랜차이즈 가맹점은 이름을 갖는다.
### 널(Null) 객체 패턴
## 참고자료

View File

@@ -0,0 +1,17 @@
package com.banjjoknim.soliddesignpatternsample.designpattern.composite.after
class FranchiseeApplication
fun main() {
val coffeeCorporation = FranchiseeCorporation("커피프린스")
coffeeCorporation.addStore(FranchiseeStore("1호점"))
coffeeCorporation.calculate()
println()
println("가맹점 정산을 완료했습니다.")
println()
val franchiseeCorporation = FranchiseeCorporation("모두의 프랜차이즈")
franchiseeCorporation.addStore(coffeeCorporation)
franchiseeCorporation.calculate()
}

View File

@@ -0,0 +1,21 @@
package com.banjjoknim.soliddesignpatternsample.designpattern.composite.after
class FranchiseeCorporation(
private val name: String,
private val stores: MutableList<Store> = mutableListOf()
) : Store {
fun addStore(store: Store) {
stores.add(store)
}
fun removeStore(store: Store) {
stores.remove(store)
}
override fun calculate() {
for (store in stores) {
store.calculate()
}
println("프랜차이즈 기업 [$name]의 정산을 완료했습니다.")
}
}

View File

@@ -0,0 +1,9 @@
package com.banjjoknim.soliddesignpatternsample.designpattern.composite.after
class FranchiseeStore(
val name: String
) : Store {
override fun calculate() {
println("프랜차이즈 가게 [$name]이(가) 정산을 진행합니다.")
}
}

View File

@@ -0,0 +1,5 @@
package com.banjjoknim.soliddesignpatternsample.designpattern.composite.after
interface Store {
fun calculate()
}

View File

@@ -0,0 +1,17 @@
package com.banjjoknim.soliddesignpatternsample.designpattern.composite.before
class FranchiseeApplication
fun main() {
val coffeeCorporation = FranchiseeCorporation("커피프린스")
coffeeCorporation.addStore(FranchiseeStore("1호점"))
coffeeCorporation.calculateAllStores()
println()
println("가맹점 정산을 완료했습니다.")
println()
val franchiseeCorporation = FranchiseeCorporation("모두의 프랜차이즈")
franchiseeCorporation.addCorporation(coffeeCorporation)
franchiseeCorporation.calculateAllCorporations()
}

View File

@@ -0,0 +1,37 @@
package com.banjjoknim.soliddesignpatternsample.designpattern.composite.before
class FranchiseeCorporation(
private val name: String,
private val corporations: MutableList<FranchiseeCorporation> = mutableListOf(),
private val stores: MutableList<FranchiseeStore> = mutableListOf()
) {
fun addCorporation(franchiseeCorporation: FranchiseeCorporation) {
corporations.add(franchiseeCorporation)
}
fun removeCorporation(franchiseeCorporation: FranchiseeCorporation) {
corporations.remove(franchiseeCorporation)
}
fun addStore(store: FranchiseeStore) {
stores.add(store)
}
fun removeStore(store: FranchiseeStore) {
stores.remove(store)
}
fun calculateAllCorporations() {
for (corporation in corporations) {
corporation.calculateAllStores()
}
println("프랜차이즈 기업 [$name]의 정산을 완료했습니다.")
}
fun calculateAllStores() {
for (store in stores) {
store.calculate()
}
println("프랜차이즈 기업 [$name]의 정산을 완료했습니다.")
}
}

View File

@@ -0,0 +1,9 @@
package com.banjjoknim.soliddesignpatternsample.designpattern.composite.before
class FranchiseeStore(
val name: String
) {
fun calculate() {
println("프랜차이즈 가게 [$name]이(가) 정산을 진행합니다.")
}
}