Theory of the Observer Pattern
In this lesson, we will explain the theory of the observer pattern.
Managing dependencies between entities in a software system is crucial to a solid design. In the previous chapter, we had a look at the open-closed principle. Now we’ll turn to another principle for dependency management and illustrate how both of these principles may be realized in C using the Observer pattern.
Dependencies arise
Returning to the examples used for the State pattern, we described techniques for implementing the behavior of a simple digital stop-watch:
- Implementing such a watch typically involves the task of fetching the time from some kind of time source.
- As the time source probably will be tied to interactions with the operating system, it is good to encapsulate it in an abstraction hiding the system-specific parts to ease unit testing and portability.
- Similarly, we should encapsulate the internals of the digital stop-watch in a module of its own.
- As the digital stop-watch is responsible for fetching the time from the time-source (to present it on its digital