2

Professionals out there,

this is the first time that I'm actually posting a question to this board that helped me out a lot in the past. I'm still a programming novice and have been trying to learn a lot about programming & software development in the last few months.

I recently tackled the topic "c++11-Smart Pointers" and decided rewrite all the Design Patterns I learned a few weeks earlier, including the"Observer Pattern" using shared_ptr & weak_ptr for resource management.

When I was trying to write a simple Implementation of the observer pattern (like in Head First's Design Pattern or Design Patterns by E.Gamma), using an interface-class for both the observable subjects and the observers, and exactly one concrete class for each, I received various error messages from Visual Studio. I just don't understand how to combine shared_ptr and weak_ptr in this pattern, which might be due to the fact that I still don't fully understand the the combination of both.

Unfortunatelly, even a thorough search on the Internet didn't provide me with a simple example of an "observer-pattern using smart-pointer" that can serve as a orientation for me.

I therefore was wondering, if one of you guys would know where to find such a simple implmentation or be so kind to provide me with one of your own. Both would be really really helpful :-)

Thanks a lot

coderAndi

CoderAndi
  • 37
  • 1
  • 2
  • 2
    Maybe we can help you with your approcah if you show us your attempt to implement the pattern and the errors you came across. In short: what have you tried so far? – Arne Mertz Jul 15 '13 at 07:51
  • 2
    @ArneMertz He's going at it backwards: choosing the solution (smart pointers) before defining the problem. The observer pattern, in fact, is a prime example of something that is designed to be used when smart pointers aren't appropriate. – James Kanze Jul 15 '13 at 07:58
  • You could see the Observer->Observable connection as a resource and use smart pointers to manage that resource. But I think that's not what he intended ;-) – Arne Mertz Jul 15 '13 at 08:02
  • 1
    @ArneMertz If you see the Observer->Observable relationship as a resource, you haven't understood the Observer pattern. – James Kanze Jul 15 '13 at 08:15
  • @JamesKanze I didn't say I do. I said one *could*. That is, if one relly *wants* to apply smart pointers to implement Observer, there are surely some ways to abstract a resource view from it. – Arne Mertz Jul 15 '13 at 08:30
  • @ArneMertz I suppose you can contort any pattern to be an instance of any other, but the whole point of the observer pattern is that the observer and the observable are otherwise unrelated objects. – James Kanze Jul 15 '13 at 08:44

1 Answers1

7

The observer pattern is a typical example of a case where smart pointers (at least the usual candidates) are inappropriate. Neither the subject nor the object "own" one another; each has its own lifespan. In fact, one frequent use of the observer pattern is when an object has a pointer to another object which it doesn't own. It registers as an observer, so as to be informed when the observed object is destructed.

It sounds to me like you're attacking the entire thing backwards. You have a solution (smart pointers), and you're trying to make every problem fit it. That never works.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • Thanks for your immediate reply:-) I already feared the reason why I couldn't find any matching example was that you normally just don't use shared_ptr & weak_ptr with the observer pattern. I actually really tried to do use smart pointers everywhere I used raw pointers before, because the books I've read so far gave me the impression it would be ok, as long as I follow a few rules... – CoderAndi Jul 15 '13 at 08:06
  • 1
    @CoderAndi It's not. Smart pointers are a useful tool, but they aren't universally applicable. They solve a particular problem in particular cases, but in most applications, most pointers will be purely for navigation, and should be raw pointers. – James Kanze Jul 15 '13 at 08:15
  • @CoderAndi trying to implement a pattern is not a very useful exercise. you can really only learn a pattern when you encounter problem in real code that can be simplified by using a pattern, not the other way around. For a real example of observer pattern, see `boost::signal`, and you won't find any shared_ptr there. – yngccc Jul 15 '13 at 08:15
  • @yngum That's a good point: the reason they are "patterns", and not class templates or such, is because they tend to vary slightly from one use to the next. And until you have the actual use, you can't implement them (or choose among existing implementations, like `boost::signal`). – James Kanze Jul 15 '13 at 08:17
  • @James Kanze/@yngum Thanks again, both of you. Well, thats a real pain, because I spent a lot time on books that taught me patterns without any example^^. I understand that you cannot fully grasp a pattern if you haven't encountered it in real code a few times, but you can still learn what the pattern is basically about. So,..you guys say observer is a pattern that is best not implemented using smart pointers, right? That sucks as those damn books actually claimed, its totally OK to use smart pointers pretty much anywhere you want and thus don't have to manually delete resources anymore... – CoderAndi Jul 15 '13 at 08:28
  • @CoderAndi *if* you find that you need to manually delete resources in an observer pattern implementation, then it should be pretty obvious where you can use smart pointers. But I cannot see how this could naturally be the case. I would expect any resource management to happen one level up from observers and subjects. – juanchopanza Jul 15 '13 at 08:35
  • @CoderAndi You should throw out any book which says that. You cannot simply replace every raw pointer with a smart pointer; smart pointers have very particular semantics, for very particular uses, and in many, if not most applications, most pointers will still be raw pointers. – James Kanze Jul 15 '13 at 08:43
  • @James Kanze: Perhaps throwing out these books is the best advice ;-). But again, even if implementing the observer pattern with smart pointers is pointless for many reasons, is there really no way of doing it? – CoderAndi Jul 15 '13 at 09:02