Observer

Description

Allows multiple objects to be notified and updated when another object's state changes. An alternate Observer profile is also available.
More info

Profile

UML profile of design pattern

Profile download (Rational Rose model)

OCL Constraints

context Observer
inv: self.treeTop implies (self.isAbstract and self.sOO->size() = 1
and self.oSO->size() = 0 and self.observerState->size() = 0)

inv: self.isAbstract implies (self.specialization->size() > 0 and
self.specialization->forAll (c | c.child.oclIsKindOf (Observer)))

inv: not self.isAbstract implies (self.oSO->size() = 1 and
self.sOO->size() = 0 and self.observerState->size() > 0)

context ObservedSubject
inv: self.treeTop implies (self.isAbstract and self.sOS->size() = 1 and
self.oSS->size() = 0 and self.attachOp->size() > 0 and
self.detachOp->size() > 0 and self.notifyOp->size() > 0 and
self.subjectState->size() = 0)

inv: not self.treeTop implies (self.getStateOp->size() > 0 and
self.setStateOp->size() > 0)

inv: self.isAbstract implies (self.specialization->size() > 0 and
self.specialization->forAll (c | c.child.oclIsKindOf (ObservedSubject)))

inv: not self.isAbstract implies (self.oSS->size() > 0 and
self.sOS->size() = 0 and self.subjectState->size() > 0)

context aSubjectObserver
inv: self.sOO.isNavigable and not self.sOS.isNavigable

context aObserverSubject
inv: self.oSS.isNavigable and not self.oSO.isNavigable
	

Explanation of Profile

  1. There is a hierarchy of observer classes with an abstract base class and at least one sub-class.
  2. There is a hierarchy of subject classes with an abstract base class and at least one sub-class.
  3. The root subject has a unidirectional association with the abstract observer.
  4. The root subject defines operations for attaching, detaching, and notifying observers.
  5. The abstract observer defines at least one operation for updating itself when it is notified by the subject.
  6. A concrete observer has one unidirectional association with a concrete subject and has one or more observer states.
  7. A concrete subject can have more than one association with concrete observers and has one or more subject states.
  8. A concrete subject has one or more operations to retrieve and set its current state.

Correct Instance

correct design pattern instance

Incorrect Instance

incorrect design pattern instance

The incorrect instance is the correct instance diagram for the alternate Observer profile which does not have a class hierarchy for Observer. Each class has a 'B' appended to the names to avoid namespace conflicts. Since the tool terminates on graphical constraint errors without checking OCL constraints, the tool will report that the tag value treeTop for DigitalClockB is missing because in the alternate profile, the tag definition is not necessary and hence not present. Therefore, we provide an arbitrary tag value ("false" in this case) so we can see which OCL constraints are violated by this instance class diagram.

Tool Output

output of tool