05 Softarch Adt
05 Softarch Adt
Software Architecture
Bertrand Meyer, Carlo A. Furia, Martin Nordio
1. Modularity
3. Object-oriented analysis
2
Reading assignment for this week
OOSC, chapters
3: Modularity
6: Abstract data types
In particular pp.153-159,
sufficient completeness
3
Modularity
General goal:
4
Modularity
5
Decomposability
6
Top-down functional design
Sequence
B C D
Loop Conditional
E1 I I1 C2 I2
7
Top-down design
http://www.acm.org/classics/dec95/
8
Composability
9
Direct Mapping
10
Few Interfaces principle
11
Small Interfaces principle
x, y
12
Explicit Interfaces principle
A B
modifies accesses
Data
item
x
13
Continuity
14
Uniform Access principle
A call such as
.
your_account balance
15
Uniform Access
list_of_deposits
(A1) list_of_withdrawals
balance
list_of_deposits
(A2)
list_of_withdrawals
17
Information Hiding
18
Information Hiding Principle
Public
The designer of every
module must select a subset
of the module‘s properties as
the official information
about the module, to be made
available to authors of client
modules
Private
19
Information hiding
Justifications:
Continuity
Decomposability
20
An object has an interface
21
An object has an implementation
22
Information hiding
23
The Open-Closed Principle
Definitions:
Open module: May be extended.
Closed module: Usable by clients. May be approved,
baselined and (if program unit) compiled.
24
The Open-Closed principle
B A C E
F A’ H I
25
The Single Choice principle
26
Reusability: Technical issues
27
Issues for a general searching module
Type variation:
What are the table elements?
Routine grouping:
A searching routine is not enough: it should be
coupled with routines for table creation, insertion,
deletion etc.
Implementation variation:
Many possible choices of data structures and
algorithms: sequential table (sorted or unsorted),
array, binary search tree, file, ...
28
Issues
Representation independence:
has (t1, y )
29
Issues
Factoring out commonality:
How can the author of supplier modules take advantage
of commonality within a subset of the possible
implementations?
30
Factoring out commonality
TABLE has
start
after SEQUENTIAL TREE_ HASH_
found _TABLE TABLE TABLE
forth
31
Implementation variants
Linked c := first .
c := c right c = Void .
c item = x
list
32
Encapsulation languages (“Object-based”)
Ada, Modula-2, Oberon, CLU...
Advantages:
For supplier author: Get everything under one roof.
Simplifies configuration management, change of
implementation, addition of new primitives.
For client author: Find everything at one place. Simplifies
search for existing routines, requests for extensions.
33
The concept of Abstract Data Type (ADT)
Why use the objects?
The need for data abstraction
Moving away from the physical representation
Abstract data type specifications
Applications to software design
34
The first step
Actions Objects
Processor
35
Finding the structure
36
Arguments for using objects
Employee
information
Produce Paychecks
Paychecks
Hours
worked
37
Object technology: A first definition
38
The O-O designer’s motto
39
Issues of object-oriented architecture
40
Description of objects
41
The theoretical basis
42
Abstract Data Types
43
A stack, concrete object
1
rep
44
A stack, concrete object
capacity Implementing a ―PUSH‖ operation:
x count
count := count + 1
Representation 1: rep [count] := x
―Array Up‖
1
rep
x
rep [free] := x
free := free - 1
Representation 2:
―Array Down‖ x free
1
rep
45
A stack, concrete object
capacity Implementing a ―PUSH‖ operation:
1
rep
rep [free] := x
free := free - 1
Representation 2:
―Array Down‖ x
free
1
rep
cell create cell
x item previous
cell.item := x
item previous cell.previous := last
Representation 3: previous
―Linked List‖ item head := cell
46
Stack: An Abstract Data Type (ADT)
Types:
STACK [G ]
-- G : Formal generic parameter
Functions (Operations):
put : STACK [G ] G STACK [G ]
remove : STACK [G ] STACK [G ]
item : STACK [G ] G Partial function
is_empty : STACK [G ] BOOLEAN (see next)
new : STACK [G ]
47
Using functions to model operations
put ( , ) =
s x s‘
48
Reminder: Partial functions
inverse (x ) = 1 / x
49
The STACK ADT (continued)
Preconditions:
remove (s : STACK [G ]) require not is_empty (s )
item (s : STACK [G ]) require not is_empty (s )
50
Exercises
51
Formal stack expressions
value =
item (remove (put (remove (put (put
(remove (put (put (put (new, x8 ), x7 ), x6 )),
52
Expressed differently
53
Expression reduction
value = item (
remove (
put (
remove (
put ( Stack 1
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put (put (new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
54
Expression reduction
value = item (
remove (
put (
remove ( x8
put ( Stack 1
put (
remove (
put (put ( put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put (put (new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
55
Expression reduction
value = item (
remove (
put ( x7
remove ( x8
put ( Stack 1
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put (put (new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
56
Expression reduction
value = item (
remove ( x6
put ( x7
remove ( x8
put ( Stack 1
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
)
, item (
remove (
put (put (new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
57
Expression reduction
value = item (
remove ( x6
put ( x7
remove ( x8
put ( Stack 1
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put (put (new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
58
Expression reduction
value = item (
remove (
put ( x7
remove ( x8
put ( Stack 1 Stack 2
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put ( put ( new , x5 ) , x4 )
)
)
),
x2 )
),
x1 )
)
)
59
Expression reduction
value = item (
remove (
put ( x7
remove ( x8 x5
put ( Stack 1 Stack 2
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put ( put (new, x5 ) , x4 )
)
)
),
x2 )
),
x1 )
)
)
60
Expression reduction
value = item (
remove (
put ( x7 x4
remove ( x8 x5
put ( Stack 1 Stack 2
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put (put ( new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
61
Expression reduction
value = item (
remove (
put ( x7 x4
remove ( x8 x5
put ( Stack 1 Stack 2
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put (put (new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
62
Expression reduction
value = item (
remove (
put ( x7
remove ( x8 x5
put ( Stack 1 Stack 2
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put (put (new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
63
Expression reduction
value = item (
remove ( x5
put ( x7
remove ( x8 x5
put ( Stack 1 Stack 2
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put (put (new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
64
Expression reduction
x2
value = item (
remove ( x5
put ( x7
remove ( x8
put (
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put (put (new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
65
Expression reduction
x2
value = item (
remove ( x5
put ( x7
remove ( x8
put (
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put (put (new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
66
Expression reduction
value = item ( x1
remove ( x5
put ( x7
remove ( x8
put (
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put (put (new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
67
Expression reduction
x1
value = item (
x5
remove (
put ( x7
remove ( x8
put (
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put (put (new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
68
Expression reduction
value = item (
x5
remove (
put ( x7
remove ( x8
put (
put (
remove (
put (put (put ( new , x8 ), x7 ), x6 )
),
item (
remove (
put (put (new, x5 ), x4 )
)
)
),
x2 )
),
x1 )
)
)
69
Expressed differently
70
An operational view of the expression
x6
x7 x7 x4 y1
x8 x8 x5 x5
s1 s2 s3 s4 s5 s6
(empty) (empty)
x2 x1
x5 x5 x5
x7 x7 x7
x8 x8 x8
s7 (s9, s11) s8 s10
value = item (remove (put (remove (put (put (remove (put (put (put
(new, x8 ), x7 ), x6)), item (remove (put (put (new, x5 ), x4 )))), x2 )), x1 )))
71
Sufficient completeness
73
Is the stack specification sufficiently complete?
Types
STACK [G]
Functions
put: STACK [G] G STACK [G]
remove: STACK [G] STACK [G]
item: STACK [G] G
empty: STACK [G] BOOLEAN
new: STACK [G]
74
The STACK ADT (continued)
Preconditions:
remove (s : STACK [G ]) require not empty (s )
item (s : STACK [G ]) require not empty (s )
75
Proving sufficient completeness
Let us say that an ADT expression f for the ADT
STACK [X] is reducible if the axioms make it possible to
determine the value of f in terms of the types X and
BOOLEAN only
item (e) or
empty (e)
is reducible
(to terms of values X and BOOLEAN respectively)
76
Proof sketch
Lemma:
A correct expression of the form
remove (g)
has a subexpression of the form
remove (put (h, x))
77
“Complete” requirements (from the Requirements lecture)
Complete with respect to what?
78
Completeness (from the Requirements lecture)
79
ADTs and software architecture
80
Implementing an ADT
Three components:
(E1) The ADT‘s specification: functions,
axioms, preconditions
(Example: stacks)
81
A choice of stack representation
1
rep
82
Information hiding
Public
The designer of every
module must select a subset
of the module‘s properties as
the official information
about the module, to be made
available to authors of client
modules
Private
83
Applying ADTs to information hiding
Public part:
ADT specification
(E1 )
Secret part:
Choice of representation
(E2 )
Implementation of
functions by features
(E3 )
84
Object technology: A first definition
85
A more precise definition
86
The fundamental structure: the class
Merging of the notions of module and type:
The connection:
The services offered by the class, viewed as a
module, are the operations available on the instances
of the class, viewed as a type.
87
Class relations
Two relations:
Client
Heir
88
Overall system structure
space_before add_space_before
CHUNK FEATURES
space_after add_space_after
QUERIES COMMANDS
length set_font
add_word font hyphenate_on
FIGURE hyphenate_off
remove_word
PARAGRAPH WORD
justify
word_count
unjustify
justified
Inheritance
Client
89
Bounded stacks
Types:
BSTACK [G]
Functions (Operations):
put : BSTACK [G] G BSTACK [G]
remove : BSTACK [G] BSTACK [G]
item : BSTACK [G] G
empty : BSTACK [G] BOOLEAN
new : BSTACK [G]
capacity : BSTACK [G] INTEGER
count : BSTACK [G] INTEGER
90
Television station example
Source: OOSC
91
91
Schedules
note set_air_time (t : DATE)
description : -- Assign schedule to
― 24-hour TV schedules‖ -- be broadcast at time t.
deferred class SCHEDULE feature require
.
t in_future
segments: LIST [SEGMENT ] deferred
-- Successive segments ensure
deferred air_time = t
end end
print
air_time : DATE is -- Produce paper version.
-- 24-hour period
deferred
-- for this schedule
deferred end
end end
92
92
Contracts
93
93
Why contracts
94
94
Segment
note sponsor : COMPANY deferred end
-- Segment‘s principal sponsor
description :
"Individual fragments of a schedule "
deferred class SEGMENT feature rating : INTEGER deferred end
-- Segment‘s rating (for
schedule : SCHEDULE deferred end
-- children‘s viewing etc.)
-- Schedule to which
-- segment belongs
index : INTEGER deferred end Commands such as change_next,
-- Position of segment in set_sponsor, set_rating omitted
-- its schedule
starting_time, ending_time : Minimum_duration : INTEGER = 30
INTEGER deferred end -- Minimum length of segments,
-- Beginning and end of -- in seconds
-- scheduled air time
next: SEGMENT deferred end Maximum_interval : INTEGER = 2
-- Segment to be played -- Maximum time between two
-- next, if any -- successive segments, in seconds
95
95
Segment (continued)
invariant
in_list: (1 <= index) and (index <= schedule.segments.count)
in_schedule: schedule.segments.item (index) = Current
next_in_list: (next /= Void ) implies
(schedule.segments.item (index + 1) = next)
no_next_iff_last: (next = Void) = (index = schedule.segments.count)
non_negative_rating: rating >= 0
positive_times: (starting_time > 0 ) and (ending_time > 0)
sufficient_duration:
ending_time – starting_time >= Minimum_duration
decent_interval :
.
(next starting_time) - ending_time <= Maximum_interval
end
96
96
Commercial
note set_primary (p: PROGRAM)
description: "Advertizing segment " -- Attach commercial to p.
deferred class COMMERCIAL inherit require
SEGMENT
program_exists: p /= Void
rename sponsor as advertizer end
same_schedule: p,schedule = schedule
feature
primary: PROGRAM deferred before:
-- Program to which this p.starting_time <= starting_time
-- commercial is attached deferred
primary_index: INTEGER deferred ensure
-- Index of primary
index_updated:
primary_index = p.index
primary_updated: primary = p
end
invariant
meaningful_primary_index: primary_index = primary.index
primary_before: primary.starting_time <= starting_time
.
acceptable_sponsor: advertizer compatible (primary.sponsor)
acceptable_rating: rating <= primary.rating
end
97
97
Diagrams: UML, BON
Text-Graphics
Equivalence
98
98
O-O analysis process
Identify abstractions
New
Reused
Describe abstractions through interfaces, with contracts
Look for more specific cases: use inheritance
Look for more general cases: use inheritance, simplify
Iterate on suppliers
99
99
Your turn! Describe this in an O-O way
Consider a small library database Transactions 1, 2, 4, and 5 are
with the following transactions: restricted to staff users, except
1. Check out a copy of a book. that ordinary borrowers can
Return a copy of a book. perform transaction 4 to find
2. Add a copy of a book to the out the list of books currently
library. Remove a copy of a borrowed by themselves. The
book from the library. database must also satisfy the
following constraints:
3. Get the list of books by a
particular author or in a All copies in the library must
particular subject area. be available for checkout or
be checked out.
4. Find out the list of books
currently checked out by a No copy of the book may be
particular borrower. both available and checked
out at the same time.
5. Find out what borrower last
checked out a particular copy A borrower may not have
of a book. more than a predefined
number of books checked out
There are two types of users: staff at one time.
users and ordinary borrowers.
100
100
Practical advice
101
101
Practical advice
102
Bounded stacks (continued)
Preconditions:
remove (s : BSTACK [G]) require not empty (s)
item (s : BSTACK [G]) require not empty (s)
put (s : BSTACK [G]) require not full (s)
Axioms: For all x : G, s : BSTACK [G]
item (put (s, x)) = x
remove (put (s, x)) = s
empty (new)
not empty (put (s, x))
full = (count = capacity)
count (new) = 0
count (put (s, x)) = count (s) + 1
count (remove (s)) = count (s) - 1
103