mirror of
https://github.com/Brandon-Rozek/website.git
synced 2024-11-21 15:56:29 -05:00
Edited post
This commit is contained in:
parent
14268bbca0
commit
7461010e46
1 changed files with 9 additions and 0 deletions
|
@ -136,3 +136,12 @@ Function 4: Goodbye, World!
|
||||||
Function 5: Goodbye, World!
|
Function 5: Goodbye, World!
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Alternative: Observer Pattern
|
||||||
|
|
||||||
|
The above approaches assume that the callbacks would be needed for the lifetime of the program. An altnerative is to use the [Observer design pattern](https://en.wikipedia.org/wiki/Observer_pattern).
|
||||||
|
|
||||||
|
This assumes that we have two classes, `Observable` and `Observer`. The latter class implements the `notify` method and when it is constructed, it adds itself to the collection contained in the `Observable` class.
|
||||||
|
|
||||||
|
[Martin](https://martinheinz.dev/blog/112) wrote a great blog post on how you can use Python weak references so that when your `Observer` class goes out of scope, a copy isn't stored in the `Observable` class and instead gets safely removed from the collection.
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue