Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10....

22
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008

Transcript of Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10....

Page 1: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

Chair of Software Engineering

1

Introduction to Programming

Bertrand Meyer

Exercise Session 13

10. November 2008

Page 2: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

2

Chair of Software Engineering

This week

Inheritance: Deferred classes & features Redefinition, renaming Observer Pattern

Genericity Hint for Assignment

Page 3: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

3

Chair of Software Engineering

Let's play Lego!

BRICK

LEGO_BRICK

LEGO_BRICK_WITH_HOLE LEGO_BRICK_SLANTED

Inheritance Relation

of Lego bricks

Page 4: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

4

Chair of Software Engineering

class BRICK

deferred class

BRICK

feature -- Access

width: INTEGER

depth: INTEGER

height: INTEGER

color: COLOR

volume: INTEGER

deferred

end

end

Page 5: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

5

Chair of Software Engineering

Class LEGO_BRICK

class LEGO_BRICK

inherit BRICK

feature -- Access

number_of_nubs: INTEGER do Result := ... end volume: INTEGER do Result := ... endend

Inherit all features of class BRICK

New feature, calculateall nubs

Implementation of`volume'

Page 6: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

6

Chair of Software Engineering

Class LEGO_BRICK_SLANTED

Feature `volume' is going to be redefined (=changed).

`volume' comes from LEGO_BRICK

class LEGO_BRICK_SLANTED

inherit LEGO_BRICK redefine volume end

feature -- Access

volume: INTEGER do Result := ... end end

Page 7: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

7

Chair of Software Engineering

Class LEGO_BRICK_WITH_HOLE

class LEGO_BRICK_WITH_HOLE

inherit LEGO_BRICK redefine volume end

feature -- Access

volume: INTEGER do Result := ... end end

Feature `volume' is going to be redefined (=changed).

`volume' comes from LEGO_BRICK

Page 8: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

8

Chair of Software Engineering

Notation

volume++

Notation:

Deferred *

Effective +

Redefinition ++

BRICK

LEGO_BRICK

LEGO_BRICK_WITH_HOLE LEGO_BRICK_SLANTED

+

++

volume*

volume+

*

volume++

Page 9: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

9

Chair of Software Engineering

Deferred & effective

Deferred Deferred classes can possess deferred features A class with at least one deferred feature must be declared

as deferred A deferred feature does not have an implementation yet Attributes can't be deferred (because they don't need a

feature body anyways) Deferred classes cannot be instantiated and hence cannot

contain a create clause

Effective Effective classes do not possess deferred features (the

“standard case”) Effective features have an implementation of their feature

body (“do .. end”)

Page 10: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

10

Chair of Software Engineering

Precursor

If a feature was redefined, but you still wish to call the old one, use the precursor keyword

volume: INTEGER do Result := Precursor - ... end

Page 11: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

11

Chair of Software Engineering

Types in the source code

Expressions have a static type

For attributes and functions it is denoted after the declaration ':'

name: STRING

list: LINKED_LIST [INTEGER]

A class is the static part of a program, which contains only static types

Page 12: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

12

Chair of Software Engineering

Type of an object

The type of an object is set upon creation

local

lego: LEGO_BRICK

brick: BRICK

create {LEGO_BRICK} brick -- explicit

-- same as

--create lego; brick := lego

create lego -- implicit Objects belong to the dynamic part of a program -

>dynamic types

Page 13: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

13

Chair of Software Engineering

Dynamic types

local

a_bag: SOME_BAG

do

create {MONEY_BAG} a_bag.put (10)

What is the static type of `a_bag'?

What's the dynamic type of the object denoted by `a_bag'?

Hands-On

SOME_BAG

MONEY_BAG

Page 14: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

Chair of Software Engineering

The Observer Pattern

PUBLISHER*

GUI_CLASS

subscribedattachdetachtrigger

OBSERVER*

APP_CLASS

Deferred (abstract)

Effective (implemented)

*+

Inherits fromClient (uses)

update*subscribe

update+

Page 15: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

Chair of Software Engineering

Observer pattern

Publisher keeps a list of observers:

subscribed: LINKED_LIST [OBSERVER]

To register itself, an observer may execute

subscribe (some_publisher)

where subscribe is defined in OBSERVER:

subscribe (p: PUBLISHER)-- Make current object observe p.

requirepublisher_exists: p /= Void

dop.attach (Current)

end

Page 16: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

Chair of Software Engineering

Attaching an observer

In class PUBLISHER:

attach (s: OBSERVER)-- Register s as subscriber to current publisher.

requiresubscriber_exists: s /= Void

do

subscribed.extend (s)

end

The invariant of PUBLISHER includes the clause

subscribed /= Void

(List subscribed is created by creation procedures of PUBLISHER)

Page 17: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

Chair of Software Engineering

trigger -- Ask all observers to -- react to current event.

do from subscribed.start until subscribed.after loop subscribed.item. subscribed.forth end

end

Each descendant of OBSERVER defines its own versionof update

Triggering an event

update

PUBLISHER*

GUI_CLASS

attachdetach

OBSERVER*

APP_CLASS

update*

update+

Page 18: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

Chair of Software Engineering

Changing the implementation of the pattern

How would the implementation of the Observer pattern change if:

each observer subscribes not itself, but one of its routines to the publisher

when the event occurs, the publisher must call these subscribed routines with information about the event

Hands-On

Page 19: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

Chair of Software Engineering

Exercise: a news agency

Consider you must create an application which allows a news agency to dispatch news to various papers, radio and TV stations, etc.

How would you design and implement this application?

Hands-On

Page 20: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

Chair of Software Engineering

Genericity

But what if a certain box should only contain Lego bricks, i.e. all kinds of them (slanted, 2x4, etc.)?

classBOX [G]

featurewipe_out is

...

put (v: G) is...

end

Page 21: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

Chair of Software Engineering

For Assignment 7

Vector Geometry

21

Page 22: Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008.

Chair of Software Engineering

End exercise session 15