Download Software Components and Programming by Contract - CMSC 433, Fall 2001 by Alan Sussman - Pr and more Study notes Programming Languages in PDF only on Docsity!
CMSC 433, Alan Sussman, U. Maryland (via
CMSC433, Fall 2001
Software Components
and
Programming by Contract
Alan Sussman November 20, 2001
CMCS 433, Fall 2001 - Alan Sussman 2
Administrivia
- Exams returned next Tuesday
- answers posted
- questions next Tuesday, after return
- Project 3
- Public and private drivers posted
- questions to TA
- Project 5
- Component readings posted on Readings page
CMCS 433, Fall 2001 - Alan Sussman 3
“Reusable code”
- Say I develop a String class in C++
- It is used in 5 different applications
- Each of which is statically linked
- a self-contained executable
CMCS 433, Fall 2001 - Alan Sussman 4
Problems with static linking
- Redundant executable code
- Can’t update defective class without replacing application
- Can’t add extensions of String class
CMCS 433, Fall 2001 - Alan Sussman 5
Dynamic linking
- Only one copy of DLL on disk
- Can upgrade/replace library
- all applications that use class get upgrade
CMCS 433, Fall 2001 - Alan Sussman 6
Dynamic linking problems
- All applications must use same link format and object layout - member alignment, virtual method table, virtual base classes, RTTI
- Class frozen in C++
- adding variables or methods breaks everything
- Upgrade “breaks” your software
- upgrade changed things it depended on
CMSC 433, Alan Sussman, U. Maryland (via
CMCS 433, Fall 2001 - Alan Sussman 7
Versioning
- Every time DLL changes, give it a new version ID
- Application only uses DLL version ID it was compiled against
- Problems:
- multiple version bloat
- inability to upgrade/enhance
CMCS 433, Fall 2001 - Alan Sussman 8
Compatible DLL’s
- Binary compatibility
- old code is able to invoke methods in new library
- Semantic compatibility
- code that expected the old functionality won’t be broken by the new functionality
CMCS 433, Fall 2001 - Alan Sussman 9
Binary compatibility
- Need to agree on link and runtime standards
- Can’t make changes that would violate compatibility - e.g., add/delete variables/methods in C++
CMCS 433, Fall 2001 - Alan Sussman 10
Semantic compatibility
- Restrictions on changes in pre and post conditions
- Which can be “upgraded” to the other?
- int search1(int[] a, int x)
- pre: exists i s.t. a[i] = x
- int search2(int[] a, int x)
- pre: a sorted and exists i s.t. a[i] = x
CMCS 433, Fall 2001 - Alan Sussman 11
Which upgrades are compatible?
- Which can be “upgraded” to the other:
- int search1(int[] a, int x)
- pre: a sorted and exists i s.t. a[i] = x
- post: a[retVal] = x
- int search2(int[] a, int x)
- pre: a sorted and exists i s.t. a[i] = x
- post: a[retVal] = x and (retVal = 0 or a[retVal-1] != x)
CMCS 433, Fall 2001 - Alan Sussman 12
Rules for semantic compatibility
- f has pre: P and post: Q
- f’has pre: P’and post: Q’
- f can be upgraded to f’iff
- P => P’
- makes precondition looser
- Q’=> Q
- makes postcondition stricter
CMSC 433, Alan Sussman, U. Maryland (via
CMCS 433, Fall 2001 - Alan Sussman 19
Directory Service w/Callback
Module Directory Type Notifier = Procedure (name: String); Procedure AddNotifier(n : Notifier); // pre: n != null // post: n is registered, will be called on // AddEntry and RemoveEntry Procedure AddEntry(n : String, f: File); // pre: n != “” && f != null // post: ThisFile(n) == f Function ThisFile(n : name) : File // pre: n != “” // post: result = file named n // or result = null and no such file
Module DirectoryDisplay Procedure myNotifier(n : String) { if Directory.ThisFile(n) == null // delete n in display else // n has been added, display it } .. Directory.AddNotifier(myNotifier); ...
CMCS 433, Fall 2001 - Alan Sussman 20
What is missing?
- Precondition for notifier
- that state of directory reflects change being notified about
- or that it doesn’t
- Postcondition for notifier
- that it doesn’t change the state of the directory
CMCS 433, Fall 2001 - Alan Sussman 21
When is the notifier called?
- Before or after change?
- When file is renamed, how many times?
- When file is replaced, how many times?
CMCS 433, Fall 2001 - Alan Sussman 22
Why is changing state bad?
- Say we attach a notifier that, whenever an entry with the name X.htm is added, renames it to X.html - violates postcondition of AddEntry
CMCS 433, Fall 2001 - Alan Sussman 23
Locking doesn’t help
- Could make Add/RemoveEntry lock directory - wouldn’t help - notifier, and subsequent potential modification, are all done in same thread
- If locks weren’t recursive, would just result in deadlock
CMCS 433, Fall 2001 - Alan Sussman 24
Thread spawning?
- One way to handle renaming notifier
- Use locks
- When notified that X.htm has been added, spawn thread to do renaming - will have to wait to obtain lock
- What is the pre/post condition of a method that spawns a thread?
CMSC 433, Alan Sussman, U. Maryland (via
CMCS 433, Fall 2001 - Alan Sussman 25
inProgress functions
- Add method that detects whether an operation is in progress
- Add precondition for operation that no other operation is in progress
CMCS 433, Fall 2001 - Alan Sussman 26
Substitutability
- Clients use an interface to invoke a method in a provider - pre: established by client => demanded by interface => required by provider - post: established by provider => demanded by interface => required by client
CMCS 433, Fall 2001 - Alan Sussman 27
subtyping of preconditions
interface^ interfacestablished by client
required by provider
CMCS 433, Fall 2001 - Alan Sussman 28
subtyping of postconditions
interface established by provider^ interfac
required by client
CMCS 433, Fall 2001 - Alan Sussman 29
Co- and Contra- variance
- Co-variance = vary in same way
- Contra-variance = vary in opposite ways
- Co-variant return types are sound and useful
- Supported in C++ but not in Java
- Contra-variant arguments are sound but rarely useful - Not supported in C++ or Java
CMCS 433, Fall 2001 - Alan Sussman 30
Co and Contra variance
interface View { Model getModel(); } interface TextView extends View { TextModel getModel(); } interface GraphicsView extends TextView { GraphicsModel getModel(); }
CMSC 433, Alan Sussman, U. Maryland (via
CMCS 433, Fall 2001 - Alan Sussman 37
Solutions
- Use private variables and setter/getter methods
- Use protected members when appropriate
- Figure out groups of mutually dependent methods - if A invokes B and B invokes A, then A and B are mutually dependent - have to override an entire set of MD methods
CMCS 433, Fall 2001 - Alan Sussman 38
Multiple inheritance
- multiple supertyping
- inherit interface from multiple supertypes
- promise substitutability for all of them
- inheritance from multiple supertypes is tricky
CMCS 433, Fall 2001 - Alan Sussman 39
Multiple inheritance of
implementations
- What if a function is defined in both B1 and B2? - what version does C invoke?
- What if a function is defined in both A and B1? - what version does B2 invoke?
- Do B1 and B2 share an A or each have their own A?
A
B1 B
C
CMCS 433, Fall 2001 - Alan Sussman 40
More issues?
- Does it matter which is declared first, B1 or B2?
- In C++, B1 and B2 share an A if both declare A as a virtual base class - Don’t know that B1 and B2 might need to share an A until class C is written - virtual base classes are less efficient
CMCS 433, Fall 2001 - Alan Sussman 41
Virtual base classes
A B A B C
A
B C
B
B
A as a non-virtual base class A as a virtual base class
A B
A B object
A C object A
B2B
A B object
A C object