Design Pattern Examples
Overview of object-oriented design patterns
ObserverSubject_NumberProducer.cs
Go to the documentation of this file.
1
19
20using System.Collections.Generic;
21
23{
33 public interface IObserverNumberChanged
34 {
40 }
41
42
43 //########################################################################
44 //########################################################################
45
46
61 public interface IEventNotifications
62 {
65 }
66
67
68 //########################################################################
69 //########################################################################
70
71
84 public interface INumberProducer
85 {
91 }
92
93
94 //########################################################################
95 //########################################################################
96
97
106 {
110 private List<IObserverNumberChanged> _observers = new List<IObserverNumberChanged>();
111
115 private uint _number;
116
117
122 {
123 // Copy the list so observers can change the original observers
124 // during the notification (this isn't strictly needed in this
125 // example but it is good practice for any notification system
126 // that handles multiple observers where multiple threads might
127 // be in play or observers can unsubscribe at any time, even in
128 // the event notification).
129 IObserverNumberChanged[] observers = _observers.ToArray();
130
131 foreach(IObserverNumberChanged observer in observers)
132 {
133 observer.NumberChanged();
134 }
135 }
136
137
141 public void Update()
142 {
143 ++_number;
145 }
146
147
148 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
149 // Implementation of the INumberProducer interface.
150 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
151
157 {
158 return _number;
159 }
160
161
162 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
163 // Implementation of the IEventNotifications interface.
164 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
165
179 {
180 if (!_observers.Contains(observer))
181 {
182 _observers.Add(observer);
183 }
184 }
185
186
199 {
200 if (_observers.Contains(observer))
201 {
202 _observers.Remove(observer);
203 }
204 }
205 }
206}
Represents the Subject in this example, in this case, a class that contains a single number that is u...
List< IObserverNumberChanged > _observers
The list of observers subscribed to this class instance.
void _NotifyNumberChanged()
Helper method to notify all observers that the number has changed.
void Update()
Update the number then notify all observers.
Represents a Subject that takes observers implementing the IObserverNumberChanged interface.
void SubscribeToNumberChanged(IObserverNumberChanged observer)
void UnsubscribeFromNumberChanged(IObserverNumberChanged observer)
Represents the Subject to the observers. This is the minimum needed by observers to get access to the...
uint FetchNumber()
Return the current value from the Subject.
Represents an observer to the Subject class. An observer implements this interface and then subscribe...
void NumberChanged()
This is called whenever the number in the ObserverSubject_NumberProducer object is changed.
The namespace containing all Design Pattern Examples implemented in C#.