-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonostate.kt
34 lines (29 loc) · 729 Bytes
/
Monostate.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 design_patterns
/**
* pattern: MonoState
*
* using: used where you need to have common data for different objects of the same class
*
* description: The Monostate pattern is very similar to the Singleton pattern. The difference is
* that in the Singleton pattern we have only one object, while in Monostate the objects use the same data.
*/
class TextView {
companion object {
/**
* stores the number of TextView elements created
*
*/
private var count = 0
}
/**
*
* @return returns the number of TextView elements created
*/
fun textViewCount() = count
init {
count++
}
fun onDetachView() {
count--
}
}