The first part is the the class that manages the observer classes
1: public class EventManager<T>
2: { 3: private Dictionary<string, ISubscriber<T>> _subscribers = new Dictionary<string, ISubscriber<T>>();
4: public Dictionary<string,ISubscriber<T>> Subscribers
5: {6: get { return _subscribers; }
7: set { _subscribers = value; }
8: } 9: 10: public void Notify(T newState)
11: {12: foreach (var observer in Subscribers.Values)
13: { 14: observer.State = newState; 15: observer.Update(); 16: } 17: } 18: }Next let’s take a look at ISubscriber<T> interface
1: public interface ISubscriber<T>
2: { 3: T State { get; set; }4: void Update();
5: }1: public class TweetLocation : ISubscriber<Location>
2: {3: public ITwitter TwitterInterfacer = new FakeTwitterClient();
4: public Location State { get; set; }
5: 6: public void Update()
7: {8: TwitterInterfacer.TweetMessage(string.Format("currently at ({0},{1})",State.Lat,State.Lon));
9: } 10: }1: public class SaveLocation : ISubscriber<Location>
2: {3: public ILocationReposigtory LocationRepository = new FakeLocationRepository();
4: public static EventManager<string> ErrorHandler = new EventManager<string>();
5: 6: public Location State{get;set;}
7: 8: public SaveLocation()
9: {10: ErrorHandler.Subscribers.Add("ErrorMailer",new ErrorMailer());
11: } 12: 13: public void Update()
14: {15: bool success = LocationRepository.SaveLocation(State);
16: if (!success)
17: {18: ErrorHandler.Notify("Failed to save location");
19: } 20: } 21: }Finally here is an example of actually using it
1: class Program
2: {3: public static EventManager<Location> LocationHandler = new EventManager<Location>();
4: 5: static void Main(string[] args)
6: {7: LocationHandler.Subscribers.Add("Repository",new SaveLocation());
8: LocationHandler.Subscribers.Add("Twitter",new TweetLocation());
9: 10: Location currentLocation = new Location(){Lat = 111,Lon = -121};
11: 12: LocationHandler.Notify(currentLocation); 13: 14: Console.ReadKey(); 15: 16: currentLocation.Lon = 1211;17: LocationHandler.Subscribers.Remove("Twitter");
18: LocationHandler.Notify(currentLocation); 19: 20: Console.ReadKey(); 21: 22: 23: } 24: }A real world example of using “Observer Pattern” is a service bus like NServiceBus or MassTransit, where an event driven application needs to send messages to one or more subscribers.
As always here is a link to the project source.