November 5, 2025Nov 5 Over the last two posts, we explored different implementations of the observer pattern in C++. We began with a very simple example, then evolved toward a more flexible, template- and inheritance-based design. This week, let’s move further — shifting away from inheritance and embracing composition. Discovering Observers - Part 3 by Sandor Dargo From the article: You might say (and you’d be right) that publishers and subscribers can just as well be used as members instead of base classes. In fact, with our previous implementation, handling changes inside subscribers felt more complicated than necessary. What we’d really like is this: subscribers that observe changes and trigger updates in their enclosing class in a coherent way. As one of the readers of Part 1 pointed out, this can be elegantly achieved if subscribers take callables. Step 1: Subscribers with callables We start by letting Subscriber accept a std::function, which gets called whenever the subscriber is updated: View the full article
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.