Title: Neelam Soundarajan1
1Tracing Correct Usage of Design Patterns
- Neelam Soundarajan1
- Johan Dovland2
- Jason Hallstrom3
3 Computer Sc. Dept. Clemson University
2 Dept. of Informatics University of Oslo
1 Computer Sc. Eng. Ohio State University
2 Computer Sc. Dept. Clemson University
2Background
- Design Patterns Time-tested solutions to
recurring problems - Patterns Have fundamentally altered how s/w is
designed - Allow designers to exploit the collective
wisdom of the s/w community.
3Background (contd.)
- Patterns Are usually described informally
- Informal Descriptions ...
- Can be ambiguous
- Subject to misinterpretations
- interpreted in subtly different ways by different
members of a large software team.
4Background (contd.)
- In Previous Work
- Proposed formal specs of patterns and their
applications in individual systems - Pattern Contracts Apply to all systems that use
the pattern - Pattern Subcontracts Specialize the contract in
a manner appropriate to a particular system
5Example Observer Pattern
- Common Problem Need to keep the states of a set
of objects consistent with that of another
object.
6Solution Observer Pattern
Idea When Subject state changes, call
Notify(). Notify() calls Update() on each
Observer. Each Observer has to update its state
to make it consistent with the new Subject
state.
7System Using Observer
- Simulation of a Hospital Patient class plays
Subject role - Nurse class plays Observer role
- Doctor class plays Observer role
- When state of patient changes, call Update() on
attached doctor and nurses - That will update the information in the doctor
and nurse objects.
8Hospital System (contd.)
- But what does state of patient changes mean?
- Change in every bit/byte? ...
- And what exactly should happen when the
information in the doctor and nurse objects are
updated? - Answer Provided by Observer contract
Hospital/Observer subcontract
9Observer Contract
- pattern contract Observer
- concepts
- Consistent( Subject, Observer )
- Modified( Subject, Subject )
- invariant
- For-all ob In players1
- Consistent( players0, ob )
- Informally The states of all the attached
observers are consistent with the subject state. - Consistent() and Modified() are auxiliary
concepts will be defined in the subcontract.
10Observer Contract (contd.)
- role contract Subject
- SetltObservergt obs
- void Notify( )
- pre true
- post
- Modified(this_at_pre, this) (obs_at_pre obs)
- (sobs) // s is trace of method calls
- (forAll ob IN obs s.ob.Update1)
- Informally subject state remains unModified
set of attached observers remains unchanged
Update() is called on each attached observer.
11Observer Contract (contd.)
- role contract Observer
- Subject sub
- void Update( )
- pre true
- post (sub sub_at_pre)
- Consistent(subSt, this)
- others
- post (sub sub_at_pre)
- Consistent(subSt_at_pre, this)
- Informally Update() makes the observer state
Consistent() with the subject state - other methods leave observer state Consistent()
with subject state.
12But ... Problem
- What if Update() calls some other method that
modifies the subject state? - E.g. Doctor.Update() invokes
Patient.AdminCPR() because of current patient
state - And this is part of the Subject.Notify() cycle!
- So Modified(this_at_pre, this) in post-cond. of
Notify() is not okay even (obs_at_pre obs) is
not okay! - But cant allow Subject.Notify() to make
arbitrary changes!
13Key Observations ...
- A complex system is a system!
- Not a collection of independent objects!
- Need to consider the behavior of appropriate
groups of objects ... ... the pattern
instances! - Need to consider the interaction behavior of
groups of objects involved in various pattern
instances.
14Solution
- Pi Traces Each pattern instance PI has a trace,
pi pi records the activitities of all the
objects in PI and only these objects. - In Hospital system Each pattern instance
consists of a patient object, the assigned doctor
object, and the assigned nurse objects. - The corresponding pi will capture all
interactions that can affect the state of the
patient object.
15Result ...
- The pattern contract, written using pi traces,
allow us to express realistic situations such as
the state of subject changing during the notify()
cycle ... while preserving intent of the
pattern. - Details In the paper.
- Thanks!
- Questions?