The observer pattern

The observer pattern is a pattern to enable the publisher/subscriber scenario between the classes of an application. In this pattern, there is an observable object that sends events to many observing objects registered to it for receiving events.

Let's take a look at the class diagram for the example implementation of the observer pattern:

So, our example is basically talking about a scenario in a user interface-based application where you have a KeyboardListener class that is listening to keyboard events and then there are some other classes that need to know about certain key events, for example, Caps Lock, Num Lock. In this case, our KeyboardListener class is a publisher of the event class, and our observers are subscriber classes that are interested in receiving the key events. In our example, our events that receive the observers are StatusBar and BalloonNotifier.

First, we show you the code of the important IKeyObserver interfaces for the observer:

    public interface IKeyObserver 
{
void Update(object anObject);
}

This is the code of IKeyObservable for the observable:

    public abstract class IKeyObservable 
{
private IList<IKeyObserver> _observers = new
List<IKeyObserver>();

public void AddObserver(IKeyObserver observer)
{
_observers.Add(observer);
}

public void RemoveObserver(IKeyObserver observer)
{
_observers.Remove(observer);
}

public void NotifyObservers(object anObject)
{
foreach (IKeyObserver observer in _observers)
{
observer.Update(anObject);
}
}
}

Now let's take a look at the implementation of the code for our observable class, that is, KeyboardListener:

    public class KeyboardListener : IKeyObservable 
{
public void SartListening()
{
ListenToKeys(); //Normally it would be a
continous process..
}

/// <summary>
/// Listen and notify only the interested keys
/// </summary>
private void ListenToKeys()
{
ObservedKeys key;

//Got the key, we are interested in..
key = ObservedKeys.NUM_LOCK;

NotifyObservers(key);
}
}

This is the code for our simple observing class StatusBar that receives events to show their key status in the display area of the status bar control:

    public class StatusBar : IKeyObserver 
{
public void Update(object anObject)
{
Trace.WriteLine("StatusBar - Key pressed: " +
anObject.ToString());
}
}

Now take a look at our sample client test code:

    public class ObserverTests 
{
[Fact]
public void Test_Observer_Pattern()
{
var listener = new KeyboardListener();
var statusBar = new StatusBar();
var balloonNotifier = new BalloonNotifier();

listener.AddObserver(statusBar);
listener.AddObserver(balloonNotifier);

listener.SartListening();

listener.RemoveObserver(balloonNotifier);
listener.SartListening(); //trigger a new
notification again
}
}

This code produces the following outcome:

StatusBar - Key pressed: NUM_LOCK
BalloonNotifier - Key pressed: NUM_LOCK
StatusBar - Key pressed: NUM_LOCK

The output confirms that when a key event, NUM_LOCK, is triggered, it is received by all the observers waiting for the key event, and we can dynamically add and remove the observers. This summarizes our sample for the observer pattern.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.47.25