Several issues related to the implementation of the dependency mechanism are discussed in this section.
This self-consistency rule is easy to violate unintentionally when Subject subclass operations call inherited operations. For example, the notification in the following code sequence is trigged when the subject is in an inconsistent state:
You can avoid this pitfall by sending notifications from template methods in abstract Subject classes. Define primitive operation for subclasses to override, and make Notify the last operation in the template method, which will ensure that the object is self-consistent when subclasses override Subject operations.void MySubject::Operation (int newValue) { BaseClassSubject::Operation(newValue); // trigger notification _myInstVar += newValue; // update subclass state (too late!) }
By the way, it's always a good idea to document which Subject operations trigger notifications.void Text::Cut (TextRange r) { ReplaceRange(r); // redefined in subclasses Notify(); }