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

118
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 12: Introduction to inheritance and genericity

Upload: brian

Post on 15-Feb-2016

30 views

Category:

Documents


0 download

DESCRIPTION

Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer. Lecture 12: Introduction to inheritance and genericity. On the menu for today (& next time). Two fundamental mechanisms for expressiveness and reliability: Genericity Inheritance - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

Chair of Software Engineering

Einführung in die ProgrammierungIntroduction to Programming

Prof. Dr. Bertrand Meyer

Lecture 12: Introduction to inheritance and genericity

Page 2: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

2

On the menu for today (& next time)Two fundamental mechanisms for expressiveness and reliability:

Genericity Inheritance

with associated (just as important!) notions: Static typing Polymorphism Dynamic binding

Page 3: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

3

But first…Some complements about the object model

Page 4: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

4

Variants of assignment and copy

Reference assignment (a and b of reference types): b := a

Object duplication (shallow): c := a.twin

Object duplication (deep): d := a.deep_twin

Also: shallow field-by-field copy (no new object is created):

e.copy (a)

Page 5: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

5

Shallow and deep cloningInitial situation:

Result of:b := a

c := a.twin

d := a.deep_twin

“Almaviva”namelandlord

loved_one

O1

“Figaro”O2 “Susanna”O3

b

“Almaviva”O4c

“Almaviva”O5

“Figaro”O6 “Susanna”O7

d

a

Page 6: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

6

Where do these mechanisms come from?

Class ANY in the Eiffel “Kernel Library”

Every class that doesn’t explicitly inherit from another is considered to inherit from ANY

As a result, every class is a descendant of ANY.

Page 7: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

7

Completing the inheritance structure

A B

D E

C

ANY

NONE

Inheritsfrom

Page 8: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

8

A related mechanism: Persistence

a.store (file)....b := retrieved (file)

Storage is automatic. Persistent objects identified individually by keys.

These features come from the library class STORABLE.

Needs to be improved,

see “ “object test”

Page 9: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

9

and now……to inheritance and genericity

Page 10: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

10

Extending the basic notion of class

LIST_OF_CARS

SET_OF_CARS

LINKED_LIST_OF_CARS

LIST_OF_CITIES

LIST_OF_PERSONS

Abstraction

Specialization

Type parameterization

Type parameterization

Genericity

Inheritance

Page 11: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

11

Extending the basic notion of class

LIST_OF_CARS

SET_OF_CARS

LINKED_LIST_OF_CARS

LIST_OF_CITIES

LIST_OF_PERSONS

LINKED_LIST_OF_CITIES

SET_OF_PERSONS

Genericity

Inheritance

Page 12: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

12

Genericity

UnconstrainedLIST [G]

e.g. LIST [INTEGER], LIST [PERSON]

Constrained HASH_TABLE [G ―> HASHABLE]VECTOR [G ―> NUMERIC ]

Page 13: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

13

Genericity: ensuring type safety

How can we define consistent “container” data structures, e.g. list of accounts, list of points? Dubious use of a container data structure:

c : CITY ; p : PERSONcities : LIST ... people : LIST ... ---------------------------------------------------------people.extend ( )cities.extend ( )

c := cities.last

c. some_city_operation

What if wrong?

pc

Page 14: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

14

Possible approaches1. Duplicate code, manually or with help of macro

processor.

2. Wait until run time; if types don’t match, trigger a run-time failure. (Smalltalk)

3. Convert (“cast”) all values to a universal type, such as “pointer to void” in C.

4. Parameterize the class, giving an explicit name G to the type of container elements. This is the Eiffel approach, now also found in Java, .NET and others.

Page 15: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

15

A generic class

class LIST [ G ] featureextend (x : G ) ...last : G ...

end

Formal generic parameter

Actual generic parameter

To use the class: obtain a generic derivation, e.g.

cities : LIST [ CITY ]

Page 16: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

16

Using generic derivationscities : LIST [CITY ]people : LIST [PERSON]c : CITYp : PERSON...

cities.extend (c)people.extend (p)

c := cities.lastc. some_city_operation

STATIC TYPINGThe compiler will reject:

people.extend (c) cities.extend (p)

Page 17: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

17

Static typing

Type-safe call (during execution):A feature call x.f such that the object attached to x has a feature corresponding to f.

[Generalizes to calls with arguments, x.f (a, b) ]

Static type checker:A program-processing tool (such as a compiler) that guarantees, for any program it accepts, that any call in any execution will be type-safe.

Statically typed language:A programming language for which it is possible to write a static type checker.

Page 18: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

18

Using genericityLIST [CITY ]LIST [LIST [CITY ]]…

A type is no longer exactly the same thing as a class!

(But every type remains based on a class.)

Page 19: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

19

A generic class

class LIST [ G ] featureextend (x : G ) ...last : G ...

end

Formal generic parameter

Actual generic parameter

To use the class: obtain a generic derivation, e.g.

cities : LIST [ CITY ]

Page 20: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

20

Extending the basic notion of class

LIST_OF_CARS

SET_OF_CARS

LINKED_LIST_OF_CARS

LIST_OF_CITIES

LIST_OF_PERSONS

Abstraction

Specialization

Type parameterization

Type parameterization

Genericity

Inheritance

Page 21: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

21

Reminder: the dual nature of classes

A class is a moduleA class is a type*

As a module, a class: Groups a set of related services Enforces information hiding (not all services are

visible from the outside) Has clients (the modules that use it) and

suppliers (the modules it uses)As a type, a class:

Denotes possible run-time values (objects & references), the instances of the type

Can be used for declarations of entities (representing such values)

*Or a type template (see genericity)

Page 22: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

22

Reminder: how the two views matchThe class, viewed as a module, groups a set of services

(the features of the class)which are precisely the operations applicable to instances of the class, viewed as a type.

(Example: class BUS, features stop, move, speed, passenger_count )

Page 23: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

23

Inheritance basics

Principle:Describe a new class as extension or specialization of an existing class

(or several with multiple inheritance)

If B inherits from A :

As modules: all the services of A are available in B

(possibly with a different implementation)

As types: whenever an instance of A is required, an instance of B will be acceptable

(“is-a” relationship)

Page 24: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

24

Terminology

Parent, HeirAncestor, Descendant

The ancestors of B are B itself and the ancestors of its parents.

Proper ancestor, Proper descendantDirect instance, Instance

The instances of A are the direct instances of its descendants.

(Other terminology: subclass, superclass, base class)

features

featuresB

A

Page 25: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

25

Terminology

If B inherits from A (by listing A in its inherit clause), B is an heir of A and A is a parent of B

The descendants of a class are the class itself and (recursively) the descendants of its heirs; proper descendants exclude the class itself

“Ancestor” and “proper ancestor” are the reverse notions

Page 26: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

26

Example hierarchy (from Traffic)

MOVING

VEHICLE

TAXI

EVENT_TAXI

LINE_VEHICLE

TRAM BUS

position

load

busy

take +

take *

update_coordinatesmove

update_coordinates ++

move ++

* Deferred+ Effective++ Redefined

*

*

**

Page 27: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

27

Features in the example

Feature

take (from_location, to_location : COORDINATE)

-- Bring passengers from from_location -- to to_location.

busy : BOOLEAN--Is taxi busy?

load (q : INTEGER)-- Load q passengers.

position : TRAFFIC_COORDINATE-- Current position on map

From class:

EVENT_TAXI

TAXI

VEHICLE

MOVING

Page 28: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

28

Inheriting features

classEVENT_TAXI

inheritTAXI

feature[… Rest of class …]

end

deferred classTAXI

inheritVEHICLE

feature[… Rest of class …]

end

All features of TAXI are applicable to instances of EVENT_TAXI

All features of VEHICLE are applicable to instances of TAXI

deferred classVEHICLE

inheritMOVING

feature[… Rest of class …]

end

All features of MOVING are applicable to instances of VEHICLE

Page 29: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

29

Example hierarchy

MOVING

VEHICLE

TAXI

EVENT_TAXI

LINE_VEHICLE

TRAM BUS

position

load

busy

take +

take *

update_coordinatesmove

update_coordinates ++

move ++

* deferred+ effective++ redefined

*

*

**

Page 30: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

30

Inherited featuresm : MOVING, v : VEHICLE, t : TAXI, e : EVENT_TAXI

v load (…)e take (…)m position -- An expressiont busy -- An expression

e load (…)e take (…)e position -- An expressione busy -- An expression

Page 31: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

31

Definitions: kinds of feature

A “feature of a class” is one of:

An inherited feature if it is a feature of one of the parents of the class.

An immediate feature if it is declared in the class, and not inherited. In this case the class is said to introduce the feature.

Page 32: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

32

Polymorphic assignment

v : VEHICLEcab : EVENT_TAXI

(VEHICLE)

(EVENT_TAXI)

v

cab

A proper descendant type of the

original

v := cab

Page 33: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

33

Assignments

Assignment:target := expression

With polymorphism:The type of expression is a descendant of thetype of target

So far (no polymorphism):expression was always of the same type as

target

Page 34: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

34

Polymorphism is also for argument passingregister_trip (v : VEHICLE )

do … end

Type of actual argument is proper descendant

of type of formal

A particular call:

register_trip ( cab )

Page 35: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

35

Definitions: Polymorphism

An attachment (assignment or argument passing) is polymorphic if its target variable and source expression have different types.

An entity or expression is polymorphic if it may at runtime — as a result of polymorphic attachments —become attached to objects of different types.

Polymorphism is the existence of these possibilities.

Page 36: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

36

Definitions (Static and dynamic type)

The static type of an entity is the type used in its declaration in the corresponding class text

If the value of the entity, during a particular execution, is attached to an object, the type of that object is the entity’s dynamic type at that time

Page 37: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

37

Static and dynamic type

v : VEHICLEcab : EVENT_TAXI

(VEHICLE)

(EVENT_TAXI)

v

cab

v := cab

Static type of v :VEHICLE

Dynamic type after this assignment:

EVENT_TAXI

Page 38: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

38

Basic type property

Static and dynamic type

The dynamic type of an entity will alwaysconform to its static type

(Ensured by the type system)

Page 39: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

39

Static typing

Type-safe call (during execution):A feature call x.f such that the object attached to x has a feature corresponding to f.

[Generalizes to calls with arguments, x.f (a, b) ]

Static type checker:A program-processing tool (such as a compiler) that guarantees, for any program it accepts, that any call in any execution will be type-safe.

Statically typed language:A programming language for which it is possible to write a static type checker.

Page 40: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

40

Inheritance and static typing

Basic inheritance type rule

For a polymorphic attachment to be valid,the type of the source must conform

to the type of the targetConformance: basic definition

Reference types (non-generic): U conforms to T if U is a descendant of T

An expanded type conforms only to itself

Page 41: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

41

Conformance: full definition

A reference type U conforms to a reference type T if either: They have no generic parameters, and U is

a descendant of T. They are both generic derivations with the

same number of actual generic parameters, the base class of U is a descendant of the base class of T, and every actual parameter of U (recursively) conforms to the corresponding actual parameter of T.

An expanded type conforms only to itself.

Page 42: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

42

Static typing (reminder)

Type-safe call (during execution):A feature call x.f such that the object attached to x has a feature corresponding to f.

[Generalizes to calls with arguments, x.f (a, b) ]

Static type checker:A program-processing tool (such as a compiler) that guarantees, for any program it accepts, that any call in any execution will be type-safe.

Statically typed language:A programming language for which it is possible to write a static type checker.

Page 43: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

43

Another example hierarchyFIGURE*

OPEN_ FIGURE

*CLOSED_ FIGURE

*

SEGMENT POLYLINE POLYGONELLIPSE

CIRCLE

RECTANGLETRIANGLE

SQUARE

center * display *rotate*

perimeter *

perimeter +

perimeter +

perimeter ++

diagonal

......

perimeter ++

+ +

side2

* deferred+ effective++ redefined

perimeter ++

side1

Page 44: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

44

Redefinition 1: polygonsclass POLYGON inherit

CLOSED_FIGUREcreate

makefeature

vertex : ARRAY [POINT]vertex_count : INTEGER perimeter : REAL

-- Perimeter lengthdo from ... until ... loop Result := Result + vertex [i ] . distance (vertex [i +

1]) ... end

endinvariant

vertex_count >= 3vertex_count = vertex.count

end

vertex [i ]

vertex [i + 1]

Page 45: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

45

Redefinition 2: rectanglesclass RECTANGLE inherit

POLYGONredefine

perimeter end

createmake

featurediagonal, side1, side2 : REALperimeter : REAL

-- Perimeter lengthdo Result := 2 * (side1 + side2 ) end

invariant

vertex_count = 4end

side1

side2diagonal

Page 46: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

46

Inheritance, typing and polymorphism

(POLYGON)

(RECTANGLE)

p

r

Assume: p : POLYGON ; r : RECTANGLE ; t : TRIANGLE x : REAL

Permitted: x := p.perimeterx := r.perimeterx := r.diagonalp := r

NOT permitted:

x := p.diagonal -- Even just after p := r !

r := p

Page 47: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

47

Dynamic bindingWhat is the effect of the following (if some_test is true)?

if some_test thenp := r

elsep := t

endx := p.perimeter

Redefinition: A class may change an inherited feature, as with POLYGON redefining perimeter.Polymorphism: p may have different forms at run-time.Dynamic binding: Effect of p.perimeter depends on run-time form of p.

Page 48: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

48

Without dynamic binding?

display (f : FIGURE )do

if “f is a CIRCLE” then ...

elseif “f is a POLYGON” then...

endend

and similarly for all other routines!

Tedious; must be changed whenever there’s a new figure type

Page 49: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

49

With inheritance and associated techniques

With:

Initialize:

and:

Then just use:

f : FIGUREc : CIRCLEp : POLYGON

create c.make (...)create p.make (...)

if ... then f := c

else f := p

end

f.move (...)f.rotate (...)f.display (...)

-- and so on for every-- operation on f !

Page 50: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

50

Inheritance: summary 1Type mechanism: lets you organize our data abstractions into taxonomies

Module mechanism: lets you build new classes as extensions of existing ones

Polymorphism: Flexibility with type safety

Dynamic binding: automatic adaptation of operation to target, for more modular software architectures

Page 51: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

51

Redefinitiondeferred class MOVING feature

origin : COORDINATEdestination : COORDINATEposition : COORDINATEpolycursor : LIST [COORDINATE]update_coordinates

-- Update origin and destination.do

[…]origin := destinationpolycursor.forthdestination := polycursor.item[…]

end[…]

end

polycursor.i_th (i)

polycursor.i_th (i + 1)

Page 52: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

52

Redefinition 2: LINE_VEHICLEdeferred class LINE_VEHICLE inherit

VEHICLEredefine update_coordinates end

featurelinecursor : LINE_CURSOR update_coordinates

-- Update origin and destination.do

[…]origin := destinationpolycursor.forthif polycursor.after then

linecursor.forthcreate polycursor.make (linecursor.item.polypoints)polycursor.start

enddestination := polycursor.item

end

polycursor.i_th(i)

polycursor.i_th (i + 1)

Page 53: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

53

Dynamic bindingWhat is the effect of the following (assuming some_test true)?m : MOVING, l : LINE_VEHICLE, t : TAXI

if some_test thenm := l

elsem := t

endm.update_coordinates

Redefinition: A class may change an inherited feature, as with LINE_VEHICLE redefining update_coordinates.Polymorphism: m may have different forms at run-time.

Dynamic binding: Effect of m.update_coordinates depends on run-time form of m

Page 54: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

54

Dynamic binding

*TAXI

EVENT_TAXI DISPATCHER_TAXI

busy

take

Inherits from

* Deferred

take*

take

There are multiple versions of take.

Page 55: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

55

Definitions (Dynamic binding)

Dynamic binding (a semantic rule) is the property that any execution of a feature call will use the version of the feature best adapted to the type of the target object.

Page 56: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

56

Extending the basic notion of class

LIST_OF_CARS

SET_OF_CARS

LINKED_LIST_OF_CARS

LIST_OF_CITIES

LIST_OF_PERSONS

Abstraction

Specialization

Type parameterization

Type parameterization

Genericity

Inheritance

Page 57: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

57

Extending the basic notion of class

LIST_OF_CARS

SET_OF_CARS

LINKED_LIST_OF_CARS

LIST_OF_CITIES

LIST_OF_PERSONS

LINKED_LIST_OF_CITIES

SET_OF_PERSONS

Genericity

Inheritance

Page 58: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

58

ConformanceDefined earlier for non-generically derived types:

Page 59: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

59

Polymorphic data structures

fleet: LIST [VEHICLE]v: VEHICLE

extend (v : G) -- Add a new occurrence of v.

…fleet.extend (v)fleet.extend (cab)

(TAXI) (TAXI)(TRAM) (TRAM) (BUS)

Page 60: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

60

Definition (Polymorphism, adapted)

An attachment (assignment or argument passing) is polymorphic if its target entity and source expression have different types.

An entity or expression is polymorphic if – as a result of polymorphic attachments – it may at runtime become attached to objects of different types.

A container data structure is polymorphic if it may contain references to objects of different types.

Polymorphism is the existence of these possibilities.

Page 61: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

61

What we have seen

The basics of fundamental O-O mechanisms: Inheritance Polymorphism Dynamic binding Static typing Genericity

Page 62: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

62

Our program for the second partReminder on genericity, including constrained

Inheritance: deferred classesInheritance: what happens to contracts?

Inheritance: how do we find the actual type of an object?

Still to see about inheritance after this lecture: multiple inheritance, and various games such as renaming

Page 63: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

63

Genericity (reminder)

UnconstrainedLIST [G]

e.g. LIST [INTEGER], LIST [PERSON]

Constrained HASH_TABLE [G ―> HASHABLE]VECTOR [G ―> NUMERIC ]

Page 64: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

64

A generic class (reminder)

class LIST [ G ] featureextend (x : G ) ...last : G ...

end

Formal generic parameter

Actual generic parameter

To use the class: obtain a generic derivation, e.g.

cities : LIST [ CITY ]

Page 65: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

65

Using generic derivations (reminder)cities : LIST [CITY ]people : LIST [PERSON]c : CITYp : PERSON...

cities.extend (c)people.extend (p)

c := cities.lastc. some_city_operation

STATIC TYPINGThe compiler will reject:

people.extend (c) cities.extend (p)

Page 66: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

66

Genericity: summary 1Type extension mechanism

Reconciles flexibility with type safety

Enables us to have parameterized classes

Useful for container data structures: lists, arrays, trees, …

“Type” now a bit more general than “class”

Page 67: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

67

Definition: TypeWe use types to declare entities, as in

x : SOME_TYPE

With the mechanisms defined so far, a type is one of:

A non-generic classe.g. METRO_STATION

A generic derivation, i.e. the name of a class followed by a list of types, the actual generic parameters, in brackets

e.g. LIST [METRO_STATION ]LIST [ARRAY

[METRO_STATION ]]

Page 68: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

68

Combining genericity with inheritance

LIST_OF_CARS

SET_OF_CARS

LINKED_LIST_OF_CARS

LIST_OF_CITIES

LIST_OF_PERSONS

Abstraction

Specialization

Type parameterization

Type parameterization

Genericity

Inheritance

Page 69: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

69

Genericity + inheritance 1: Constrained genericity

class VECTOR [G ] feature plus alias "+" (other : VECTOR [G]): VECTOR [G]

-- Sum of current vector and otherrequire

lower = other.lowerupper = other.upper

locala, b, c: G

do... See next ...

end... Other features ...

end

Page 70: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

70

Adding two vectors

i a b c=+

+ =u v w

12

Page 71: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

71

Constrained genericity

Body of plus alias "+":

create Result.make (lower, upper)from

i := lower until

i > upper loop

a := item (i )b := other.item (i )c := a + b -- Requires “+” operation on G!

Result.put (c, i )i := i + 1

end

Page 72: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

72

The solutionDeclare class VECTOR as

class VECTOR [G –> NUMERIC ] feature... The rest as before ...

end

Class NUMERIC (from the Kernel Library) provides features plus alias "+", minus alias "-"and so on.

Page 73: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

73

Improving the solution

Make VECTOR itself a descendant of NUMERIC,effecting the corresponding features:

class VECTOR [G –> NUMERIC ] inheritNUMERIC

feature... Rest as before, including infix "+"...

endThen it is possible to define

v : VECTOR [INTEGER ]vv : VECTOR [VECTOR [INTEGER ]]vvv : VECTOR [VECTOR [VECTOR [INTEGER ]]]

Page 74: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

74

Combining genericity with inheritance

LIST_OF_CARS

SET_OF_CARS

LINKED_LIST_OF_CARS

LIST_OF_CITIES

LIST_OF_PERSONS

Abstraction

Specialization

Type parameterization

Type parameterization

Genericity

Inheritance

Page 75: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

75

Genericity + inheritance 2: Polymorphic data structures

figs : LIST [FIGURE ]p1, p2 : POLYGONc1, c2 : CIRCLEe : ELLIPSE

(POLYGON) (CIRCLE) (POLYGON)(CIRCLE) (ELLIPSE)

class LIST [G ] featureextend (v : G) do … endlast : G…

end

figs.extend (p1 ) ; figs.extend (c1 ) ; figs.extend (c2 )figs.extend (e ) ; figs.extend (p2 )

Page 76: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

76

Example hierarchyFIGURE*

OPEN_ FIGURE

*CLOSED_ FIGURE

*

SEGMENT POLYLINE POLYGONELLIPSE

CIRCLE

RECTANGLETRIANGLE

SQUARE

center * display *rotate*

perimeter *

perimeter +

perimeter +

perimeter ++

diagonal

......

perimeter ++

+ +

side2

* deferred+ effective++ redefined

perimeter ++

side1

Page 77: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

77

Another application: undoing-redoingThis example again uses a powerful polymorphic data structureThis will only be a sketch; we’ll come back to the details in the agent lecture

References:

Chapter 21 of my Object-Oriented Software Construction, Prentice Hall, 1997

Erich Gamma et al., Design Patterns, Addison –Wesley, 1995: “Command pattern”

Page 78: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

78

The problemEnabling users of an interactive system to cancel the effect of the last command

Often implemented as “Control-Z”

Should support multi-level undo-redo (“Control-Y”), with no limitation other than a possible maximum set by the user

Page 79: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

79

Our working example: a text editorNotion of “current line”.Assume commands such as:

Remove current line Replace current line by specified text Insert line before current position Swap current line with next if any “Global search and replace” (hereafter GSR):

replace every occurrence of a specified string by another

...

This is a line-oriented view for simplicity, but the discussion applies to more sophisticated views

Page 80: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

80

A straightforward solution

Before performing any operation, save entire state

In the example: text being edited,current position in text

If user issues “Undo” request, restore entire state as last saved

But: huge waste of resources, space in particular

Intuition: only save the “diff” between states.

Page 81: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

81

Keeping the history of the sessionThe history list:

history : TWO_WAY_LIST [COMMAND]

Removal

SwapInsertion

Insertion

Insertion

Oldest Most recent

Page 82: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

82

What’s a “command” object?

A command object includes information about one execution of a command by the user, sufficient to:

Execute the command Cancel the command if requested later

For example, in a Removal command object, we need:• The position of the line being removed• The content of that line!

Page 83: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

83

General notion of command

deferred

class

COMMAND feature

execute -- Carry out one execution of this

command.

undo -- Cancel an earlier execution of this command.

end

deferred: doneend

deferredend

done: BOOLEAN -- Has this command been executed?

ensure already: done

require already: done

Page 84: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

84

Command class hierarchy

execute*

undo*

execute+

undo+

line: STRINGindex: INTEGER...

execute+

undo+

index...

+* deferr

edeffective

*COMMAND

+

REMOVAL

+

INSERTION

Page 85: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

85

Underlying class (from business model)

class EDIT_CONTROLLER featuretext : TWO_WAY_LIST [STRING]remove

-- Remove line at current position.require

not offdo

text.removeend

put_right (line : STRING)-- Insert line after current position.

requirenot after

dotext.put_right (line)

end... also item, index, go_ith, put_left ...

end

Page 86: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

86

A command class (sketch, no contracts)

class REMOVAL inherit COMMAND featurecontroller : EDIT_CONTROLLER

-- Access to business modelline : STRING

-- Line being removedindex : INTEGER

-- Position of line being removedexecute

-- Remove current line and remember it.do line := controller.item ; index := controller.index

controller.remove ; done := Trueendundo

-- Re-insert previously removed line.do controller.go_i_th (index)

controller.put_left (line)endend

Page 87: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

87

A polymorphic data structure:

history : TWO_WAY_LIST [COMMAND]

Removal

SwapInsertion

Insertion

Insertion

Oldest Most recent

The history list

Page 88: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

88

Reminder: the list of figuresclass

LIST [G] feature

...last: G do ...extend (x: G) do ...

end

fl : LIST [FIGURE]r : RECTANGLEs : SQUAREt : TRIANGLEp : POLYGON...fl.extend (p); fl.extend (t); fl.extend (s); fl.extend (r)fl.last.display

(SQUARE)

(RECTANGLE)

(TRIANGLE)

(POLYGON)

fl

Page 89: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

89

Reminder: the list of figures

figs : LIST [FIGURE ]p1, p2 : POLYGONc1, c2 : CIRCLEe : ELLIPSE

class LIST [G ] featureextend (v : G) do … endlast : G…

end

figs.extend (p1 ) ; figs.extend (c1 ) ; figs.extend (c2 )figs.extend (e ) ; figs.extend (p2 )

(POLYGON) (CIRCLE) (POLYGON)(CIRCLE) (ELLIPSE)

Page 90: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

90

A polymorphic data structure:

history : TWO_WAY_LIST [COMMAND]

Removal

SwapInsertion

Insertion

Insertion

Oldest Most recent

The history list

Page 91: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

91

Executing a user commanddecode_user_request

if “Request is normal command” then“Create command object c corresponding to user request”history.extend (c)c.execute

elseif “Request is UNDO” thenif not history.before then -- Ignore excessive requests history.item.undo history.backend

elseif “Request is REDO” thenif not history.is_last then -- Ignore excessive requests history.forth history. item.execute end

end

item

Pseudocode, see implementation next

Removal

SwapInsertion

Insertion

Page 92: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

92

Command class hierarchy

execute*

undo*

execute+

undo+

line: STRINGindex: INTEGER...

execute+

undo+

index...

+* deferr

edeffective

*COMMAND

+

REMOVAL

+

INSERTION

Page 93: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

93

Example hierarchyFIGURE*

OPEN_ FIGURE

*CLOSED_ FIGURE

*

SEGMENT POLYLINE POLYGONELLIPSE

CIRCLE

RECTANGLETRIANGLE

SQUARE

center * display *rotate*

perimeter *

perimeter +

perimeter +

perimeter ++

diagonal

......

perimeter ++

+ +

side2

* deferred+ effective++ redefined

perimeter ++

side1

Page 94: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

94

Enforcing a type: the problem

fl.store ("FILE_NAME")...

-- Two years later:fl := retrieved ("FILE_NAME") -- See nextx := fl.last -- [1]print (x.diagonal ) -- [2]

What’s wrong with this?

If x is declared of type RECTANGLE, [1] is invalid.If x is declared of type FIGURE, [2] is invalid.

Page 95: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

95

Object-Test Local

Enforcing a type: the Object Test

if {r : RECTANGLE } fl.retrieved ("FILE_NAME") then

print (r.diagonal )… Do anything else with r, guaranteed… to be non void and of dynamic type

RECTANGLEelse

print ("Too bad.")end

Expression to be tested

SCOPE of the Object-Test Local

Page 96: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

96

Earlier mechanism: assignment attemptf : FIGUREr : RECTANGLE...fl.retrieve ("FILE_NAME")f := fl.last

r ?= f

if r /= Void thenprint (r.diagonal )

elseprint ("Too bad.")

end

Page 97: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

97

Assignment attemptx ?= y

withx : A

Semantics: If y is attached to an object whose type conforms

to A, perform normal reference assignment.

Otherwise, make x void.

Page 98: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

98

The role of deferred classesExpress abstract concepts independently of implementation

Express common elements of various implementations

Terminology: Effective = non-deferred(i.e. fully implemented)

Page 99: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

99

A deferred feature

In e.g. LIST:

forth

deferred

end

require not

after

ensure index = old index + 1

Page 100: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

100

Deferred!

In the same class

search (x : G)-- Move to first position after current

-- where x appears, or after if none.

do from until after or else item = x loop

forth endend

“Programs with holes”

Mixing deferred and effective features

Effective!

Page 101: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

101

“Don’t call us, we’ll call you!”A powerful form of reuse:

The reusable element defines a general scheme Specific cases fill in the holes in that scheme

Combine reuse with adaptation

Page 102: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

102

Applications of deferred classesAnalysis and design, top-down

Taxonomy

Capturing common behaviors

Page 103: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

103

Deferred classes in EiffelBase

*: deferred

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*

… …

Page 104: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

104

Java and .NET solutionSingle inheritance only for classesMultiple inheritance from interfaces

An interface is like a fully deferred class, with no implementations (do clauses), no attributes (and also no contracts)

Page 105: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

105

Applications of deferred classesAbstraction

Taxonomy

High-level analysis and design

Page 106: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

106

Television station example

class SCHEDULE featuresegments : LIST [SEGMENT]

end

Source: Object-Oriented Software Construction, 2nd edition, Prentice

Hall

Page 107: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

107

Schedules

notedescription :“ 24-hour TV schedules”

deferred class SCHEDULE feature

segments : LIST [SEGMENT ] -- Successive segmentsdeferred

end

air_time : DATE -- 24-hour period -- for this scheduledeferredend

set_air_time (t : DATE) -- Assign schedule to -- be broadcast at

time t.require t.in_futuredeferredensure air_time = tend

print -- Produce paper

version.deferredend

end

Page 108: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

108

Segmentnote

description : "Individual fragments of a schedule "deferred class SEGMENT feature

schedule : SCHEDULE deferred end

-- Schedule to which-- segment belongsindex : INTEGER deferred

end-- Position of segment in-- its schedulestarting_time, ending_time :

INTEGER deferred end-- Beginning and end of-- scheduled air timenext: SEGMENT deferred end-- Segment to be played-- next, if any

sponsor : COMPANY deferred end-- Segment’s principal sponsor

rating : INTEGER deferred end-- Segment’s rating (for-- children’s viewing etc.)

Commands such as change_next, set_sponsor, set_rating, omitted

Minimum_duration : INTEGER = 30-- Minimum length of segments,-- in seconds

Maximum_interval : INTEGER = 2-- Maximum time between two-- successive segments, in seconds

Page 109: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

109

Segment (continued)

invariantin_list: (1 <= index) and (index <= schedule.segments.count)in_schedule: schedule.segments.item (index) = Currentnext_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 >= 0positive_times: (starting_time > 0 ) and (ending_time > 0)sufficient_duration:

ending_time – starting_time >= Minimum_durationdecent_interval :

(next.starting_time) - ending_time <= Maximum_intervalend

Page 110: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

110

Commercialnote

description: "Advertizing segment "

deferred class COMMERCIAL inherit

SEGMENT rename sponsor as advertizer

end

featureprimary : PROGRAM deferred

-- Program to which this-- commercial is attached

primary_index : INTEGER deferred-- Index of primary

set_primary (p : PROGRAM)-- Attach commercial to p.

requireprogram_exists: p /= Voidsame_schedule:

p.schedule = schedule

before:p.starting_time <= starting_time

deferredensure

index_updated: primary_index =

p.indexprimary_updated: primary = p

end

Page 111: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

111

Commercial (continued)

invariantmeaningful_primary_index: primary_index = primary.indexprimary_before: primary.starting_time <= starting_timeacceptable_sponsor: advertizer.compatible (primary.sponsor)acceptable_rating: rating <= primary.rating

end

Page 112: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

112

deferred classVAT

inheritTANK

featurein_valve, out_valve : VALVE

-- Fill the vat.require

in_valve.openout_valve.closed

deferredensurein_valve.closedout_valve.closedis_full

endempty, is_full, is_empty, gauge, maximum, ... [Other features] ...

invariantis_full = (gauge >= 0.97 * maximum)  and  (gauge <= 1.03 *

maximum)end

Chemical plant example

Page 113: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

113

Contracts and inheritanceIssue: what happens, under inheritance, to

Class invariants?

Routine preconditions and postconditions?

Page 114: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

114

InvariantsInvariant Inheritance rule:

The invariant of a class automatically includes the invariant clauses from all its parents, “and”-ed.

Accumulated result visible in flat and interface forms.

Page 115: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

115

Contracts and inheritance

rrequire

ensure

rrequire

ensure

a1 : Aa1.r (…)…

Correct call in C: if a1. then a1.r (...) -- Here a1. holds end

r ++

C A

D B

Client Inheritance++ Redefinition

Page 116: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

116

Assertion redeclaration ruleWhen redeclaring a routine, we may only:

Keep or weaken the precondition

Keep or strengthen the postcondition

Page 117: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

117

A simple language rule does the trick!

Redefined version may have nothing (assertions kept by default), or

require else new_preensure then new_post

Resulting assertions are: original_precondition or new_pre

original_postcondition and new_post

Assertion redeclaration rule in Eiffel

Page 118: Einführung  in die  Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer

118

What we have seenDeferred classes and their role in software analysis and design

Contracts and inheritance

Finding out the “real” type of an object