Dave Burke : Freelance .NET Web Developer specializing in Online Communities

The Observer Pattern

An element of IssueVision that I've taken for my Smart Client application is the Observer pattern.  If you Google IObserver you'll find some good articles, but they're usually way too complicated.  Hopefully this post helps simplify the Observer pattern for those interested in it.  This is also covered in the DevDays 2004 Smart Track presentation #1 and of course in the IV source.

The Observer pattern is how you coordinate multiple UI views of the same set of data.  You have a SUBJECT, which is the common set of core data we're interested in.  For IssueVision the subject is all about staff, issues, issue history and issue details.

With the IssueVision UI as a guide (shown at bottom), there are multiple panes that care about the subject and need to know when the subject changes or is modified.  The panes need to be able to tell the subject when they need to change the current record, change the display of a record count, a statusbar messages, etc.

We use the observer pattern to wire up the various observers of the subject data. All observer changes in focus or selection or whatever, pass through the subject. Then when a subject detects a change that the different panes may care about, it sends out an event. 

Let's take the Synchronization_Finished action, for instance.  In the IssueSubject.cs we create our delegate, the event, and fire it when any synchronization process completes.  Here's what that looks like.

public delegate void Synchronization_FinishedEventHandler(object sender, EventArgs e);
public virtual event Synchronization_FinishedEventHandler Synchronization_Finished;

// Then, when a synchronization is completed in IssueSubject.cs it fires the event.

Synchronization_Finished(this, EventArgs.Empty);

// Which goes out to the observers, like the MainForm, for instance which executes its Synchronization_Finished event method.

private void IssueSubject_Synchronization_Finished(object sender, System.EventArgs e)
{
 UpdateOnlineStatus();
}

Then in the Main Form, we create an instance of the Subject and each pane's subject--each pane has a member variable called subject which is simply a reference to the subject. They all have a pointer to it in other words.   (Keep in mind that while IV provides a fully working Observer pattern, it's UI is rather simplistic with all usercontrols residing pretty much on the mainform.  This would not be the case in the Real World.)  Here's what the Main Form reference and initialization looks like.

m_issueSubject = new IssueSubject(this.components);

paneStaff.Subject = m_issueSubject;
paneMiddle.Subject = m_issueSubject;
// etc...

m_issueSubject.Synchronization_Finished += new IssueSubject.Synchronization_FinishedEventHandler(IssueSubject_Synchronization_Finished);
// etc...

m_issueSubject.Initialize();

So now, whenever the observers change the state of the subject, all other observers are affected.  An example in IssueVision would be clicking on a staffer in the staff tree view resulting in the issue pane displaying issues applying only to that staff member.

There's a lot of intricate coordination going on, but what's nice is that you can build these panes independently of each other and the only thing you have to worry about are the events coming back from the subject.

So there ya go.  The Observer Pattern. 

 

Comments (8) | Post RSS RSS comment feed

Posted on 6/25/2005 3:08:00 PM by Dave Burke
Categories: .NET
Tags:

Related posts

Comments (8) -

6/25/2005 8:13:04 PM Permalink

Great write up on Observer.

Dave, you should pick up the book Head First Design Patterns, it's a great book to learn the GoF patterns.

Ali Aghareza |

6/26/2005 5:28:17 AM Permalink

Head First Design Patterns is very popular from its 52 customer reviews on Amazon.  I'd have to translate the java to C#, so that makes me hesitate a little.  What's GoF?  I know, its probably something obvious that I'm missing.

daveburke |

6/26/2005 6:01:14 AM Permalink

Head First Design Patterns is one of the best computer books ever -- hands down.  And they are making a C# version, but its nearly trivial to understand Java if you know C# -- there's very little reliance on framework specific things at the pattern level.  GoF = Gang of Four -- the original book on design patterns that was far too academic for my taste.

Paul Wilson |

6/26/2005 12:17:39 PM Permalink

Dave, C# is my weapon of choice and has been for a few years now.  I recently took a full-time position with a company that does little-to-no .NET work and was very happy when I started working with Java and JSP due to the fact that C# and ASP.NET are so closely based on Java and JSP.  I still prefer C# to Java (haven't tried the latest version of either, though) but as far as reading code in a book goes, you'll have no trouble at all understanding what is going on.

Also, I think being forced to read code in a different language (even when only marginally different) helps move me outside the comfortable space I am usually in and allows/forces me to think about things in a different way.  The end result of this is that I am probably a little stronger and better than if I had spent the same amount of time learning in C# or learning through techniques I already knew and used.

Ideas are more important than code.

Besides, you're talking C# and Java, not some strange, unrelated language like VB.NET...

Shannon J Hager |

6/26/2005 2:39:34 PM Permalink

Swingin' Shannon,  Very wise counsel there.  "Ideas are more important than code..."  Sweet.  And yeah, it WOULD be easier than reading through VB.NET. Smile  I'll put the book on my Wish List and then reconsider it next time I'm on a Click-fest.  

Paul, thanks for the GoF definition.  Like you said in your podcast interview with Wally, you have been doing this stuff a long time.  I definitely wasn't thinking about Design Patterns when the book came out in 1995.

daveburke |

6/27/2005 7:30:45 AM Permalink

I'm a fan of Head First Design Patterns as well.  I haven't read all the way through it yet but what I've read so far is very good.  They do a good job of explaining patterns in a way that "sticks" a little better then a lot of the other "patterns" books I've ready (GoF).

Steve |

9/13/2005 2:40:57 PM Permalink

Good explanation and I guess I missed it in my aggregator.  Yes, this is my second comment today - it felt like a Dave Burke's blog kind of day.

Don't know if you've picked up the Head First book yet but I blogged a little about it yesterday (blog.eriklane.com/.../1973.aspx)..mainly about Jeff Atwood's review of it but I'm really like the Observer Pattern stuff though.

Thanks again.

Erik Lane |

9/13/2005 6:18:23 PM Permalink

Erik Lane Day for me then!  I'll definitely have to give Head First a look, since Ali also recommended it and he's quite the coding stud.  I also read your post and comments.  Interesting to start from the back of the book.  Hmm, as for "real world" solutions, I'll have to draw my own conclusion.  But with Design Patterns, it seems enough just to "get" them.  

This Observer Pattern though, is something I'll be focusing on in my Code Camp 4 talk coming up.  Thanks for the reminder of its value...and for another comment! Smile

daveburke |


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke