Object-Oriented Software Construction

32
OOSC - Summer Semester 2004 1 Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer

description

Object-Oriented Software Construction. Bertrand Meyer. Lecture 19: Agents and event-driven design. Agenda for today. Scope of this development Applications The mechanism. Scope. Starting from an object-oriented basis, add a new kind of objects representing potential computations. - PowerPoint PPT Presentation

Transcript of Object-Oriented Software Construction

Page 1: Object-Oriented Software Construction

OOSC - Summer Semester 2004

1

Chair of Software Engineering

Object-Oriented Software Construction

Bertrand Meyer

Page 2: Object-Oriented Software Construction

OOSC - Summer Semester 2004

2

Chair of Software Engineering

Lecture 19:

Agents and event-driven design

Page 3: Object-Oriented Software Construction

OOSC - Summer Semester 2004

3

Chair of Software Engineering

Agenda for today

Scope of this development Applications The mechanism

Page 4: Object-Oriented Software Construction

OOSC - Summer Semester 2004

4

Chair of Software Engineering

Scope

Starting from an object-oriented basis, add a new kind of objects representing potential computations.

Such objects are called “agents”.

Earlier names: Delayed calls Routine objects

Similar to: “Closures” Delegates (C#) Blocks (Smalltalk) Lambda expressions

Page 5: Object-Oriented Software Construction

OOSC - Summer Semester 2004

5

Chair of Software Engineering

Compare to…

... “Functional” style of programming, e.g. Haskell

Conjecture: Haskell should be an Eiffel library (Eifskell?)

Page 6: Object-Oriented Software Construction

OOSC - Summer Semester 2004

6

Chair of Software Engineering

Traditional input scheme

fromopen_file

until end_of_file

loop read_next process (last_item)

end

Page 7: Object-Oriented Software Construction

OOSC - Summer Semester 2004

7

Chair of Software Engineering

The starting idea of object-technology

Organize software architecture around data types.

Agents: Can an object represent an action?

Processor

ObjectAction

Page 8: Object-Oriented Software Construction

OOSC - Summer Semester 2004

8

Chair of Software Engineering

Event-driven programming

PUBLISHERS SUBSCRIBERS

trigger events handle events

EVENTS

ROUTINE

ROUTINE

ROUTINE

Page 9: Object-Oriented Software Construction

OOSC - Summer Semester 2004

9

Chair of Software Engineering

Applications of agents

Iteration High-level contracts Numerical programming Introspection High-level functionals, type-safe

Page 10: Object-Oriented Software Construction

OOSC - Summer Semester 2004

10

Chair of Software Engineering

Integration example (1)

b

my_function (x) dx

a

my_integrator.integral (agent my_function, a, b)

Page 11: Object-Oriented Software Construction

OOSC - Summer Semester 2004

11

Chair of Software Engineering

b

your_function (x, u, v) dxa

my_integrator.integral (agent your_function (?, u, v), a, b)

In the first example (one argument), the notation

agent my_function

is a synonym for

agent my_function (?)

Integration example (2)

Page 12: Object-Oriented Software Construction

OOSC - Summer Semester 2004

12

Chair of Software Engineering

Open and closed arguments

agent your_function (?, u, v)

Closed: set at the time of the agent’s definition

Open: set at the time of any call to the agent

Closed

Open

Page 13: Object-Oriented Software Construction

OOSC - Summer Semester 2004

13

Chair of Software Engineering

Using a routine from another class

agent some_object.some_routine (?, u, v)

Target

Page 14: Object-Oriented Software Construction

OOSC - Summer Semester 2004

14

Chair of Software Engineering

Iteration

Consider

my_integer_list: LIST [INTEGER]

in a class C that has the function

is_positive (x: INTEGER): BOOLEAN is-- Is x positive?

doResult := (x > 0)

end

To test that all integers in a list are positive:

all_positive := my_integer_list.for_all (agent is_positive)

Page 15: Object-Oriented Software Construction

OOSC - Summer Semester 2004

15

Chair of Software Engineering

Iteration (cont’d)

Consider

my_employee_list: LIST [EMPLOYEE]

where class EMPLOYEE has the feature

is_married: BOOLEAN-- Does this object represent a-- married employee?

To test that all employees in a list are married:

all_married :=

my_employee_list.for_all (agent {EMPLOYEE}.is_married)

Page 16: Object-Oriented Software Construction

OOSC - Summer Semester 2004

16

Chair of Software Engineering

Target or argument open

Compare the two examples (both in a class C):

my_integer_list: LIST [INTEGER]my_employee_list: LIST [EMPLOYEE]is_positive (x: INTEGER): BOOLEAN -- In class Cis_married: BOOLEAN -- In class EMPLOYEE

-- Abbreviated as-- my_integer_list.for_all (agent is_positive):

my_integer_list.for_all (agent is_positive (?))

my_employee_list.for_all (agent {EMPLOYEE}.is_married)

Open

Page 17: Object-Oriented Software Construction

OOSC - Summer Semester 2004

17

Chair of Software Engineering

An EiffelBase contract (class HASH_TABLE)

extend (new: G; key: H) -- Assuming there is no item of key

key,-- insert new with key; set inserted.

requirenot_key_present: not has (key)

ensureinsertion_done: item (key) = newkey_present: has (key)inserted: insertedone_more: count = old count + 1

Page 18: Object-Oriented Software Construction

OOSC - Summer Semester 2004

18

Chair of Software Engineering

Agents’ potential for contracts

Express general properties such as “none of the elements from positions 1 to count – 1 have been changed”.

Page 19: Object-Oriented Software Construction

OOSC - Summer Semester 2004

19

Chair of Software Engineering

Event-driven programming

PUBLISHERS SUBSCRIBERS

trigger events handle events

EVENTS

ROUTINE

ROUTINE

ROUTINE

Page 20: Object-Oriented Software Construction

OOSC - Summer Semester 2004

20

Chair of Software Engineering

Event Library

Class EVENT_TYPE

Publisher side, e.g. GUI library:

(Once) declare event type:

click: EVENT_TYPE [TUPLE [INTEGER, INTEGER]]

(Once) create event type object:

create click

Each time the event occurs:

click.publish ([x_coordinate, y_coordinate])

Subscriber side:

click.subscribe (agent my_procedure)

Page 21: Object-Oriented Software Construction

OOSC - Summer Semester 2004

21

Chair of Software Engineering

Subscriber variants

click.subscribe (agent my_procedure)

my_button.click.subscribe (agent my_procedure)

click.subscribe (agent your_procedure (a, ?, ?, b))

click.subscribe (agent other_object.other_procedure)

Page 22: Object-Oriented Software Construction

OOSC - Summer Semester 2004

22

Chair of Software Engineering

EiffelVision style

my_button.click.action_list.extend (agent my_procedure)

Page 23: Object-Oriented Software Construction

OOSC - Summer Semester 2004

23

Chair of Software Engineering

Observer pattern (C++, Java)

SUBSCRIBER*

PUBLISHER*

APPCLASSLIBCLASS

attachdetach

update*

update+

Deferred (abstract)

Effective (implemented)

*+

Inherits fromClient (uses)

Page 24: Object-Oriented Software Construction

OOSC - Summer Semester 2004

24

Chair of Software Engineering

Observer pattern

Publishers know about subscribers Subscriber may subscribe to at most one publisher May subscribe at most one operation Not reusable — must be coded anew for each

application

Page 25: Object-Oriented Software Construction

OOSC - Summer Semester 2004

25

Chair of Software Engineering

Event library

Publisher, e.g. GUI library:

Declare and create:

click: EVENT_TYPE [TUPLE [INTEGER, INTEGER]]

Trigger each event with arguments. click.publish ([x, y])

Subscriber (to subscribe a routine r):

my_button.click.subscribe (agent r)

Page 26: Object-Oriented Software Construction

OOSC - Summer Semester 2004

26

Chair of Software Engineering

.NET event-delegate mechanism

Publisher or subscriber:

Introduce descendant ClickArgs of EventArgs repeating types of arguments of myProcedure. (Adds a class.)

public class ClickArgs{

int x, y;...}

Declare delegate type ClickDelegate based on that class. (Adds a type.)

public void delegate ClickDelegate

(Object sender, ClickArgs e);

D1

D2

Page 27: Object-Oriented Software Construction

OOSC - Summer Semester 2004

27

Chair of Software Engineering

Declare new event type Click based on the type ClickDelegate. (Adds a type.)

public event ClickDelegate Click;

Write procedure OnClick to wrap handling. (Adds a routine.)

protected void OnClick (ClickArgs e){

if (Click != null)Click (this, e);

}

For every event occurrence, create instance of ClickArgs, passing arg values to constructor. (Adds a run-time object.)

ClickArgs myClickArgs = new ClickArgs (h, v);

For every occurrence, trigger event

OnClick (myClickArgs);

.NET delegates (2): publisher

D3

D4

D5

D6

Page 28: Object-Oriented Software Construction

OOSC - Summer Semester 2004

28

Chair of Software Engineering

.NET delegates (3): subscriber

To subscribe a routine myProcedure:

Declare a delegate myDelegate of type ClickDelegate. (Can be combined with following step as shown next.)

Instantiate it with myProcedure as constructor’s argument.

ClickDelegate myDelegate = new ClickDelegate (myProcedure)

Add it to the delegate list for the event.

yourButton.Click += myDelegate

D7

D8

D9

Page 29: Object-Oriented Software Construction

OOSC - Summer Semester 2004

29

Chair of Software Engineering

.NET delegates (4)

event is a keyword of the language (special features of a class). But event types should be treated as ordinary objects.

Cannot have closed arguments: for equivalent ofr (a, ?, ?, b)

must write routine wrapper to be used for delegate.

Cannot have open target: for equivalent of{TYPE}.r (...)

must write routine wrapper.

Page 30: Object-Oriented Software Construction

OOSC - Summer Semester 2004

30

Chair of Software Engineering

Lessons

Avoid magic: what’s available to the language designer should be available to the programmer

Role of language mechanisms: genericity, constrained genericity, tuples

Importance of choosing the right abstractions Observer Pattern: PUBLISHER, SUBSCRIBER .NET: event, delegate, event type, delegate

type? Eiffel Event Library: EVENT_TYPE

Page 31: Object-Oriented Software Construction

OOSC - Summer Semester 2004

31

Chair of Software Engineering

Observer pattern (C++, Java)

SUBSCRIBER*

PUBLISHER*

APPCLASS LIBCLASS

attachdetach

update*

update+

Deferred (abstract)

Effective (implemented)

*+

Inherits fromClient (uses)

Page 32: Object-Oriented Software Construction

OOSC - Summer Semester 2004

32

Chair of Software Engineering

End of lecture 19