
.NET 1.1+Observer Design Pattern (2)
The observer pattern is a design pattern that defines a link between objects so that when one object's state changes, all dependent objects are updated automatically. This pattern allows communication between objects in a loosely coupled manner.
Example Observer
In this section we will create a simple example of the observer design pattern using C#. This example will implement the message logging system described earlier in the article. The subject will be the class that receives messages to be logged and holds the details of the last message in its state.
We will create two observer classes to simulate emailing messages to an administrator or logging to the event log. In both cases we will omit the logging code, as this would over-complicate the sample. Instead we will write a message to the console.
The code for the logging system is as follows:
public abstract class EventReceiverBase
{
private ArrayList _monitors = new ArrayList();
public void Attach(EventMonitor monitor)
{
_monitors.Add(monitor);
}
public void Detach(EventMonitor monitor)
{
_monitors.Remove(_monitors);
}
public void Notify()
{
foreach (EventMonitor monitor in _monitors)
{
monitor.Update();
}
}
}
public class EventReceiver : EventReceiverBase
{
private string _lastMessage;
public string GetLastMessage()
{
return _lastMessage;
}
public void LogMessage(string message)
{
_lastMessage = message;
Notify();
}
}
public abstract class EventMonitor
{
public abstract void Update();
}
public class EventEmailer : EventMonitor
{
private EventReceiver _receiver;
public EventEmailer(EventReceiver receiver)
{
_receiver = receiver;
}
public override void Update()
{
string message = _receiver.GetLastMessage();
Console.WriteLine("Emailing: {0}", message);
}
}
public class EventLogger : EventMonitor
{
private EventReceiver _receiver;
public EventLogger(EventReceiver receiver)
{
_receiver = receiver;
}
public override void Update()
{
string message = _receiver.GetLastMessage();
Console.WriteLine("Logging: {0}", message);
}
}
7 June 2009