Neelam Soundarajan 1 Johan Dovland 2 Jason Hallstrom 3 Tracing Correct Usage of Design Patterns 2...

15
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

Transcript of Neelam Soundarajan 1 Johan Dovland 2 Jason Hallstrom 3 Tracing Correct Usage of Design Patterns 2...

Page 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.

Neelam Soundarajan1

Johan Dovland2

Jason Hallstrom3

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 InformaticsUniversity of Oslo

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

Tracing Correct Usage of Design Patterns, SEA '07 2

Background

• Design PatternsTime-tested solutions to recurring problems;

• PatternsHave fundamentally altered how s/w is

designed;

Allow designers to exploit the collective wisdom of the s/w community.

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

Tracing Correct Usage of Design Patterns, SEA '07 3

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.

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

Tracing Correct Usage of Design Patterns, SEA '07 4

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;

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

Tracing Correct Usage of Design Patterns, SEA '07 5

Example: Observer Pattern

• Common Problem:Need to keep the states of a set of

objects consistent with that of another object.

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

Subject

Attach(in Obs)

Notify()

Detach(in Obs)

observers*1

ConcreteSubject

-subjectState1

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)

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

Tracing Correct Usage of Design Patterns, SEA '07 7

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 nurses;

• That will update the information in the doctor and nurse objects.

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

Tracing Correct Usage of Design Patterns, SEA '07 8

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

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

Tracing Correct Usage of Design Patterns, SEA '07 9

Observer Contract

pattern contract Observerconcepts:

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.

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

Tracing Correct Usage of Design Patterns, SEA '07 10

Observer Contract (contd.) role contract Subject

Set<Observer> obs;void Notify( ) :

pre: truepost:

~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.

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

Tracing Correct Usage of Design Patterns, SEA '07 11

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.

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

Tracing Correct Usage of Design Patterns, SEA '07 12

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!

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

Tracing Correct Usage of Design Patterns, SEA '07 13

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.

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

Tracing Correct Usage of Design Patterns, SEA '07 14

Solution

• Pi Traces:Each pattern instance PI has a trace, pi;pi records the activitities of all the objects in PIand 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.

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

Tracing Correct Usage of Design Patterns, SEA '07 15

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?