We need a place to store points when they are generated by mouse clicks. Because ATL Scribble is a simple SDI application, we can create a new class that has a single global instance to contain the points and to handle persistence. Our CDataModel class handles these responsibilities. CDataModel keeps an STL vector of strokes that represents the sketch. Points are added to the data model using the AddPoint method of the global _DataModel instance. The data model (subject) also keeps a list of views (observers), which are notified when data changes in the model. You might not need a subject or an observer implementation in a small sample with a single view, but some kind of update mechanism is necessary if multiple views or splitters are allowed in your application. We'll use the update mechanism here to demonstrate. The data model class derives from a CSubjectImpl class that maintains and updates a list of observers. CSubjectImpl derives from the abstract base class CSubject, which defines the following three methods:
virtual void AddObserver(CObserver* pObserver) = 0; virtual void RemoveObserver(CObserver* pObserver) = 0; virtual void UpdateAllObservers(CObserver* pSender, DWORD dwType) = 0;
CSubjectImpl implements these methods on an STL vector. CDataModel contains the application-specific state, which is a vector of strokes. The view adds points when the mouse is dragged and then generates an update by calling UpdateAllObservers on the data model. The view subscribes to the data model update list when it is created and removes itself when destroyed. Figure 16-4 shows the class relationships for the data model.
Figure 16-4. Class relationships for the data model.