einführung in die programmierung introduction to programming prof. dr. bertrand meyer

38
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10

Upload: casimir-lel

Post on 01-Jan-2016

18 views

Category:

Documents


0 download

DESCRIPTION

Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer. Exercise Session 10. Today. Multiple inheritance. Combining abstractions. Given the classes TRAIN_CAR , RESTAURANT how would you implement a DINER ?. Inheritance is never the only way. - PowerPoint PPT Presentation

TRANSCRIPT

1

Einführung in die ProgrammierungIntroduction to Programming

Prof. Dr. Bertrand Meyer

Exercise Session 10

2

Today

Multiple inheritance

3

Combining abstractions

Given the classes

TRAIN_CAR, RESTAURANT

how would you implement a DINER?

4

Inheritance is never the only way

Given the classes

TRAIN_CAR, RESTAURANT

how would you implement a DINER?

You could have an attribute in TRAIN_CAR

train_service: SERVICE

Then have RESTAURANT inherit from SERVICE

This is flexible if the kind of service may change to a type that is unrelated to TRAIN_CAR

Changes in TRAIN_CAR do not affect SERVICE easily

5

Examples of multiple inheritance

Combining separate abstractions:

Restaurant, train car Calculator, watch

Other examples?

Hands-On

6

Examples of multiple inheritance

Combining separate abstractions:

Restaurant, train car Calculator, watch

Other examples?

Teacher, student Home, vehicle

Hands-On

7

Multiple inheritance: Combining abstractions

COMPARABLE NUMERIC

STRING COMPLEX

INTEGER

REAL

<, <=, >, >=, …

+, –, *, / …(total order

relation)(commutative ring)

8

Composite figures

9

Multiple inheritance: Composite figures

A composite figure

Simple figures

10

Defining the notion of composite figure

COMPOSITE_FIGURE

centerdisplayhiderotatemove…

countputremove…

FIGUREV_LIST

[FIGURE ]

11

In the overall structure

COMPOSITE_FIGURE

FIGUREV_LIST

[FIGURE ]

OPEN_FIGURE

CLOSED_FIGURE

SEGMENT POLYLINE POLYGON ELLIPSE

RECTANGLE

SQUARE

CIRCLE

TRIANGLE

perimeter+

perimeter*

perimeter++

diagonal

perimeter++

perimeter++

perimeter+

12

A composite figure as a list

Cursor

item

forth

afterbefore

13

Composite figures

class COMPOSITE_FIGURE inheritFIGURE

V_LIST [FIGURE]feature

display-- Display each constituent figure in turn.

dofrom start until after loop

item.display

forth endend... Similarly for move, rotate etc. ...

end

Requires dynamic binding

14

An alternative solution: the composite pattern

COMPOSITE_FIGURE

FIGURELIST

[FIGURE ]

OPEN_FIGURE

CLOSED_FIGURE

SEGMENT POLYLINE POLYGON ELLIPSE

RECTANGLE

SQUARE

CIRCLE

TRIANGLE

perimeter+

perimeter*

perimeter++

diagonal

perimeter++

perimeter++

perimeter+

figure_list

16

Lessons from this example

Typical example of program with holes

We need the full spectrum from fully abstract (fully deferred) to fully implemented classes

Multiple inheritance is there to help us combine abstractions

17

Multiple inheritance: Name clashes

f

C

f A B

?

Hands-On

18

Resolving name clashes

f

rename f as A_f

C

f A B

A_f, f

Hands-On

19

Consequences of renaming

Valid or invalid?

a1 : Ab1 : Bc1 : C...

c1.fa1.A_f

c1.A_f

b1.f

b1.A_f

rename f as A_f

C

f A B

A_f, f

f

Hands-On

Invalid

Valid

Valid

Valid

Invalid

20

Are all name clashes bad?

A name clash must be removed unless it is: Under repeated inheritance (i.e. not a real clash)

Between features of which at most one is effective(i.e. others are deferred)

indirect repeated inheritance

direct repeated inheritance

21

Feature merging

A B C

D

f +f * f *

* Deferred

+ Effective

22

Feature merging: with different names

A B C

D

h +g * f *

* Deferred

+ Effective Renaming

g f h f

classD

inheritA

rename

g as f

end

B

C

rename

h as f

end

feature...

end

23

Feature merging: effective features

A B C

D

f +

f + f +

* Deferred

+ Effective-- Undefine

f --f --

24

Undefinition

deferred classT

inheritS

undefine v end

feature

...

end

25

Merging through undefinition

classD

inheritA

undefine f end

B

C

undefine f end

feature...

end

A B C

D

f +

f + f +

f -- f --

* Deferred

+ Effective-- Undefine

26

Merging effective features with different names

A B C

D

h +

f + g +

f --

f --

classD

inheritA

undefine f end

Brename

g as f

undefine f

end

Crename

h as f

end

feature ... end

h f

g f

27

Acceptable name clashes

If inherited features have all the same names, there is no harmful name clash if:

They all have compatible signatures At most one of them is effective

Semantics of such a case: Merge all features into one If there is an effective feature, it imposes its

implementation

29

Exercise: All-in-one-device

PRINTER

ALL_IN_ONE_DEVICE

Hands-On

SCANNER FAX

30

Exercise: All-in-one-deviceclass PRINTERfeature

print_page -- Print a page.do

print ("Printer prints a page...")

end

switch_on -- Switch from ‘off‘ to ‘on‘

doprint

("Printer switched on...")end

end

class FAXfeature

send -- Send a page over the phone net.

doprint (“Fax

sends a page...")end

start -- Switch from ‘off‘ to ‘on‘do

print (“Fax switched on...")

end

end

Hands-Onclass SCANNER

featurescan_page -- Scan a page.

doprint

(“Scanner scans a page...")end

switch_on -- Switch from ‘off‘ to ‘on‘

doprint

(“Scanner switched on...")end

send -- Send data to PC.do

print (“Scanner sends data...")

end

end

31

Exercise: All-in-one-device

PRINTER

ALL_IN_ONE_DEVICE

Hands-On

SCANNER FAX

classALL_IN_ONE_DEVICE

inherit...

end

How to resolve the name clashes?

switch_on send

32

Exercise: All-in-one-deviceclass ALL_IN_ONE_DEVICE

inheritPRINTER

renameswitch_on as start

undefinestart

end

SCANNERrename

switch_on as start,send as send_data

end

FAXrename

send as send_messageundefine

startend

feature ... end

Hands-On

33

Valid or invalid?

s: SCANNERf: FAXa: ALL_IN_ONE_DEVICE

a.switch_ona.print_pagef.send_messages.switch_onf.senda.send

Hands-Onclass ALL_IN_ONE_DEVICE

inheritPRINTER

rename

switch_on as startundefine

startend

SCANNERrename

switch_on as start,send

as send_dataend

FAXrename

send as send_message

undefinestart

end

feature ... end

Invalid

Valid

Invalid

Valid

Valid

Invalid

34

A special case of multiple inheritance

TEACHER STUDENT

ASSISTANT

UNIVERSITY_MEMBER id

This is a case of repeated inheritance

????

????

35

Indirect and direct repeated inheritance

A

D

B C

A

D

36

Multiple is also repeated inheritance

A typical case:

copy ++

is_equal ++

copy

is_equal

??

copy C_copyis_equal C_is_equal

CLIST

D

ANY

37

Sharing and replication

Features such as f, not renamed along any of the inheritance paths, will be shared.Features such as g, inherited under different names, will be replicated.

A

B C

D

fg

g g_b

g g_c

38

The need for select

A potential ambiguity arises because of polymorphism and dynamic binding:

a1 : ANYd1 : D

a1 := d1

a1.copy (…)

copy ++

is_equal ++

copy C_copyis_equal C_is_equal

CLIST

D

copy

is_equal ANY

39

Removing the ambiguity

classD

inheritV_LIST [T ]

selectcopy, is_equal

end

Crename

copy as C_copy, is_equal as C_is_equal,

...end

40

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.