You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+32-4Lines changed: 32 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ Kotlin OOP and FP Design Patterns
14
14
*[ ][Mediator](#mediator)
15
15
*[ ][Memento](#memento)
16
16
*[ ][Null Object](#null-object)
17
-
*[][Observer](#observer)
17
+
*[x][Observer](#observer)
18
18
*[x][State](#state)
19
19
*[ ][Template](#template)
20
20
*[ ][Visitor](#visitor)
@@ -189,12 +189,40 @@ Null Object
189
189
190
190
**In progress**
191
191
192
-
Observer
192
+
[Observer](/src/main/kotlin/oop/Observer)
193
193
------------
194
194
195
195
> It defines a _one-to-many_ dependency between object so that when one changes its state, all its dependents are notified and updated automatically.
196
196
197
-
**In progress**
197
+
In Kotlin, this pattern is extremely easy to implement thanks to [property delegation](https://kotlinlang.org/docs/reference/delegated-properties.html)
newValue > oldValue ->println("A new customer entered. Current customers $newValue")
209
+
else->println("A customer left. Current customers: $newValue")
210
+
}
211
+
}
212
+
213
+
classShop(privatevalobserver:Observer<Int>) {
214
+
var currentCustomers by Delegates.observable(0) { _, old, new ->
215
+
observer.onValueChange(new, old)
216
+
}
217
+
}
218
+
```
219
+
220
+
### Usage
221
+
222
+
```kotlin
223
+
shop.currentCustomers++// prints "A new customer entered ..."
224
+
shop.currentCustomers--// prints "A customer left ..."
225
+
```
198
226
199
227
[State](/src/main/kotlin/oop/State)
200
228
-------
@@ -451,7 +479,7 @@ kitchen.cook()
451
479
452
480
> It attachs additional responsabilities to an object dynamically.
453
481
454
-
In Kotlin, we don't need to redefine the methods of the decorated interface. We can use `by` to delegate those methods to the decorated class.
482
+
In Kotlin, we don't need to redefine the methods of the decorated interface. We can use `by` to [delegate](https://kotlinlang.org/docs/reference/delegation.html) those methods to the decorated class.
0 commit comments