Presentation is loading. Please wait.

Presentation is loading. Please wait.

Neelam Soundarajan 1 Johan Dovland 2 Jason Hallstrom 3 Tracing Correct Usage of Design Patterns 2 Computer Sc. Dept. Clemson University 1 Computer Sc.

Similar presentations


Presentation on theme: "Neelam Soundarajan 1 Johan Dovland 2 Jason Hallstrom 3 Tracing Correct Usage of Design Patterns 2 Computer Sc. Dept. Clemson University 1 Computer Sc."— Presentation transcript:

1 Neelam Soundarajan 1 Johan Dovland 2 Jason Hallstrom 3 Tracing Correct Usage of Design Patterns 2 Computer Sc. Dept. Clemson University 1 Computer Sc. & Eng. Ohio State University 3 Computer Sc. Dept. Clemson University 2 Dept. of Informatics University of Oslo

2 Tracing Correct Usage of Design Patterns, SEA '072 Background 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.

3 Tracing Correct Usage of Design Patterns, SEA '073 Background (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.

4 Tracing Correct Usage of Design Patterns, SEA '074 Background (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;

5 Tracing Correct Usage of Design Patterns, SEA '075 Example: Observer Pattern Common Problem: Need to keep the states of a set of objects consistent with that of another object.

6 Subject Attach(in Obs) Notify() Detach(in Obs) observers *1 ConcreteSubject -subjectState 1 subject * Observer Update() ConcreteObserver -observerState Update() For all o in observers o.Update() 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. Solution: Observer Pattern: Subject Attach(in Obs) Notify() Detach(in Obs)

7 Tracing Correct Usage of Design Patterns, SEA '077 System 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 nurse s; That will update the information in the doctor and nurse objects.

8 Tracing Correct Usage of Design Patterns, SEA '078 Hospital 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

9 Tracing Correct Usage of Design Patterns, SEA '079 Observer Contract pattern contract Observer concepts: Consistent( Subject, Observer ) Modified( Subject, Subject ) invariant: For-all ob In players[1:] Consistent( players[0], 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.

10 Tracing Correct Usage of Design Patterns, SEA '0710 Observer Contract (contd.) role contract Subject Set obs; void Notify( ) : pre: true post: ~Modified(this@pre, this) & (obs@pre = obs) &(|σ|=|obs|) // σ is trace of method calls &(forAll ob IN obs: |σ.ob.Update|=1) Informally: subject state remains unModified; set of attached observers remains unchanged; Update() is called on each attached observer.

11 Tracing Correct Usage of Design Patterns, SEA '0711 Observer Contract (contd.) role contract Observer Subject sub; void Update( ) : pre:true post:(sub = sub@pre) &Consistent(subSt, this) others: post: (sub = sub@pre) & Consistent(subSt@pre, this) Informally: Update() makes the observer state Consistent() with the subject state; other methods leave observer state Consistent() with subject state.

12 Tracing Correct Usage of Design Patterns, SEA '0712 But... 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@pre, this) in post-cond. of Notify() is not okay; even (obs@pre = obs) is not okay! But can’t allow Subject.Notify() to make arbitrary changes!

13 Tracing Correct Usage of Design Patterns, SEA '0713 Key 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.

14 Tracing Correct Usage of Design Patterns, SEA '0714 Solution 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.

15 Tracing Correct Usage of Design Patterns, SEA '0715 Result... 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?


Download ppt "Neelam Soundarajan 1 Johan Dovland 2 Jason Hallstrom 3 Tracing Correct Usage of Design Patterns 2 Computer Sc. Dept. Clemson University 1 Computer Sc."

Similar presentations


Ads by Google