Implementing the Observer pattern
To implement the Observer pattern, we will first need to define our Subject
and Observer
classes. We will then need to derive concrete classes from these classes to incorporate our application specifics and to put our pattern in motion. Let’s get started!
Creating an Observer, Subject, and domain-specific derived classes
In our example, we will create Subject
and Observer
classes to establish the framework for registering an Observer
with a Subject
and for the Subject
to Notify()
its set of observers of a state change it may have. We will then derive from these base classes descendent classes we are accustomed to seeing – Course
and Student
, where Course
will be our concrete Subject
and Student
will become our concrete Observer
.
The application we will model will involve a course registration system and the concept of a waitlist. As we have seen before in Question 2 of Chapter 10, Implementing Association, Aggregation, and Composition...