-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactory Method.kt
More file actions
42 lines (32 loc) · 748 Bytes
/
Factory Method.kt
File metadata and controls
42 lines (32 loc) · 748 Bytes
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
35
36
37
38
39
40
41
42
package design_patterns
/**
*
* A factory method is a generic design pattern that defines
*
* a common interface for creating objects in a superclass,
*
* allowing subclasses to change the type of objects they create.
*
*/
abstract class Pony4
class EarthPony4 : Pony4()
class Pegasus4 : Pony4()
class Unicorn4 : Pony4()
abstract class Place {
private var numberOfPonies = 0
abstract fun pony() : Pony4
fun newPony() : Pony4 {
numberOfPonies++
return pony()
}
fun count() = numberOfPonies
}
class Cloudsdale : Place() {
override fun pony() = Pegasus4()
}
class Canterlot : Place() {
override fun pony() = Unicorn4()
}
class Ponyville : Place() {
override fun pony() = EarthPony4()
}