views:

504

answers:

5

Is the observer design pattern already defined in STL (Like the java.util.Observer and java.util.Observable in Java) ?

+2  A: 

No it doesn't. The C++ STL is much smaller than Java's Standard Library. If you are looking for something to expand on the STL that is supported by almost everything, it would be worth taking a look at the Boost libraries. In this case you may want to look at Boost.Signals which provides a signal/slot model.

Yacoby
+5  A: 

No, but Boost.Signals gives you something similar.

David Seiler
+1, Boost.Signals is great. Also check Signals2 if you have multiple threads.
Manuel
+4  A: 

Here is a reference implementation (scroll to bottom of page).

http://en.wikipedia.org/wiki/Observer_pattern

Hogan
Hard to take seriously a C++ implementation that uses illegal member function names such as _Notify.
anon
It's still valid C++ though.
Nick Bedford
Apparently it has other problems as well. Check the discussion page:http://en.wikipedia.org/wiki/Talk:Observer_pattern#Implementation_flaw_in_C.2B.2B
Manuel
hmmm... maybe my answer should have been "Modify the code located here to a reference implementation" :(
Hogan
+1  A: 

the observer design pattern is not defined in the stl. you can refer to the gang of four design patterns book or a google search should provide enough details to implement it. if this answer isnt answered soon ill write a quick POC in code blocks.

Athens
Or you can copy-paste the Wikipedia implementation that somebody has linked in another answer
Manuel
thanks! Boost signals look interesting. I have used Boost for random number generation but never for an observer pattern or signals as they call it. Anyhow, I think evaluating the GOF observer pattern and Boost Signals is a good idea at least for academic purposes.
Athens
+2  A: 

As far as my knowledge goes in C++, STL doesn't have an implementation for Observer pattern. There was a proposal for Signal/Slot for standard library in TR2 though.

There are plenty of libraries which provides implementation for Observer pattern Qt library being one of the pioneers. The boost library has an implementation (see Boost::Signals & Boost::Signals2).

The Poco C++ library has a neat implementation of the observer pattern (see NotificationCenter).

libsigc++, cpp-events are some of the other libraries that provide signal/slot implementations.

agupta666