Click here to Skip to main content
6,306,412 members and growing! (18,388 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Cross Platform » General     Intermediate License: The Code Project Open License (CPOL)

A Template Way to More Stable Observer Pattern

By Ma Xi

using template to sovle problems of observer pattern
VC6, VC7, VC7.1, VC8.0, Windows, Visual Studio, Dev
Posted:28 Oct 2006
Views:10,343
Bookmarked:13 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
8 votes for this article.
Popularity: 2.71 Rating: 3.00 out of 5
1 vote, 12.5%
1
2 votes, 25.0%
2
1 vote, 12.5%
3
1 vote, 12.5%
4
3 votes, 37.5%
5

Introduction

The Observer Pattern (also called the "Publish-Subscribe" or "Subject-Observer" Pattern) is one of the most well known patterns in developer community. There are also many articles talking about it. If you are not very familiar with observer pattern, you may like to check here first. In this article, I want to solve a slightly more stable version of the observer pattern which is particularly useful and time-saving in dealing with dynamically changed subject-observer relationships.

In a standard implementation, a subject holds an array of pointers referring to attached observers. Those attached observer should explicitly "detach" itself from the subject before dying. So the observers need to hold references back to the subject in some way. As you might realize, the two party hold references to each other. What if any subject or observer die in silence -- let's say an throwed exception skips the 'detach' call. Dangling pointers!

My template implementation offer you: A Observer doesn't hold reference to subjects, and a subject can die silently before its attached observers.  My templates also provides standard features -- automatically implementing bolstering codes (e.g. attaching/detaching and notifying).

Implementation

The attached source code should explain itself. Here, I would like to show some basic ideas.

basic idea

As shown in the above figure, a double-direction list is used for each subject. The benefit of a double-direction list is that the observer can delink from the chain itself at any time. So the delink function can be called from a destructor of a observer, which ensure that no dangling pointer left in the list.

The observer itself is actually not linked into the list directly. Instead, A observer owns an array of linkable nodes, which in turn link into the list. The nodes also act as proxy to forward notification messages to its owner, the observer.

Using the Code

Using the template is quite simple. Before I show you a simple example, make sure you have installed boost library. Let's say in a stock market application, you need write a price monitor (an observer) to alert user when the latest trade price changed. The below is the interface needs to implement.

struct PriceChangeListener
{
 virtual void OnChange(double latest_price) = 0;
};

A price monitor look like this:

class PriceMoniter : public LinkableObserverImpl<PriceChangeListener>
{ 
 virtual void OnChange(double latest_price)
 {
  cout << "Latest price changed to " << latest_price;
 }
}

A content subject look like this:

#include <boost/bind.hpp>

class PricePublisher : public SubjectSourceImpl<PriceChangeListener>
{
public:
 void SetLastestPrice(double latest_price_))
 {
   NotifyChanges(double latest_price_));  
 }

private:
 void NotifyChanges(double latest_price_)
 {
  TravelObservers(boost::bind(&PriceChangeListener::OnChange, _1, latest_price_)));
 }
}

Note that, all the attach and detach functions of PricePublisher is provided by the SubjectSourceImpl template. The only function the PricePublisher need to implement is NotifyChanges, which call the OnChange function of attached observers. Here, I'm using boost::bind to simplify the calling.

The below code snippet shows how to attach observer to subjects.

PricePublisher subject_A;
{
PriceMoniter observer_1;
subject_A.Attach(&observer_1);
// processing ...
// ...
// ...
subject_A.SetLastestPrice(2.35); //trigger observer 1
}
PriceMoniter observer_2;
subject_A.Attach(&observer_2);
subject_A.SetLastestPrice(2.40); //trigger observer 2 only
//....
The local variables observer_1, is inside a block, which will be destoried right out of the block. You can see that, there is no need to "detach" the observer_1.

As you might notice that, the code is shorter than a standard observer pattern implementation. So, have your fun with these templates.

Notice

  • Try not create Observers and Subjects in different threads, as I didn't implement any mutex operation when operating the linked list. Of course, you are free to add this feature on your own.
  • I uses boost library to easy my daily life. If you are not comfortable with boost library, also feel free to change to boost-less version.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ma Xi


Member
When you watching a TV, I'm coding in front of a PC.
Occupation: Web Developer
Location: Singapore Singapore

Other popular Cross Platform articles:

  • Introduction to Mono - Your first Mono app
    The first in a series of articles about Mono. This article explains how to install Mono and shows how to compile your first Cross Platform application.
  • MONO: an alternative for the .NET framework
    This article presents possibilities for development of .NET applications running on operating systems other than Windows, using the MONO platform. Advantages and challenges will be presented. Also presented are some common issues encountered while developing applications using the .NET technology.
  • Introduction to Mono - ASP.NET with XSP and Apache
    The second article in a series of articles about Mono. This article explains how to host and serve ASP.NET Web Applications and Web Services on Linux using XSP and Apache with the help of Mono.
  • Embed ActiveX controls inside Java GUI
    With this your Java projects can take advantage of ActiveX controls and Office documents such as spreadsheets, charts, calendars, word processors, specialized graphics, and many more.
  • Phalanger, PHP for .NET: Introduction for .NET developers
    Phalanger is a PHP language compiler for the .NET Framework which introduces PHP as a first-class .NET citizen.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralCompiler problem. Pinmemberahandke0:34 12 May '09  
GeneralRe: Compiler problem. PinmemberMa Xi2:31 18 May '09  
GeneralPlease left your precious comments for me to improve it. PinmemberMa Xi1:07 31 Oct '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Oct 2006
Editor:
Copyright 2006 by Ma Xi
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project