-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTemplateMethod.kt
34 lines (28 loc) · 1.04 KB
/
TemplateMethod.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package org.vld.sdp.behavioral
/**
* Template abstract class defines the skeleton/structure of the algorithm
*/
abstract class Employee {
// invariant step of the algorithm is defined in the abstract class final method
fun getUp(): String = "Employee gets up"
// variable step of the algorithm is defined in the abstract method and is open for overriding
abstract fun work(): String
// invariant step of the algorithm is defined in the abstract class final method
fun sleep(): String = "Employee sleeps"
// the overall algorithm structure is defined in one operation in the abstract class final method
fun act(): List<String> = listOf(getUp(), work(), sleep())
}
/**
* Abstract Template specialization
*/
class Developer : Employee() {
// redefine the variable step of the algorithm
override fun work(): String = "Developer programs"
}
/**
* Abstract Template specialization
*/
class Architect : Employee() {
// redefine the variable step of the algorithm
override fun work(): String = "Architect designs"
}