-
Notifications
You must be signed in to change notification settings - Fork 14
/
AbstractFactory.kt
74 lines (64 loc) · 1.98 KB
/
AbstractFactory.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package org.vld.sdp.creational
/**
* Letter Product interface
*/
interface Letter {
val name: String
}
/**
* Resume Product interface
*/
interface Resume {
val name: String
}
/**
* Document Creator Abstract Factory interface
*/
interface DocumentCreator {
/**
* Creates Letter Product
*/
fun createLetter(name: String): Letter
/**
* Creates Resume Product
*/
fun createResume(name: String): Resume
}
/**
* Modern Letter Product interface implementation
*/
data class ModernLetter(override val name: String) : Letter
/**
* Modern Resume Product interface implementation
*/
data class ModernResume(override val name: String) : Resume
/**
* Modern Document Creator Abstract Factory interface implementation
*
* Modern Document Creator singleton encapsulates the knowledge about the modern documents product family
*/
object ModernDocumentCreator : DocumentCreator {
// the only place where the concrete Modern Letter Product class is referenced
override fun createLetter(name: String): Letter = ModernLetter(name)
// the only place where the concrete Modern Resume Product class is referenced
override fun createResume(name: String): Resume = ModernResume(name)
}
/**
* Product interface implementation for fancy documents
*/
data class FancyLetter(override val name: String) : Letter
/**
* Product interface implementation for fancy documents
*/
data class FancyResume(override val name: String) : Resume
/**
* Fancy Document Creator Abstract Factory interface implementation
*
* Fancy Document Creator singleton encapsulates the knowledge about the fancy documents product family
*/
object FancyDocumentCreator : DocumentCreator {
// the only place where the concrete Fancy Letter Product class is referenced
override fun createLetter(name: String): Letter = FancyLetter(name)
// the only place where the concrete Fancy Resume Product class is referenced
override fun createResume(name: String): Resume = FancyResume(name)
}