Chair of Software Engineering
Einführung in die Programmierung Introduction to Programming
Prof. Dr. Bertrand Meyer
Lecture 9: Abstraction Topics for today
Abstraction, especially fflunctional abstraction
The notion of routine
The final word on features: all feature categories
The Uniform Access ppprinciple
Abstraction and client privileges
Information hiding
2 Routine: algorithm abstraction
To abstract is to capture the essence behind the details and the specifics
Implies giving a name to the result
In programming:
¾ Data abstraction: class
¾ Algorithm (operational) abstraction: routine
3 A routine is one of the two kinds of feature...
... the oth er is attribute
We have encountered lots of routines already, without the name.
4 A routine r (arg : TYPE ; ...) is -- Header comment require Precondition (boolean expression) do
Body (instructions)
ensure Postcondition (boolean expression) end
5 Uses of routines Bottom-up: capture existing algorithm, possibly for reuse
Top-down: placeholder routines — attractive alternative to pseudddocode.
build a_ linea_line is create fancy_fancy line_line is -- Build imaginary line. -- Create line and fill do -- stations. Paris.disppylay do -- To be completed Metrocreate_fancy_line.highlight -- BM, 26 Oct 08 end end
6 Two kinds of routine
Procedure: dd’oesn’t return a resul t Yields a command CllCalls are itinstruc tions
Function: returns a result
f (arg : TYPE; ...): RESULT TYPE_TYPE is ... (The rest as before) ...
Yields a query Calls are expressions
7 Features: the full story
A class is characterized by its features Each feature is an operation on the corresponding objects: query or command Features are grouped into categories for readability Class clauses: ¾ Indexing ¾ Inheritance ¾ Creation ¾ Feature (any number) ¾ Invariant Anatomy of a class: Demo
8 Features: the full story
Client view (specification) (implementation) Command Procedure
No result Routine
ComputationInternal view Feature Memory Feature Returns result Function
Computation Query Memory Attribute
9 Uniform access principle
It doesn‘t matter to the client whthhether you lklook up or compute
A call such as
yoontour_account.blnebalance could use an attribute or a function
10 Uniform Access: an example
blbalance = lfdlist_of_deposits.total – lfhdllist_of_withdrawals.total
list_of_deposits
(()A1) list_of_withdrawals
balance
list_of_deposits (A2) list_of_withdrawals
11 Uniform Access Principle
Expressed more technically:
Features should be accessible to clients the same way whether implemented by storage or by computation
12 An object has an interface
set x y set x_x set_y
13 An object has an implementation
set
firstx y set x_x set_y count
14 Information hiding
set
x y set x_x set_y
15 What clients may do
class METRO STATION_STATION feature
x, y: REAL -- Coordinates of metro station size: REAL -- Size of bounding square
upper_lftleft: POSITION -- Upper-left position of bounding square
adjust positions_positions is -- Set positions of bounding square do upper_left . set (x – size/2, y + size/2 ) ... end
end
16 What clients may not do
class METRO STATION_STATION feature
x, x, y: REAL -- Coordinates of metro station size: REAL -- Size of bounding square
upper_lftleft: POSITION -- Upper-left position of bounding square-- Upperposition of bounding square adjj_pust_positions is ---- Set positions of bounding square do upper_left . x := 3 ... end NOT PERMITTED! end
17 Use procedures:
upper_left.set (3, upper_left.y )
upper_left.set_x (3 )
upper_left.move (3, h)
18 Possible client privileges
If class A has an attribute att : SOME_TYPE, what may a client class C with att a : A a CA do with aatt ? : A The attribute may be: Read, Read-only Secret restricted write Full write
Example: modify x Modify through with move in “set_...” procedure POINT
19 Possible client privileges
If class A has an attribute att : SOME_TYPE, what may a client class C with a : A C A a : A att do with aatt ? The attribute may be: Read, Read-only Secret restricted write Full write
Modify through Modify through a some_procedure a set_att (v)
a att a att permitted in C (for access) invalid
20 Abstraction and client privileges
If class A has an attribute att : SOME_TYPE, what may a client class C with att a : A a CA do with aatt ? : A
Read access if attribute is exported ¾a.att is an expression.
¾An assignment a.att := v would be syntactically illegal!
(It would assign to an expression, like x + y := v.)
21 Applying abstraction principles
Beyon d rea d access: fu ll or res tr ic te d wr ite, throug h exported procedures.
Full write privileges: set_attribute procedure, e.g. set_temperature (u : REAL) is -- Set temperature value to u. do temperature := u ensure temperature_set: temperature = u end
Client will use e.g. x.set_temperature (21.5).
22 Other uses of a setter procedure
set_temperature (u : REAL) is -- Set temperature value to u. require not_under_minimum: u >= -273 not_abibove_maximum: u <= 2000 do temperature := u update_database ensure temperature_set: temperature = u end
23 Having it both ways
Make it possib blle to ca ll a setter proce dure te Emp L erature: REAL assign set_ temperature
Then the syntax x.temperature := 21.5 is accepted as a shorthand for x.set_temperature (21.5)
Retains contracts etc.
24 Information hiding class A Status of calls in a client with a1: A: feature f ... ¾ a1.f, a1.g: valid in any client g ... ¾ a1 h: invalid everywhere feature {NONE} . (including in A’s own text!) h, i ... feature {B, C} ¾ a1.j: valid only in B, C and their descendants j, k, l ... (not valid in A!) feature {A, B, C} ¾ a1.m: valid in B, C and their descendants, m, n… as well as in A and its descendants end
25 An example of selective export
LINKABLE exports its features to LINKED_LIST ¾ Does not export them to the rest of the world ¾ Clients of LINKED LIST_LIST don’ t need to know about LINKABLE cells.
count 3
first_element
active
Haldenegg Central Haupt- bahnhof
item item item right right right
26 Exporting selectively
These feat ures are sel ecti vel y exported to LINKED_LIST and its class descendants (and no other classes) LINKABLE [G] feature {LINKED_LIST }
put_right (...) is do ... end
right: G is do ... end
... end
27 Information hiding
Information hiding only applies to use by clients, using dot notation or infix notation, as with a1.f (Qualified calls).
Unqualified calls (within class) not subject to information hiding:
class A feature {NONE } h is ... do ... end feature
f is do ...; h ; ... end end
28 What we have seen
The full categorization of features Routines, procedures, functions Uniform access Information hiding Selective exports Setters and getters
Eiffel: assig ner commands
29