Posted on May 24th, 2009 at 20:43 by fr3@K
In a recent post of Scott Wheeler's - C and C++ are not the same language. He talked about differences between C and C++, and applications of different programming languages (C, C++, Java, Ruby) in his company.
In the post, Wheeler implemented observer pattern in C, C++ and Java, demonstrating some of his points - including how C and C++ are different from each other, and how C++ and Java are actually more alike.
Though I do agree with Wheeler's conclusion for the most part, I failed to share his view in C, C++ and Java comparison. IMHO, his observer implementation in C++ is rather old school, or should I say it's so Java.
In C++, oftentimes, there are more than one way to get you from point A to point B, while OO (inheritance) is the one feature that expresses coupling the most. In modern C++, when there is a way to do something without involving inheritance (read "tight coupling"), it is usually appreciated. One such way to implement the subject in observer pattern is:
-
class subject
-
{
-
public:
-
void register_observer(
-
const boost::function<void ()>& observer)
-
{
-
observers_.connect(observer);
-
}
-
-
void notify_observers() const
-
{
-
observers_();
-
}
-
-
private:
-
boost::signal<void ()> observers_;
-
};
With help from Boost, nothing from subject, (or the Listener interface used in Wheeler's implementation) except the signature of expected observers, are exposed to Observers (actually, the signature is exposed mostly to programmers). They are decoupled from each other. Next, let's see what types of observers can be utilized with this subject implementation:
-
class foo_observer
-
{
-
public:
-
void notify()
-
{
-
cout <<__PRETTY_FUNCTION__ <<endl;
-
}
-
};
-
-
class bar_observer
-
{
-
public:
-
bar_observer(unsigned int id)
-
: id_(id)
-
{}
-
-
void operator()()
-
{
-
cout <<__PRETTY_FUNCTION__ <<'[' <<id_ <<']' <<endl;
-
}
-
private:
-
unsigned int id_;
-
};
-
-
void free_observer()
-
{
-
cout <<__PRETTY_FUNCTION__ <<endl;
-
}
And how are they put together:
-
int main()
-
{
-
subject sub;
-
foo_observer foo;
-
bar_observer bar1(1);
-
-
sub.register_observer(
-
boost::bind(&foo_observer::notify, &foo));
-
sub.register_observer(bar1);
-
sub.register_observer(bar_observer(2));
-
sub.register_observer(&free_observer);
-
-
sub.notify_observers();
-
-
return 0;
-
}
This subject implementation can be used with general objects (e.g. foo_observer), functional objects (e.g. bar_observer), and of course free functions (e.g. free_observer) and more.
Don't be mistaken, though his C++ observer implementation is not among the best I've seen, Wheeler's post is nevertheless a good read. Enjoy.
Full source code: observer.cpp
![]() |
|
| Previous Post « My First JavaScript – Konami Code « |
Next Post » It’s Better Without Asus » |








This just came across on a pingback — naturally if I were working with Qt, as I usually do, I’d use a signal and slot just as used here. I think a lot of folks got sidetracked by that detail in the post; the point wasn’t so much “How to implement an observer pattern”, but “look how different the C and C++ implementations are” and “the C++ and Java implementations are closer to each other than to the C implementation”. There are naturally better ways to implement the observer pattern in Java as well.
Comment by Scott Wheeler — May 24, 2009 @ 21:05