Thursday, January 23, 2014

Head First Design Patterns - Part 1: Observer Pattern

My thoughts on patterns

I'll be honest with you...I never did like design patterns. I never got on board with it and, instead, just coded away like I thought I was supposed to do. Now, I realize they're a bit of a necessary evil to learn, for the main reason just to be able to speak to other developers about how they did this or that. With that in mind, I thought it would be a good idea to actually start learning these things. To do that, I turn to a fairly popular book on the subject - Head First Design Patterns.

The main reason I felt I should get into design patterns is from watching a presentation from Venkat Subramaniam. After watching Design Patterns for .NET Programmers I felt like I had an epiphany on what design patterns can do and what they're truly used for. You could say my mind was blown.

Observer Pattern

In this post I'll describe my understanding of the Observer Pattern, mostly gained from the material presented in the book and will (attempt to) put them into my own words so others like myself can gain a further understanding of design patterns and be able to apply them to their day-to-day coding.

As the name suggests, the Observer Pattern is a pattern where the important objects are "observed" or watched for changes. A physical example of what the Observer Pattern does can be the DVD service of Netflix. You subscribe to the service and they will send you the allotted number of DVDs available. Once you unsubscribe, you no longer get a DVD.

With .NET and C# there are actually a couple of different ways to use the Observer Pattern. There's always the classic way, using events/delegates, and the new IObservable<T> interface in the Base Class Library (BCL). Let's briefly go through each one.

With each of these implementations I'll try to wire up a very simple stock ticker. These will use the following Stock class:

 public class Stock
{
  public string Symbol { get; set; }
  public double Price { get; set; }
  public DateTime DateRecieved { get; set; }
}
Classic

This classic version of the pattern is what is described in the Gang of Four book on design patterns. This text pretty much set the tone in what design patterns are and they go through the most notable ones. Most of the ones you'll hear, I'm sure, are from this book.

In my example, I used interfaces, though in some implementations you may see an abstract base class being used instead. Below are the two interfaces I have for the Subject and the Observable.

 public interface ISubject
{
  void Subscribe(IObserver observer);
  void Unsubscribe(IObserver observer);
}

public interface IObserver
{
  void Update();
}
Pretty straight forward to see what each of these set up a contract for in each of the implementations: ISubject will Subscribe and Unsubscribe an IObserver object, which just does updates.

Let's look at the implementation of the ISubject interface on the StockTicker class:

 public class StockTicker : ISubject
{
  private readonly List<IObserver> _observers;
  private Stock _stock;
  
  public StockTicker()
  {
    _observers = new List<IObserver>();
  }
  
  public void Subscribe(IObserver observer)
  {
    _observers.Add(observer);
  }
  
  public void Unsubscribe(IObserver observer)
  {
    if (_observers.Contains(observer))
    {
        _observers.Remove(observer);
    }
  }
  
  public void Notify()
  {
    _observers.ForEach(i => i.Update());
  }
  
  public Stock Stock
  {
    get { return _stock; }
    set
    {
        _stock = value;
        this.Notify();
    }
  }
}