Observer Pattern in Java and C#
Tigraine
Daniel Hoelbling talks about programming and technology

Observer Pattern in Java and C#

December 12, 2007 by Daniel Hoelbling
Since yesterday I am reading my brand new Head First - Design Patterns and really enjoy doing so. The book is funny, informative and it's quite unique style really ... works. Most articles I read on Design Patterns where (while informative) really hard to understand and made them a pain to read. So I was thrilled to find a book that was entertaining and informative. So, I tried some of that stuff out. Obviously I didn't make it past Chapter 2 till now and am still working on the Observer Pattern. The samples in the book are all in Java, so I immediately tried to translate them to C#. I looked for the Observable-Class in the .NET Framework, no clue. I searched the Web, and it looked like C# has no built-in support for the Observer Pattern. Even Wikipedia has a C# Example on their Observer Pattern page that is just a plain rewrite of the standard Java Examples. But still no sign of something like an Observable-Class. Until I figured out that the Observer Pattern is hardcoded into the C# Language as part of their Event System using an Event Pattern!

Implementation

While writing this Article I assumed that you are already familiar with the Observer Pattern! First of all, the standard implementation works both in C# and Java. So if the Event Pattern in C# isn't applicable, you can still fallback to the standard implementation. So, let's get into things. As always told, we start off by defining our Subject Class (You should always program towards an Interface, but for simplicity I used a Class in this Article): Observer_Subject This is the usual Observeable Class. 3 Methods (Attach, Detach and Notify) and one function to send out the Notification (In Java it would set the Observeable.setChanged-Method and should also change a State) Now, this is the Implementation for our Subject I came up with after applying the Event Pattern: Event_Subject So, hell yes this looks simpler doesn't it? The only catch is that you can't fire the Notify event if there are no subscribers because in that case it is null. That's why you need to check it before firing. But how does this thing work for our Observer (Subscriber)? Here is the Code that would Observe the standard Java-like Observer: Observer_Observer Note that this Class uses .Attach to subscribe to our Subject. While the C# implementation just hooks up an event to a function. That function doesn't need be called update() as the above, it has only to match the delegate we defined in our subject! Event_Observer So. The implementation of an Observer Pattern in C# is basically 3 lines of code.

Limitations

At the beginning of this article I said this Event Pattern comes with limitations. So, here are some of them: The Observer Pattern has two Interfaces you program with, the Event Pattern depends on a delegate type that can't be defined in an interface. You need a class to hold that delegate besides the interface. You also miss a collection that holds your subscribed clients. So you can find out how many clients you have (through: "this.Notify.GetInvocationList().Length;"), but you can't directly access their instances (the Java-like implementation allows this). Besides these two? I can't think of any problems this very simple approach C# takes may cause.