Skip to content

Commit c515c06

Browse files
committed
ch01: 다형성 지원하기 위한 구조로 PerformanceCalculator 팩토리 변경
1 parent e7cf9a3 commit c515c06

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/main/kotlin/chapter01/CreateStatementData.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class EnrichedPerformance() {
2222
}
2323
}
2424

25-
class PerformanceCalculator {
25+
open class PerformanceCalculator {
2626
var aPerformance: Performance
2727
var play: Play? = null
2828
val amount: Int
@@ -64,6 +64,14 @@ class PerformanceCalculator {
6464
}
6565
}
6666

67+
class TragedyCalculator : PerformanceCalculator {
68+
constructor(aPerformance: Performance, aPlay: Play) : super(aPerformance, aPlay)
69+
}
70+
71+
class ComedyCalculator : PerformanceCalculator {
72+
constructor(aPerformance: Performance, aPlay: Play) : super(aPerformance, aPlay)
73+
}
74+
6775
internal fun createStatementData(plays: Plays, invoice: Invoice): StatementData {
6876
fun playFor(aPerformance: Performance): Play {
6977
return plays[aPerformance.playID]!!
@@ -94,5 +102,10 @@ internal fun createStatementData(plays: Plays, invoice: Invoice): StatementData
94102
return statementData
95103
}
96104

97-
private fun createPerformanceCalculator(aPerformance: Performance, aPlay: Play): PerformanceCalculator =
98-
PerformanceCalculator(aPerformance, aPlay)
105+
private fun createPerformanceCalculator(aPerformance: Performance, aPlay: Play): PerformanceCalculator {
106+
return when (aPlay.type) {
107+
"tragedy" -> TragedyCalculator(aPerformance, aPlay)
108+
"comedy" -> ComedyCalculator(aPerformance, aPlay)
109+
else -> throw Error("알 수 없는 장르: ${aPlay.type}")
110+
}
111+
}

0 commit comments

Comments
 (0)