Home > Articles > Software Development & Management > UML

Publish and Subscribe Using C++ and the Observer Pattern

  • Print
  • + Share This
Separation of concerns is increasingly on the programmer's radar. Given the growing range of data access products and platforms, it is now essential to separate data producers from data consumers. The observer pattern provides a simple but powerful model for achieving this crucial design goal.
Like this article? We recommend

The late 1990s saw the widespread commercial introduction of the client-server paradigm. The demands of this technology gave rise to a growing need for separating software functions. This trend was followed by multi-tier architectures with applications being divided into layers such as presentation, business logic, business rules, and database. As clients become thinner and networks speed up, the division and demarcation between software elements will continue to be a hot topic.

A key element of this separation of concerns is the need to share data from one source with many users.

Data is created and published by one entity for use by a number of subscribers or observers. Stock prices are a classic example of one publisher and multiple consumers. A design pattern called the observer pattern was created [1] to accommodate this type of scenario and a C++ realization of it is the topic of this article. Along the way, you'll see some introductory comments on the increasingly important field of software architecture.

I'm always struck by the elegant simplicity of the 23 classic design patterns. Learning and applying these patterns is an excellent practice for programmers, designers, and architects! They provide a common vocabulary that transcends the domain-specific technology and also provide access to what is a key element of our software heritage. Using patterns allows us to stand on the shoulders of giants as we try to engineer the software solutions of today and tomorrow.

The Takeaway

You'll see the ease with which a relatively complex data-sharing problem can be solved using the observer pattern. This is one of the so-called behavioral design patterns [1]. I use Visual C++ as the platform and follow the pattern implementation code as faithfully as possible.

  • + Share This
  • 🔖 Save To Your Account