Observer - Alternate Profile

Description

This is an alternate profile for the Observer pattern where the Observer class hierarchy is collapsed into a single Observer object. This profile can be used when there is no need for an abstract Observer class. The difference in the profile compared to the first one is in the stereotype definition, OCL constraints, and multiplicities of stereotypes connected to the Observer class.

Profile

UML profile of design pattern

Profile download (Rational Rose model)

OCL Constraints

context Observer
inv: not self.isAbstract

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 one concrete observer class.
  2. There is a hierarchy of subject classes with an abstract base class and at least one sub-class.
  3. The abstract subject has a unidirectional association with the observer.
  4. The abstract subject defines operations for attaching, detaching, and notifying observers.
  5. The observer class has one unidirectional association with a concrete subject, one or more observer states, and defines at least one operation for updating itself when it is notified by the subject.
  6. A concrete subject can have more than one association with concrete observers and has one or more subject states.
  7. 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 diagram in this case is the correct instance of the original Observer profile with a class hierarchy for the Observer class. It is essentially the same class diagram with "2" appended to all class names to avoid namespace conflicts with the correct instance of this alternate profile. The tool will report errors at the association multiplicity (graphical constraint) level because of the changes to them for this alternate profile. MyObserver2 also violates the first OCL constraint for Observer but the tool does not check it until the graphical constraints are fixed, and in this case the user should realize that the profile does not have a class hierarchy for Observer.

Tool Output

output of tool