|
2 | 2 |
|
3 | 3 | The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. |
4 | 4 |
|
| 5 | +## Problem |
| 6 | + |
| 7 | +* A one-to-many dependency between objects should be defined without making the objects tightly coupled. |
| 8 | +* It should be ensured that when one object changes state an open-ended number of dependent objects are updated automatically. |
| 9 | +* It should be possible that one object can notify an open-ended number of other objects. |
| 10 | + |
| 11 | +## Solution |
| 12 | + |
| 13 | +* Define `Subject` and `Observer` Objects |
| 14 | +* When a subject changes state, all registered observers are notified and updated automatically. |
| 15 | + |
| 16 | +## Common Structure |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +- Subject (IObservable) |
| 21 | + - provides an interface for attaching and detaching Observer objects. |
| 22 | +- ConcreteSubject (WeatherData) |
| 23 | + - knows its observers. |
| 24 | + - sends a notification to its observers when its state changes |
| 25 | +- Observer (IObserver) |
| 26 | + - defines an updating interface for objects that should be notified of changes in a subject. |
| 27 | +- ConcreteObserver (CurrentConditionDisplay) |
| 28 | + - maintains a reference to a ConcreteSubject object |
| 29 | + - implements the Observer updating interface to keep its state consistent with the subject's. |
| 30 | + |
| 31 | + ## Collaboration |
| 32 | + |
| 33 | +- ConcreteSubject notifies its observers whenever a change occurs. |
| 34 | +- After being notified, a ConcreteObserver object may query the subject for information. |
| 35 | +- The Observer object that initiates the change request **postpone its update** until it gets a notification from the subject to avoid redundant updates. |
| 36 | +* Notify can be called by Subject, Observer or by any other object. |
| 37 | + |
| 38 | +## Benefits |
| 39 | + |
| 40 | +* Abstract coupling between Subject and Observer. |
| 41 | + * Subject is not coupled to concrete classes. All a subject knows is that it has a list of observers, each conforming to abstract `Observer` interface. |
| 42 | +* Observers can be attached and detached dynamically. |
| 43 | +* Support for broadcast communication |
| 44 | + * The subject doesn't care how many interested objects exist. The notification is broadcast automatically to all of them. It's up to the observer to handle or ignore a notification. |
| 45 | + |
| 46 | +## Drawbacks |
| 47 | + |
| 48 | +* Observers are notified in random order. |
| 49 | +* Unexpected updates |
| 50 | + * A seemingly innocuous operation on the subject may cause a cascade of updates to observers and their dependent objects. |
| 51 | + * If dependency criteria aren't well-defined or maintained, it can lead to spurious updates, which can be hard to track down. |
| 52 | + |
| 53 | +## Example |
| 54 | + |
5 | 55 | **Definition** |
6 | 56 | ```cs |
7 | 57 | // Observable / Subject |
@@ -53,24 +103,6 @@ The Observer Pattern defines a one-to-many dependency between objects so that wh |
53 | 103 | Console.ReadLine(); |
54 | 104 | ``` |
55 | 105 |
|
56 | | -## Common Structure |
57 | | - |
58 | | - |
59 | | - |
60 | | - |
61 | | -- Subject (IObservable) |
62 | | - - provides an interface for attaching and detaching Observer objects. |
63 | | -- ConcreteSubject (WeatherData) |
64 | | - - knows its observers. |
65 | | - - sends a notification to its observers when its state changes |
66 | | -- Observer (IObserver) |
67 | | - - defines an updating interface for objects that should be notified of changes in a subject. |
68 | | -- ConcreteObserver (CurrentConditionDisplay) |
69 | | - - maintains a reference to a ConcreteSubject object |
70 | | - - implements the Observer updating interface to keep its state consistent with the subject's |
71 | | - |
72 | | - _[Source: http://www.dofactory.com/net/observer-design-pattern]_ |
73 | | - |
74 | 106 | Checkout built-it Observer Pattern in .NET - [Observer Design Pattern Best Practices](https://msdn.microsoft.com/en-us/library/ff519622(v=vs.110).aspx) |
75 | 107 |
|
76 | 108 | # .NET Event |
@@ -112,4 +144,9 @@ weatherData.OnTemperatureChanged += this.OnTemperatureChanged; |
112 | 144 |
|
113 | 145 | // unsubscribe |
114 | 146 | weatherData.OnTemperatureChanged -= this.OnTemperatureChanged; |
115 | | -``` |
| 147 | +``` |
| 148 | + |
| 149 | +## Relations with Other Patterns |
| 150 | + |
| 151 | +- **Mediator** - By encapsulating complex update semantics, the ChangeManager acts as mediator between subjects and observers. |
| 152 | +- **Singleton**: The ChangeManager may use the Singleton pattern to make it unique and globally accessible. |
0 commit comments