/
Prototype.kt
71 lines (56 loc) · 1.83 KB
/
Prototype.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
package design_patterns
/**
*
* Prototype is a generative design pattern that allows you to copy objects
*
* without going into the details of their implementation.
*
*/
abstract class Sweets(
protected val title: String,
protected val description: String,
protected val price: Int
) {
// we only have a copy interface and don't know about its implementation
abstract fun copy(): Sweets
}
class OreoCookies(
title: String,
private val flavor: String,
price: Int
) : Sweets(
title = title,
price = price,
description = "Take a delicious break with OREO Chocolate Sandwich Cookies, America's favorite sandwich cookie for over 100 years..."
) {
// each implementation creates its own copy
override fun copy(): Sweets {
return OreoCookies(title, flavor, price)
}
override fun equals(other: Any?): Boolean {
if (other == null) return false
if (this === other) return true
if (other !is OreoCookies) return false
return title == other.title && description == other.description &&
flavor == other.flavor && price == other.price
}
}
class `M&MsChocolate`(
private val taste: String,
price: Int,
) : Sweets(
title = "M&M's chocolate",
price = price,
description = "M&M's are a chocolate jelly bean produced by Mars LLC. It first appeared in the United States in 1941 and is now sold in more than 100 countries..."
) {
override fun copy(): Sweets {
return `M&MsChocolate`(taste, price)
}
override fun equals(other: Any?): Boolean {
if (other == null) return false
if (this === other) return true
if (other !is `M&MsChocolate`) return false
return title == other.title && description == other.description &&
taste == other.taste && price == other.price
}
}