Title: UML Distilled
1UML Distilled
2Design by contract
- Object-Oriented Software Construction by Bertrand
Meyer, Prentice Hall - The presence of a precondition or postcondition
in a routine is viewed as a contract.
3Rights and obligations
- Parties in the contract class and clients
- require pre, ensure post with method r If you
promise to call r with pre satisfied then I, in
return, promise to deliver a final state in which
post is satisfied. - Contract entails benefits and obligations for
both parties
4Rights and obligations
- Precondition binds clients
- Postcondition binds class
5Example
6If precondition is not satisfied
- If clients part of the contract is not
fulfilled, class can do what it pleases return
any value, loop indefinitely, terminate in some
wild way. - Advantage of convention simplifies significantly
the programming style.
7Source of complexity
- Does data passed to a method satisfy requirement
for correct processing? - Problem no checking at all or multiple
checking. - Multiple checking conceptual pollution
redundancy complicates maintenance - Recommended approach use preconditions
8Class invariants and class correctness
- Preconditions and postconditions describe
properties of individual methods - Need for global properties of instances which
must be preserved by all routines - 0ltnb_elements nb_elementsltmax_size
- empty(nb_elements0)
9Class invariants and class correctness
- A class invariant is an assertion appearing in
the invariant clause of the class. - Must be satisfied by all instances of the class
at all stable times (instance in stable state) - on instance creation
- before and after every remote call to a routine
(may be violated during call)
10Class invariants and class correctness
- A class invariant only applies to public methods
private methods are not required to maintain the
invariant.
11Invariant Rule
- An assertion I is a correct class invariant for a
class C iff the following two conditions hold - The constructor of C, when applied to arguments
satisfying the constructors precondition in a
state where the attributes have their default
values, yields a state satisfying I. - Every public method of the class, when applied to
arguments and a state satisfying both I and the
methods precondition, yields a state satisfying
I.
12Invariant Rule
- Precondition of a method may involve the initial
state and the arguments - Postcondition of a method may only involve the
final state, the initial state (through old) and
in the case of a function, the returned value. - The class invariant may only involve the state
13Invariant Rule
- The class invariant is implicitly added (anded)
to both the precondition and postcondition of
every exported routine - Could do, in principle, without class invariants.
But they give valuable information. - Class invariant acts as control on evolution of
class - A class invariant applies to all contracts
between a method of the class and a client
14Resource Allocation
reqs
ltJobCategorygt
ltFacilitygt
0..
type
provides
0..
ltJobgt when TimeInterval
allocated
schedule
ltResourcegt
0..1
0..
inv Joballocatedltgt0 gt allocated.provides-gtincl
udesAll(type.reqs) --Any allocated resource must
have the required facilities inv Resourcejo1,
jo2 Job (schedule-gtincludesAll(jo1,jo2)
gt jo1.when.noOverlap(jo2.when) -- no
double-booking of resources
15Collaborations in UMLUML Distilled page 113
- A collaboration is a name given to the
interaction among two or more classes. - You can also add a class diagram to show the
classes that participate in the collaboration.
16Collaborations
- Use them to show common behavior.
- Often you may find that the same collaboration is
used by different classes in the system. - You can illustrate this by parameterizing the
collaboration.
17Roles
- Show roles that objects play in collaboration.
- UML also uses the term pattern for a
parameterized collaboration.
18Collaboration basics
- collaboration checkDef
- role Cd_graph
- out check() // out provided
- getClasses() // local method
- in getAll // in expected
- from Cd_graph bypassing Neighbors to
Vertex - role Neighbors
- role Vertex
-
- A collaboration is self-contained and
parameterized - by roles and expected members.
19Java Code What is the collaboration?
- Cd_graph
- void isDefined(ClassGraph cg)
- checkDefined(cg, getClasses(cg))
- HashSet getClasses(ClassGraph cg)
- Strategy getAll new Strategy(
- "from Cd_graph bypassing Neighbors
to Vertex") - TraversalGraph tg new TraversalGraph(getAll,
cg) - Visitor v new Visitor()
- HashSet return_val new HashSet()
- void before(Vertex v)
- return_val.add(v.get_vertex_name())
- public Object getReturnValue()return
return_val - tg.traverse(this,v)
- return (HashSet)v.getReturnValue()
-
20Java Code What is the collaboration?
- void checkDefined(ClassGraph cg, HashSet
tempClassSet) - final HashSet classHash tempClassSet
- Strategy checkAll new Strategy(
- from Cd_graph through Neighbors to
Vertex") - TraversalGraph tg new TraversalGraph(checkAll,
cg) - tg.traverse(this, new Visitor()
- void before(Vertex v)
- if(!classHash.contains(v.get_vertex_name()))
- System.out.println("The class "
- v.get_vertex_name() " is
undefined.") - )
-
21Find undefined classes
vertices
getAll
Cd_graph
Vertex
checkAll
neighbors
vertices
Neighbors
getAll from Cd_graph bypassing Neighbors to
Vertex checkAll from Cd_graph through Neighbors
to Vertex
22High-level description
- It is useful to have a high-level description of
the collaboration besides the Java source code.
Useful documentation. - Ultimately we are interested in the executable
form of the collaboration (Java source code).
23Collaboration 1 with strategies
- collaboration checkDef
- participant Cd_graph
- out check()(uses getClasses, checkDefined)
- getClasses()(uses getAll)
- checkDefined()(uses checkAll)
- in getAll
- from Cd_graph bypassing Neighbors to
Vertex - in checkAll
- from Cd_graph through Neighbors to Vertex
- participant Neighbors
- participant Vertex
24Collaboration 2 with accessors
- collaboration checkDef
- participant Cd_graph
- out check()(uses getClasses, checkDefined)
- getClasses()(uses getAll)
- checkDefined()(uses checkAll)
- in getNeighbors()
- in getVertices()
- getAll()return getVertices()
- // from Cd_graph bypassing Neighbors to
Vertex - checkAll()return getNeighbors().getVertices()
- // from Cd_graph through Neighbors to
Vertex - participant Neighbors in getVertices()
- participant Vertex
25Use of 2. collaboration
- Need to provide the expected methods (in methods)
and provide name map (identity in this case) - Cd_graph in getNeighbors()
- Cd_graph in getVertices()
- Neighbors in getVertices()
- Cd_graph getNeighbors()
- return cg.gather(this,from Cd_graph to
Neighbors) - Cd_graph getVertices()
- return cg.gather(this,
- from Cd_graph bypassing Neighbors to
Vertex) - Neighbors getVertices()
- return cg.gather(this,from Neighbors to
Vertex)
26Use of 1. collaboration
- Need to provide the expected methods (in methods)
and provide name map (identity in this case) - Cd_graph
- in getAll // use default
- in checkAll // use default
27The Collaboration in Action
- Computer Science
- Project
- Smaller class graph for class graphs
- Mathematics
28Class dictionary (part 1)
Cd_graph ltadjacenciesgt Nlist(Adjacency)
EOF. Adjacency lt source gt Vertex lt ns gt
Neighbors "." . Neighbors Neighbors_wc
. Neighbors_wc Construct_ns Alternat_ns
common lt construct_ns gt List(Any_vertex). Construc
t_ns "". Alternat_ns "" lt alternat_ns gt
Bar_list(Term) ltcommongt Common. Common
"common". Any_vertex Opt_labeled_term
Optional_term Syntax_vertex .
29Class dictionary (part 2)
Vertex lt vertex_name gt Ident. Syntax_vertex
Regular_syntax common. Regular_syntax lt string
gt String . Opt_labeled_term Labeled Regular
common ltvertexgt Term. Regular . Labeled "lt" lt
label_name gt Ident "gt" . Term Normal common
ltvertexgt Vertex. Normal . Optional_term ""
ltoptgt Sandwich(Opt_labeled_term) "".
30Class dictionary (part 1)
getAll from Cd_graph bypassing Neighbors to
Vertex checkAll from Cd_graph through Neighbors
to Vertex
Cd_graph ltadjacenciesgt Nlist(Adjacency)
EOF. Adjacency lt source gt Vertex lt ns gt
Neighbors "." . Neighbors Neighbors_wc
. Neighbors_wc Construct_ns Alternat_ns
common lt construct_ns gt List(Any_vertex). Construc
t_ns "". Alternat_ns "" lt alternat_ns gt
Bar_list(Term) ltcommongt Common. Common
"common". Any_vertex Opt_labeled_term
Optional_term Syntax_vertex .
31Class dictionary (part 1)
getAll from Cd_graph bypassing Neighbors to
Vertex checkAll from Cd_graph through Neighbors
to Vertex
Cd_graph ltadjacenciesgt Nlist(Adjacency)
EOF. Adjacency lt source gt Vertex lt ns gt
Neighbors "." . Neighbors Neighbors_wc
. Neighbors_wc Construct_ns Alternat_ns
common lt construct_ns gt List(Any_vertex). Construc
t_ns "". Alternat_ns "" lt alternat_ns gt
Bar_list(Term) ltcommongt Common. Common
"common". Any_vertex Opt_labeled_term
Optional_term Syntax_vertex .
32Class dictionary (part 2)
getAll from Cd_graph bypassing Neighbors to
Vertex checkAll from Cd_graph through Neighbors
to Vertex
Vertex lt vertex_name gt Ident. Syntax_vertex
Regular_syntax common. Regular_syntax lt string
gt String . Opt_labeled_term Labeled Regular
common ltvertexgt Term. Regular . Labeled "lt" lt
label_name gt Ident "gt" . Term Normal common
ltvertexgt Vertex. Normal . Optional_term ""
ltoptgt Sandwich(Opt_labeled_term) "".
33UML class diagram ClassG
getAll from ClassG bypassing Body to ClassName
Entry
0..
EParse
entries
ClassG
BParse
ClassDef
Body
Part
parts
className
0..
ClassName
Concrete
Abstract
34UML class diagram ClassG
checkAll from ClassG through Body to ClassName
Entry
0..
EParse
entries
ClassG
BParse
ClassDef
Body
Part
parts
className
0..
ClassName
Concrete
Abstract
35Equation System
change of domain from Computer Science to
Mathematics
Example x 1.0 . y ( x 4.0). z ( x y).
EquationSystem ltequationsgt List(Equation). Equat
ion ltlhsgt Variable ltrhsgt Expression
.. Variable Ident. Expression Simple
Compound. Simple Variable Numerical. Compound
( Op ltargsgt List(Expression) ). Op Add
Mul. Add . Mul . Numerical float.
Write a program for Are all variables in an
equation system defined? (disregard order of
equations)
36Make More Reusable
- Cd_graph
- String vi from Vertex to edu.neu.ccs.demeter
.Ident - void isDefined(ClassGraph cg)
- checkDefined(cg, getClasses(cg))
- HashSet getClasses(ClassGraph cg)
- Strategy getAll new Strategy(
- "from Cd_graph bypassing Neighbors
to Vertex") - TraversalGraph tg new TraversalGraph(getAll,
cg) - Visitor v new Visitor()
- HashSet return_val new HashSet()
- void before(Vertex v)
- return_val.add(cg.fetch(v, vi) )
- public Object getReturnValue()return
return_val - tg.traverse(this,v)
- return (HashSet)v.getReturnValue()
-
37Make More Reusable
- void checkDefined(ClassGraph cg, HashSet
tempClassSet) - final HashSet classHash tempClassSet
- Strategy checkAll new Strategy(
- from Cd_graph through Neighbors to
Vertex") - TraversalGraph tg new TraversalGraph(checkAll,
cg) - tg.traverse(this, new Visitor()
- void before(Vertex v) Ident vn cg.fetch(v,
vi) - if (!classHash.contains(vn))
- System.out.println("The object "
vn - " is undefined.")
- )
-
38Equation System
Example x 1.0 . y ( x 4.0). z ( x y).
EquationSystem ltequationsgt List(Equation). Equat
ion ltlhsgt Variable ltrhsgt Expression
.. Variable Ident. Expression Simple
Compound. Simple Variable Numerical. Compound
( Op ltargsgt List(Expression) ). Op Add
Mul. Add . Mul . Numerical float.
Write a program for Are all variables in an
equation system defined? (disregard order of
equations)
39Name map
40Find undefined things
Adaptive Object-Oriented Design
definedThings
System
Thing
usedThings
UsedThingsHolder
definedThings from System bypassing
UsedThingsHolder to Thing usedThings from
System through UsedThingsHolder to Thing
41Equation System
usedThings from EquationSystem through
Expression to Variable
Example x 1.0 . y ( x 4.0). z ( x y).
EquationSystem ltequationsgt List(Equation). Equat
ion ltlhsgt Variable ltrhsgt Expression
.. Variable Ident. Expression Simple
Compound. Simple Variable Numerical. Compound
( Op ltargsgt List(Expression) ). Op Add
Mul. Add . Mul . Numerical float.
42Equation System
definedThings from EquationSystem bypassing
Expression to Variable
Example x 1.0 . y ( x 4.0). z ( x y).
EquationSystem ltequationsgt List(Equation). Equat
ion ltlhsgt Variable ltrhsgt Expression
.. Variable Ident. Expression Simple
Compound. Simple Variable Numerical. Compound
( Op ltargsgt List(Expression) ). Op Add
Mul. Add . Mul . Numerical float.
43Name map
44Conclusion Collaborations
- Collaborations are an important architectural
element in object-oriented design. - Separate collaboration from its use.
45Collaboration CheckUnique3 Roles
- //checks for unique parts.
- // Collaboration CheckUnique
- // role System
- // out void checkUnique(final ClassGraph cg)
- // in Strategy getAllUnitsToBeChecked
- // role UnitToBeChecked
- // in Strategy checkAllParts
- // role NotDuplicated
- // System List(UnitToBeChecked).
- // UnitToBeChecked List(NotDuplicated).
- // NotDuplicated Ident.
46Collaboration CheckUniquewith role play
- //checks for unique parts.
- // Collaboration CheckUnique
- // role System played by Grammar
- // out void checkUnique(final ClassGraph cg)
- // in Strategy getAllUnitsToBeChecked
- // role UnitToBeChecked played by Statement
- // in Strategy checkAllParts
- // role NotDuplicated played by NonTerminal
- // System List(UnitToBeChecked).
- // UnitToBeChecked List(NotDuplicated).
- // NotDuplicated Ident.
47Collaboration CheckUniquewith role play 2
- //checks for unique parts.
- // Collaboration CheckUnique
- // role System played by Cd_graph
- // out void checkUnique(final ClassGraph cg)
- // in Strategy getAllUnitsToBeChecked
- // role UnitToBeChecked played by Adjacency
- // in Strategy checkAllParts
- // role NotDuplicated played by Vertex
- // System List(UnitToBeChecked).
- // UnitToBeChecked List(NotDuplicated).
- // NotDuplicated Ident.
48Program part 1
- System
- Strategy getAllUnitsToBeChecked
- from System to UnitToBeChecked
- void checkUnique(final ClassGraph cg)
- cg.traverse(this,
- getAllUnitsToBeChecked,
- new Visitor()
- void before(UnitToBeChecked a)
- a.checkDuplicateParts(cg) )
49Program part 2
- UnitToBeChecked
- Strategy checkAllParts
- from UnitToBeChecked to NotDuplicated
- void checkDuplicateParts(ClassGraph cg)
- cg.traverse(this, checkAllParts,
- new Visitor()
- HashSet hParts new HashSet()
- void before(NotDuplicated l)
- if(!hParts.add(l.get_ident()))
- System.out.println("Element
" l.get_ident() " is not unique.") - )
50Map
- Generic
- System
- UnitToBeChecked
- NotDuplicated
- Project
- Cd_graph
- Adjacency
- Vertex