UML Distilled - PowerPoint PPT Presentation

About This Presentation
Title:

UML Distilled

Description:

( disregard order. of equations) change of domain: from Computer Science. to Mathematics ... ( disregard order. of equations) Name map. Find undefined things ... – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 51
Provided by: karllie
Category:

less

Transcript and Presenter's Notes

Title: UML Distilled


1
UML Distilled
2
Design 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.

3
Rights 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

4
Rights and obligations
  • Precondition binds clients
  • Postcondition binds class

5
Example
6
If 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.

7
Source 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

8
Class 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)

9
Class 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)

10
Class invariants and class correctness
  • A class invariant only applies to public methods
    private methods are not required to maintain the
    invariant.

11
Invariant 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.

12
Invariant 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

13
Invariant 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

14
Resource 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
15
Collaborations 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.

16
Collaborations
  • 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.

17
Roles
  • Show roles that objects play in collaboration.
  • UML also uses the term pattern for a
    parameterized collaboration.

18
Collaboration 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.

19
Java 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()

20
Java 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.")
  • )

21
Find 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
22
High-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).

23
Collaboration 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

24
Collaboration 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

25
Use 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)

26
Use 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

27
The Collaboration in Action
  • Computer Science
  • Project
  • Smaller class graph for class graphs
  • Mathematics

28
Class 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 .
29
Class 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) "".
30
Class 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 .
31
Class 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 .
32
Class 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) "".
33
UML 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
34
UML 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
35
Equation 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)
36
Make 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()

37
Make 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.")
  • )

38
Equation 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)
39
Name map
40
Find 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
41
Equation 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.
42
Equation 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.
43
Name map
44
Conclusion Collaborations
  • Collaborations are an important architectural
    element in object-oriented design.
  • Separate collaboration from its use.

45
Collaboration 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.

46
Collaboration 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.

47
Collaboration 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.

48
Program 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) )

49
Program 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.")
  • )

50
Map
  • Generic
  • System
  • UnitToBeChecked
  • NotDuplicated
  • Project
  • Cd_graph
  • Adjacency
  • Vertex
Write a Comment
User Comments (0)
About PowerShow.com