Chair of Software Engineering ATOT - Lecture 14, 19 May 2003 1 Advanced Topics in Object Technology...
-
date post
20-Jan-2016 -
Category
Documents
-
view
214 -
download
0
Transcript of Chair of Software Engineering ATOT - Lecture 14, 19 May 2003 1 Advanced Topics in Object Technology...
ATOT - Lecture 14, 19 May 2003
1
Chair of Software Engineering
Advanced Topics in Object Technology
Bertrand Meyer
ATOT - Lecture 14, 19 May 2003
2
Chair of Software Engineering
Lecture 14:
Advanced inheritance mechanisms
ATOT - Lecture 14, 19 May 2003
3
Chair of Software Engineering
Deferred classes
deferred class STACK [G]
feature
put (x: G) is-- Push x on top of stack.
requirenot full
deferredensure
item = xnot empty
end
ATOT - Lecture 14, 19 May 2003
4
Chair of Software Engineering
The STACK class (cont’d)
remove is-- Pop top element of stack.
requirenot empty
deferredensure
not fullcount = old count – 1
end
ATOT - Lecture 14, 19 May 2003
5
Chair of Software Engineering
The STACK class (cont’d)
full: BOOLEAN is-- Is there no room place for new items?
deferredend
empty: BOOLEAN is-- Is there no item?
deferredend
count: BOOLEAN is-- Number of items on stack
deferredensure
Result >= 0end
ATOT - Lecture 14, 19 May 2003
6
Chair of Software Engineering
The STACK class (cont’d)
-- Not all features need be deferred!
replace (x: G) is-- Replace top element of stack by x.
requirenot empty
doremoveput (x)
ensurenot emptyitem = xcount = old count
end
invariant
not (full and empty)
end
ATOT - Lecture 14, 19 May 2003
7
Chair of Software Engineering
Applications of deferred classes
Taxonomy Library organization Capturing common abstractions Capturing common behaviors “Don’t call us, we’ll call you” Analysis High-level architecture and design
ATOT - Lecture 14, 19 May 2003
8
Chair of Software Engineering
EiffelBase: Container structures
CONTAINER*
BOX* COLLECTION* TRAVERSABLE*
FINITE* INFINITE*
BOUNDED* UNBOUNDED* COUNTABLE*
RESIZABLE*
BAG* SET* HIERARCHICAL* LINEAR*
TABLE* ACTIVE* INTEGER_ INTERVAL
* BILINEAR*
INDEXABLE* CURSOR_ STRUCTURE
* DISPENSER* SEQUENCE*
ARRAY STRING HASH_TABLE STACK* QUEUE*
… …
ATOT - Lecture 14, 19 May 2003
9
Chair of Software Engineering
deferred class VAT
inherit
TANK
feature
in_valve, out_valve: VALVE
fill is-- Fill the vat.
requirein_valve.openout_valve.closed
deferredensure
in_valve.closedout_valve.closedis_full
end
empty, is_full, is_empty, gauge, maximum, ... [Other features] ...
invariant
is_full = (gauge >= 0.97 * maximum) and (gauge <= 1.03 * maximum)
end
Object-oriented analysis
ATOT - Lecture 14, 19 May 2003
10
Chair of Software Engineering
Indirect and direct repeated inheritance
A
B C
D
A
D
ATOT - Lecture 14, 19 May 2003
11
Chair of Software Engineering
Repeated inheritance
Assume class TAXPAYER with attributes
age: INTEGERaddress: STRINGbank_account: ACCOUNTtax_id: INTEGER
and routines such as
pass_birthday is do
age := age + 1 end
pay_taxes is ...
deposit_to_account (sum: INTEGER) is ...
TAXPAYER addresstax_idpass_birthday
age
pay_taxes
ATOT - Lecture 14, 19 May 2003
12
Chair of Software Engineering
Repeated inheritance (cont’d)
Heirs may include SWISS_TAXPAYER and US_TAXPAYER.
TAXPAYER addresstax_id
age
pay_taxespass_birthday
US_ TAXPAYER
SWISS_ TAXPAYER
ATOT - Lecture 14, 19 May 2003
13
Chair of Software Engineering
Repeated inheritance (cont’d)
The two above classes may in turn have a common heir: SWISS_US_TAXPAYER.
TAXPAYER addresstax_id
pay_taxespass_birthday
US_ TAXPAYER
SWISS_ TAXPAYER
SWISS_US_ TAXPAYER
ATOT - Lecture 14, 19 May 2003
14
Chair of Software Engineering
Repeated inheritance issues
What happens with features inherited twice from the common ancestor TAXPAYER, such as address, age, tax_id, pass_birthday?
ATOT - Lecture 14, 19 May 2003
15
Chair of Software Engineering
The inheritance clause
inherit
SWISS_TAXPAYERrename
address as swiss_address, tax_id as swiss_tax_id,pay_taxes as pay_swiss_taxes,bank_account as swiss_bank_account,deposit_to_account as deposit_to_swiss_account,...
end
US_TAXPAYERrename
address as us_address, tax_id as us_tax_id,pay_taxes as pay_us_taxes, bank_account as us_bank_account,deposit_to_account as deposit_to_us_account,...
end
ATOT - Lecture 14, 19 May 2003
16
Chair of Software Engineering
Sharing and replication
Features such as age and birthday, not renamed along any of the inheritance paths, will be shared.
Features such as tax_id, inherited under different names, will be replicated.
TAXPAYERaddresstax_id
pay_taxespass_birthday
US_ TAXPAYER
SWISS_ TAXPAYER
SWISS_US_ TAXPAYER
address us_addresstax_id us_tax_idpay_taxes pay_us_taxes
address swiss_addresstax_id swiss_tax_idpay_taxes pay_swiss_taxes
ATOT - Lecture 14, 19 May 2003
17
Chair of Software Engineering
The need for select
Assume there is a redefinition somewhere along the way:
TAXPAYER address
US_ TAXPAYER
SWISS_ TAXPAYER
SWISS_US_ TAXPAYER
address++address++
address us_address address swiss_address
ATOT - Lecture 14, 19 May 2003
18
Chair of Software Engineering
The need for select (cont’d)
A potential ambiguity arises because of polymorphism and dynamic binding:
t: TAXPAYERs: SWISS_TAXPAYERu: US_TAXPAYERsu: SWISS_US_TAXPAYER
if ... thent := s
elset := su
end...print (t.address)
ATOT - Lecture 14, 19 May 2003
19
Chair of Software Engineering
Removing the ambiguity
class SWISS_US_TAXPAYER
inherit
SWISS_TAXPAYERrename
address as swiss_address, tax_id as swiss_tax_id,pay_taxes as pay_swiss_taxes,bank_account as swiss_bank_account,deposit_to_account as deposit_to_swiss_account,...
selectswiss_address, swiss_tax_id, pay_swiss_taxes, swiss_bank_account, deposit_to_swiss_account
end
US_TAXPAYERrename
address as us_address, tax_id as us_tax_id,...
end
ATOT - Lecture 14, 19 May 2003
20
Chair of Software Engineering
Creating with a specified type
To avoid this:
a1: Ab1: B...create b1.make (...)a1 := b1
Simply use
a1: A...create {B} a1.make (...)
A
B C
ATOT - Lecture 14, 19 May 2003
21
Chair of Software Engineering
Once routines
If instead of
r isdo
... Instructions ...end
you write
r isonce
... Instructions ...end
then Instructions will be executed only for the first call by any client during execution. Subsequent calls return immediately.
In the case of a function, subsequent calls return the result computed by the first call.
ATOT - Lecture 14, 19 May 2003
22
Chair of Software Engineering
Scheme for shared objects
class SHARED_OBJECTS
feature
error_window: WINDOW isonce
create Result.make (...) end
exit_button: BUTTON isonce
create Result.make (...) end
...end
class MY_APPLICATION_CLASS
inherit
SHARED_OBJECTS
feature
r is do
error_window.put (my_error_message)end
...end
ATOT - Lecture 14, 19 May 2003
23
Chair of Software Engineering
Undefining a feature
deferred class B
inherit
Aundefine
fend
feature
...
end
ATOT - Lecture 14, 19 May 2003
24
Chair of Software Engineering
Feature merging
A B C
D
f* f* f+
ATOT - Lecture 14, 19 May 2003
25
Chair of Software Engineering
Feature merging (cont’d)
class D
inherit
A
B
C
feature
...
end
ATOT - Lecture 14, 19 May 2003
26
Chair of Software Engineering
Feature merging: with different names
A B C
D
g* f* h+
g f h f
ATOT - Lecture 14, 19 May 2003
27
Chair of Software Engineering
Feature merging: with different names
class D
inherit
Arename
g as f end
B
Crename
h as f end
feature...
end
ATOT - Lecture 14, 19 May 2003
28
Chair of Software Engineering
Feature merging: effective features
a1: A b1: B c1: C d1: Da1.g b1.f c1.h d1.f
A B C
D
g* f* h+
g f h ff- f-
ATOT - Lecture 14, 19 May 2003
29
Chair of Software Engineering
Feature merging: effective features
class D
inherit
Arename
g as f undefine
fend
B
Crename
h as f undefine
fend
feature...
end
ATOT - Lecture 14, 19 May 2003
30
Chair of Software Engineering
When is a name clash acceptable?
(Between n features of a class, all with the same name, immediate or inherited.)
They must all have compatible signatures.
If more than one is effective, they must all come from a common ancestor feature under repeated inheritance.
ATOT - Lecture 14, 19 May 2003
31
Chair of Software Engineering
Feature adaptation clauses
rename export undefine redefine select
ATOT - Lecture 14, 19 May 2003
32
Chair of Software Engineering
Export adaptation
class B
inherit
Aexport
{ANY} all{NONE} h{A, B, C, D} i, j, k
end
feature
...
ATOT - Lecture 14, 19 May 2003
33
Chair of Software Engineering
End of lecture 14