By: Thu-Anh Le, Lily Han & Samantha Haber
Dec 21, 2015
ATOT - Lecture 20, 11 June 2003
1
Chair of Software Engineering
Advanced Topics in Object Technology
Bertrand Meyer
ATOT - Lecture 20, 11 June 2003
2
Chair of Software Engineering
Lecture 20:
Agents and event-driven design
ATOT - Lecture 20, 11 June 2003
3
Chair of Software Engineering
Agenda for today
Scope of this development Applications The mechanism
ATOT - Lecture 20, 11 June 2003
5
Chair of Software Engineering
Agenda for today
Scope of this development Applications The mechanism
ATOT - Lecture 20, 11 June 2003
6
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
ATOT - Lecture 20, 11 June 2003
7
Chair of Software Engineering
Compare to…
... “Functional” style of programming, e.g. Haskell
Conjecture: Haskell should be an Eiffel library (Eifskell?)
ATOT - Lecture 20, 11 June 2003
8
Chair of Software Engineering
Extensions (Eiffel: The Language, 1991)
“There is one and only one kind of acceptable language extension: the one that dawns on you with the sudden self-evidence of morning mist. It must provide a complete solution to a real problem, but usually that is not enough: almost all good extensions solve several potential problems at once, through a simple addition. It must be simple, elegant, explainable to any competent user of the language in a minute or two. It must fit perfectly within the spirit and letter of the rest of the language. It must not have any dark sides or raise any unanswerable questions. And because software engineering is engineering, you must see the implementation technique.”
ATOT - Lecture 20, 11 June 2003
9
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
ATOT - Lecture 20, 11 June 2003
10
Chair of Software Engineering
Agenda for today
Scope of this development Applications The mechanism
ATOT - Lecture 20, 11 June 2003
11
Chair of Software Engineering
Applications of agents
Iteration High-level contracts Numerical programming Introspection High-level functionals, type-safe
ATOT - Lecture 20, 11 June 2003
12
Chair of Software Engineering
Integration example (1)
b
my_function (x) dx
a
my_integrator.integral (agent my_function, a, b)
ATOT - Lecture 20, 11 June 2003
13
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)
ATOT - Lecture 20, 11 June 2003
14
Chair of Software Engineering
Agenda for today
Scope of this development Applications The mechanism
ATOT - Lecture 20, 11 June 2003
15
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
ATOT - Lecture 20, 11 June 2003
16
Chair of Software Engineering
Using a routine from another class
agent some_object.some_routine (?, u, v)
Target
ATOT - Lecture 20, 11 June 2003
17
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)
ATOT - Lecture 20, 11 June 2003
18
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)
ATOT - Lecture 20, 11 June 2003
19
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
ATOT - Lecture 20, 11 June 2003
20
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
ATOT - Lecture 20, 11 June 2003
21
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”.
ATOT - Lecture 20, 11 June 2003
22
Chair of Software Engineering
Event-driven programming
PUBLISHERS SUBSCRIBERS
trigger events handle events
EVENTS
ROUTINE
ROUTINE
ROUTINE
ATOT - Lecture 20, 11 June 2003
23
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)
ATOT - Lecture 20, 11 June 2003
24
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)
ATOT - Lecture 20, 11 June 2003
25
Chair of Software Engineering
EiffelVision style
my_button.click.action_list.extend (agent my_procedure)
ATOT - Lecture 20, 11 June 2003
26
Chair of Software Engineering
Observer pattern (C++, Java)
SUBSCRIBER*
PUBLISHER*
APPCLASS LIBCLASS
attachdetach
update*
update+
Deferred (abstract)
Effective (implemented)
*+
Inherits fromClient (uses)
ATOT - Lecture 20, 11 June 2003
27
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
ATOT - Lecture 20, 11 June 2003
28
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)
ATOT - Lecture 20, 11 June 2003
29
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
ATOT - Lecture 20, 11 June 2003
30
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
ATOT - Lecture 20, 11 June 2003
31
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
ATOT - Lecture 20, 11 June 2003
32
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.
ATOT - Lecture 20, 11 June 2003
33
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
ATOT - Lecture 20, 11 June 2003
34
Chair of Software Engineering
Observer pattern (C++, Java)
SUBSCRIBER*
PUBLISHER*
APPCLASS LIBCLASS
attachdetach
update*
update+
Deferred (abstract)
Effective (implemented)
*+
Inherits fromClient (uses)