-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathFactoryMethod.kt
48 lines (42 loc) · 1.29 KB
/
FactoryMethod.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
package org.vld.sdp.creational
/**
* Article Product interface
*/
interface Article {
val name: String
}
/**
* Article Creator Factory Method interface
*/
interface ArticleCreator {
/**
* Creates Article Product (factory method)
*/
fun createArticle(name: String): Article
}
/**
* Modern Article Product interface implementation
*/
data class ModernArticle(override val name: String) : Article
/**
* Modern Article Creator Factory Method interface implementation
*
* Modern Article Creator singleton encapsulates the knowledge about the Modern Article single product
*/
object ModernArticleCreator : ArticleCreator {
// the only place where the concrete Modern Article Product class is referenced
override fun createArticle(name: String): Article = ModernArticle(name)
}
/**
* Fancy Article Product interface implementation
*/
data class FancyArticle(override val name: String) : Article
/**
* Fancy Article Creator Factory Method interface implementation
*
* Modern Article Creator singleton the encapsulates the knowledge about Fancy Article single product
*/
object FancyArticleCreator : ArticleCreator {
// the only place where the concrete Fancy Article Product class is referenced
override fun createArticle(name: String): Article = FancyArticle(name)
}