68
1 http://libre.adacore.com © AdaCore under the GNU Free Documentation License Franco Gasperoni [email protected] http://libre.adacore.com

1 © AdaCore under the GNU Free Documentation License Franco Gasperoni [email protected]

Embed Size (px)

Citation preview

Page 1: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

1http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Franco Gasperoni

[email protected]

http://libre.adacore.com

Page 2: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

3http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Page 3: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

4http://libre.adacore.com © AdaCore under the GNU Free Documentation License

When creating a new systemWhen creating a new systemyou must identify its ...you must identify its ...

• Data types (what kind of data will be manipulated)

• Functionalities(what kind of manipulations are

allowed)

Page 4: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

5http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Software System Software System OrganizationOrganization

• Around its functionalities– (functionality-oriented / structured programming)

• around its data types– (object-oriented programming)

Page 5: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

6http://libre.adacore.com © AdaCore under the GNU Free Documentation License

• Object-Oriented Organization– inheritance (simple)

– polymorphism

– abstract types & subprograms

– modifying an OO system

– when to use OO organization

Page 6: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

7http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Often types have Often types have some but not all some but not all

properties in common...properties in common...

• Create completely different types

• Use variant programming to factor commonalties

• Use inheritance

Page 7: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

8http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Alert

Time_Of_ArrivalCause

Handle ()Log ()

Alert

Time_Of_ArrivalCause

Handle ()Log ()

inherited

Low_Alert

Time_Of_ArrivalCause

Handle ()Log ()

Low_Alert

Time_Of_ArrivalCause

Handle ()Log ()

Medium_Alert

Time_Of_Arrival

Cause

Handle ()Log ()

Medium_Alert

Time_Of_Arrival

Cause

Handle ()Log ()

High_Alert

Time_Of_ArrivalCause

Handle ()Log ()

High_Alert

Time_Of_ArrivalCause

Handle ()Log ()

Page 8: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

9http://libre.adacore.com © AdaCore under the GNU Free Documentation License

High_Alert

Time_Of_ArrivalCause

Handle ()Log ()

High_Alert

Time_Of_ArrivalCause

Handle ()Log ()

Alert

Time_Of_ArrivalCause

Handle ()Log ()

Alert

Time_Of_ArrivalCause

Handle ()Log ()

Low_Alert

Time_Of_ArrivalCause

Handle ()Log ()

Low_Alert

Time_Of_ArrivalCause

Handle ()Log ()

Medium_Alert

Time_Of_Arrival

Cause

Handle ()Log ()

Medium_Alert

Time_Of_Arrival

Cause

Handle ()Log ()

redefined

Page 9: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

10http://libre.adacore.com © AdaCore under the GNU Free Documentation License

High_Alert

Time_Of_ArrivalCause

EngineerRing_Alarm_At

Handle ()Log ()

Set_Alarm

High_Alert

Time_Of_ArrivalCause

EngineerRing_Alarm_At

Handle ()Log ()

Set_Alarm

Alert

Time_Of_ArrivalCause

Handle ()Log ()

Alert

Time_Of_ArrivalCause

Handle ()Log ()

Low_Alert

Time_Of_ArrivalCause

Handle ()Log ()

Low_Alert

Time_Of_ArrivalCause

Handle ()Log ()

Medium_Alert

Time_Of_Arrival

Cause

Technician

Handle ()Log ()

Medium_Alert

Time_Of_Arrival

Cause

Technician

Handle ()Log ()

added

Page 10: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

11http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Are 4 Are 4 Different Different TypesTypes

• Alert• Low_Alert• Medium_Alert• High_Alert

Page 11: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

12http://libre.adacore.com © AdaCore under the GNU Free Documentation License

with …;

package Alerts is

type Alert is tagged record Time_Of_Arrival : Calendar.Time; Cause : String (1 .. 200); end record;

procedure Handle (A : in out Alert); procedure Log (A : Alert);

...end Alerts;

Primitiveoperatio

ns(method

s)

Alert is a tagged typeAlert is a tagged type

Page 12: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

13http://libre.adacore.com © AdaCore under the GNU Free Documentation License

package Alerts is

type Alert is tagged record Time_Of_Arrival : Calendar.Time; Cause : String (1 .. 200); end record; procedure Handle (A : in out Alert); procedure Log (A : Alert);

type Low_Alert is new Alert with null record;

...end Alerts; Derived type

inherits everythingby default

inherited

Page 13: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

14http://libre.adacore.com © AdaCore under the GNU Free Documentation License

package Alerts is

type Alert is tagged record Time_Of_Arrival : Calendar.Time; Cause : String (1 .. 200); end record; procedure Handle (A : in out Alert); procedure Log (A : Alert);

type Medium_Alert is new Alert with record Technician : Person; end record;

procedure Handle (A : in out Medium_Alert);

...end Alerts;

added added

redefined redefined

inherited inherited

Page 14: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

15http://libre.adacore.com © AdaCore under the GNU Free Documentation License

package Alerts is

type Alert is tagged record Time_Of_Arrival : Calendar.Time; Cause : String (1 .. 200); end record; procedure Handle (A : in out Alert); procedure Log (A : Alert);

type High_Alert is new Alert with record Engineer : Person; Ring_Alarm_At : Calendar.Time; end record;

procedure Set_Alarm (A : in out Alert; Wait :

Duration);

procedure Handle (A : in out High_Alert);

end Alerts;

added added

redefined redefined

inherited inherited

Page 15: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

16http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Attributes (record fields)Attributes (record fields)

• Are always inherited

• Can never be redefined or deleted

• You can add new attributes

Page 16: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

17http://libre.adacore.com © AdaCore under the GNU Free Documentation License

with Alerts; use Alerts;procedure Client is A : Alert; A_L : Low_Alert; A_M : Medium_Alert; A_H : High_Alert;

begin A . Time_Of_Arrival := …; A_L . Time_Of_Arrival := …; A_M . Time_Of_Arrival := …; A_H . Time_Of_Arrival := …;end Client;

OK

Inherited AttributesInherited Attributes

Page 17: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

18http://libre.adacore.com © AdaCore under the GNU Free Documentation License

with Alerts; use Alerts;procedure Client is A : Alert; A_L : Low_Alert; A_M : Medium_Alert; A_H : High_Alert;

begin A . Engineer := …; A_L . Engineer := …; A_M . Engineer := …;

A_H . Engineer := …;end Client;

Compilation Error

Engineer defined only for

High_Alert

Added AttributesAdded Attributes

Page 18: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

19http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Operations (methods)Operations (methods)

• Inherited operation has exactly the same code as the original

• Redefined (or overridden) operations have new code (can never delete an operation)

• Added operations are new operations

Page 19: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

20http://libre.adacore.com © AdaCore under the GNU Free Documentation License

with Alerts; use Alerts;procedure Client is A : Alert; A_L : Low_Alert; A_M : Medium_Alert; A_H : High_Alert;

begin Handle (A);

Handle (A_L);

Handle (A_M);

Handle (A_H);end Client;

with Alerts; use Alerts;procedure Client is A : Alert; A_L : Low_Alert; A_M : Medium_Alert; A_H : High_Alert;

begin Handle (A);

Handle (A_L);

Handle (A_M);

Handle (A_H);end Client;

type Alert is tagged record ... end record;procedure Handle (A : in out Alert);

type Low_Alert is tagged record ... end record;-- procedure Handle (A : in out Low_Alert);

type Medium_Alert is new Alert with ... end record;

procedure Handle (A : in out Medium_Alert);

type High_Alert is new Alert with ... end record;

procedure Handle (A : in out High_Alert);

type Alert is tagged record ... end record;procedure Handle (A : in out Alert);

type Low_Alert is tagged record ... end record;-- procedure Handle (A : in out Low_Alert);

type Medium_Alert is new Alert with ... end record;

procedure Handle (A : in out Medium_Alert);

type High_Alert is new Alert with ... end record;

procedure Handle (A : in out High_Alert);

Inherited & Inherited & RedefinedRedefinedOperationsOperations

Page 20: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

21http://libre.adacore.com © AdaCore under the GNU Free Documentation License

with Alerts; use Alerts;procedure Client is A : Alert; A_L : Low_Alert; A_M : Medium_Alert; A_H : High_Alert;

begin Set_Alarm (A, 1800); Set_Alarm (A_L, 1800); Set_Alarm (A_M, 1800);

Set_Alarm (A_H, 1800);end Client;

Compilation Error

Set_Alarm defined only for

High_Alert

Added OperationsAdded Operations

Page 21: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

22http://libre.adacore.com © AdaCore under the GNU Free Documentation License

procedure Handle (A : in out Alert) isbegin A.Time_Of_Arrival := Calendar.Clock; A.Cause := Get_Cause (A);

Log (A);

case A.P is when Low => null;

when Medium => A.Technician := Assign_Technician;

when High => A.Engineer := Assign_Engineer; Set_Alarm (A, Wait => 1800); end case;end Handle;

Variant ProgrammingVariant Programming

Page 22: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

23http://libre.adacore.com © AdaCore under the GNU Free Documentation License

procedure Handle (…) isbegin A.Time_Of_Arrival := …; A.Cause := …; Log (A); case A.P is when Low => null;

when Medium => A.Technician := …;

when High => A.Engineer := …; Set_Alarm (A, ...); end case;end Handle;

procedure Handle (…) isbegin A.Time_Of_Arrival := …; A.Cause := …; Log (A); case A.P is when Low => null;

when Medium => A.Technician := …;

when High => A.Engineer := …; Set_Alarm (A, ...); end case;end Handle;

procedure Handle (A : in out Alert) isbegin A.Time_Of_Arrival := Calendar.Clock; A.Cause := Get_Cause (A);

Log (A);

end Handle;

Programming with Programming with InheritanceInheritance

procedure Handle (A : in out Medium_Alert) isbegin Handle (Alert (A)); -- First handle as plain Alert

A.Technician := Assign_Technician;

end Handle;

Page 23: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

24http://libre.adacore.com © AdaCore under the GNU Free Documentation License

procedure Handle (A : in out Alert) isbegin A.Time_Of_Arrival := Calendar.Clock; A.Cause := Get_Cause (A);

Log (A);

end Handle;

procedure Handle (A : in out High_Alert) isbegin Handle (Alert (A)); -- First handle as plain Alert

A.Engineer := Assign_Engineer; Set_Alarm (A, Wait => 1800);

end Handle;

procedure Handle (…) isbegin A.Time_Of_Arrival := …; A.Cause := …; Log (A); case A.P is when Low => null;

when Medium => A.Technician := …;

when High => A.Engineer := …; Set_Alarm (A, ...); end case;end Handle;

procedure Handle (…) isbegin A.Time_Of_Arrival := …; A.Cause := …; Log (A); case A.P is when Low => null;

when Medium => A.Technician := …;

when High => A.Engineer := …; Set_Alarm (A, ...); end case;end Handle;

Page 24: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

25http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Centralized vs Distributed CodeCentralized vs Distributed Code

• The code which is centralized in the same routine in the functionality-oriented version

• is now distributed across 3 different routines in the object-oriented version

Page 25: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

26http://libre.adacore.com © AdaCore under the GNU Free Documentation License

• Object-Oriented Organization– inheritance (simple)

•encapsulation & inheritance

– polymorphism

– abstract types & subprograms

– modifying an OO system

– when to use OO organization

Page 26: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

27http://libre.adacore.com © AdaCore under the GNU Free Documentation License

with …;

package Alerts is type Alert is tagged private;

procedure Handle (A : in out Alert); procedure Log (A : Alert);

private type Alert is tagged record Time_Of_Arrival : Calendar.Time; Cause : String (1 .. 200); end record;end Alerts;

Page 27: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

28http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Two possibilities to extend Two possibilities to extend AlertAlert

• Child package to access fields – Time_Of_Arrival– Cause

• Normal package if you do not need to access – Time_Of_Arrival– Cause

Page 28: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

29http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Child PackageChild Packagewith Alerts; use Alerts;with …;package Alerts.Medium is type Medium_Alert is new Alert with private; procedure Handle (A : in out Medium_Alert); private type Medium_Alert is new Alert with record Technician : Person; end record;end Alerts.Medium;

with Alerts; use Alerts;with …;package Alerts.Medium is type Medium_Alert is new Alert with private; procedure Handle (A : in out Medium_Alert); private type Medium_Alert is new Alert with record Technician : Person; end record;end Alerts.Medium;

package body Alerts.Medium is

end Alerts.Medium;

package body Alerts.Medium is

end Alerts.Medium;

Can access fields - Time_Of_Arrival - Cause

Page 29: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

30http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Regular PackageRegular Packagewith Alerts; use Alerts;package High_Importance is type High_Alert is new Alert with private; procedure Handle (A : in out High_Alert); procedure Set_Alarm (A : in out High_Alert; W : Duration); private type High_Alert is new Alert with record Engineer : Person; Ring_Alarm_At : Calendar.Time; end record;end High_Importance;

with Alerts; use Alerts;package High_Importance is type High_Alert is new Alert with private; procedure Handle (A : in out High_Alert); procedure Set_Alarm (A : in out High_Alert; W : Duration); private type High_Alert is new Alert with record Engineer : Person; Ring_Alarm_At : Calendar.Time; end record;end High_Importance; package body High_Importance is

end High_Importance;

package body High_Importance is

end High_Importance;

Cannot access fields - Time_Of_Arrival - Cause

Page 30: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

31http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Important RemarkImportant Remark

• Adding a new type derived from Alert– No need to modify what is working already– No need to retest what you did already

• Just add the data type in a separate package (regular or child package)

Page 31: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

32http://libre.adacore.com © AdaCore under the GNU Free Documentation License

• Object-Oriented Organization– inheritance (simple)

– polymorphism

– abstract types & subprograms

– modifying an OO system

– when to use OO organization

Page 32: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

33http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Handling an AlertHandling an Alert

• You have a Get_Alert routine

• Connected to the sensors in the factory

• Collects the alerts

with Alerts; use Alerts;

function Get_Alert return ???;with Alerts; use Alerts;

function Get_Alert return ???;

Page 33: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

34http://libre.adacore.com © AdaCore under the GNU Free Documentation License

ObjectiveObjective

with Alerts; use Alerts;with Get_Alert;procedure Process_Alerts isbegin loop -- infinite loop Handle (Get_Alert); end loop;end Process_Alerts;

with Alerts; use Alerts;with Get_Alert;procedure Process_Alerts isbegin loop -- infinite loop Handle (Get_Alert); end loop;end Process_Alerts;

• Be able to mimic the code used in the variant programming case

Page 34: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

35http://libre.adacore.com © AdaCore under the GNU Free Documentation License

HOW ?HOW ?• 4 different Handle routines depending on the type of the

alert object returned• How can Get_Alert return objects of different types ?

type Alert is tagged record ... end record;procedure Handle (A : in out Alert);

type Low_Alert is tagged record ... end record;-- procedure Handle (A : in out Low_Alert);

type Medium_Alert is new Alert with ... end record;procedure Handle (A : in out Medium_Alert);

type High_Alert is new Alert with ... end record;

procedure Handle (A : in out High_Alert);

type Alert is tagged record ... end record;procedure Handle (A : in out Alert);

type Low_Alert is tagged record ... end record;-- procedure Handle (A : in out Low_Alert);

type Medium_Alert is new Alert with ... end record;procedure Handle (A : in out Medium_Alert);

type High_Alert is new Alert with ... end record;

procedure Handle (A : in out High_Alert);

Page 35: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

37http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Handle()

Log()

Privatestuff

Low_Alert

Handle()

Log()

Privatestuff

Medium_Alert

Handle()

Log()

Privatestuff

Set_Alarm()

High_Alert

The exact same interface becausethey all derive from type Alert

Page 36: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

38http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Inheritance & InterfacesInheritance & Interfaces

• All type T derived from Alert must implement or inherit:

– procedure Handle (A : in out T);

– procedure Log (A : T);

• Cannot remove inherited operations, you can

only redefine their implementation

Page 37: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

39http://libre.adacore.com © AdaCore under the GNU Free Documentation License

?

Handle()

?

Handle()

Idea: select the operation Idea: select the operation dynamicallydynamically

Obj : some unknown type derived from Alert;

Handle (Obj);

Obj : some unknown type derived from Alert;

Handle (Obj);

Page 38: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

41http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Generally Speaking ...Generally Speaking ...

For any tagged type TT’Class

denotes ANY type D derived from T

Page 39: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

42http://libre.adacore.com © AdaCore under the GNU Free Documentation License

For all type Dderived from

T

For all type Dderived from

T

Inheritance TheoremInheritance Theorem

set of operationsimplemented

for objects of type

T

set of operationsimplemented

for objects of type

T

set of operationsimplementedfor objects of

type

D

set of operationsimplementedfor objects of

type

D

Page 40: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

54http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Handling an AlertHandling an Alert

• You have a Get_Alert routine

• Connected to the sensors in the factory

• Collects the alerts

with Alerts; use Alerts;

function Get_Alert return Alert’Class;with Alerts; use Alerts;

function Get_Alert return Alert’Class;

Page 41: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

55http://libre.adacore.com © AdaCore under the GNU Free Documentation License

with Alerts; use Alerts;with Get_Alert;procedure Process_Alerts isbegin loop -- infinite loop declare A : Alert’Class := Get_Alert; begin Handle (A); -- could have written Handle (Get_Alert); end; end loop;end Process_Alerts;

with Alerts; use Alerts;with Get_Alert;procedure Process_Alerts isbegin loop -- infinite loop declare A : Alert’Class := Get_Alert; begin Handle (A); -- could have written Handle (Get_Alert); end; end loop;end Process_Alerts;

Dispatching Call

Dynamic DispatchingDynamic Dispatching

Page 42: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

59http://libre.adacore.com © AdaCore under the GNU Free Documentation License

A : Alert’Class := Get_Alert;A : Alert’Class := Get_Alert;

Handle (A);Handle (A);

A : Alert’Class := Get_Alert;A : Alert’Class := Get_Alert;

Handle (A);Handle (A);

DynamicDynamicBindingBinding

AL : Low_Alert;AL : Low_Alert;

Handle (AL);Handle (AL);

AL : Low_Alert;AL : Low_Alert;

Handle (AL);Handle (AL);StaticStatic

BindinBindingg

A : High_Alert’Class := …;A : High_Alert’Class := …;

Handle (A);Handle (A);

A : High_Alert’Class := …;A : High_Alert’Class := …;

Handle (A);Handle (A); DynamDynamicic

BindinBindingg

Page 43: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

61http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Where is the magic ?Where is the magic ? A : Alert’Class := Get_Alert;

Handle (A);

A : Alert’Class := Get_Alert;

Handle (A);DynamDynamic ic

BindinBindingg

? ?Low_Alert

Handle()

High_Alert

Handle()

Page 44: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

62http://libre.adacore.com © AdaCore under the GNU Free Documentation License

11

33

22

11

22

11

22

’TagTime_Of_Arrival

Cause

’TagTime_Of_Arrival

Cause

Technician

’TagTime_Of_Arrival

Cause

Engineer

Ring_Alarm_At

Low_AlertLow_AlertMedium_AlertMedium_AlertHigh_AlertHigh_Alert

HandleHandle

LogLog

HandleHandleHandleHandle

Set_AlarmSet_Alarm

Tables of pointers to primitive operationsTables of pointers to primitive operations

’Tag is a pointer’Tag is a pointer

Page 45: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

63http://libre.adacore.com © AdaCore under the GNU Free Documentation License

A : Alert’Class := Get_Alert;

Handle (A);

A : Alert’Class := Get_Alert;

Handle (A);

A : Alert’Class := Get_Alert;

Log (A);

A : Alert’Class := Get_Alert;

Log (A);indirect indirect callcallto to

operation operation pointed bypointed by

A’Tag (2)A’Tag (2)

indirect indirect callcallto to

operation operation pointed bypointed by

A’Tag (1)A’Tag (1)

Page 46: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

64http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Can we DOUBLE dispatch ?Can we DOUBLE dispatch ?type Base is tagged null record;

procedure Op (X : Base; Y : Base);

type Base is tagged null record;

procedure Op (X : Base; Y : Base);

type Deriv is new Base with null record;

procedure Op (X : Deriv; Y : Deriv);

type Deriv is new Base with null record;

procedure Op (X : Deriv; Y : Deriv);

V1 : Base ' Class := …;V2 : Base ' Class := …;

Op (V1, V2);

V1 : Base ' Class := …;V2 : Base ' Class := …;

Op (V1, V2);DynamDynamicic

BindinBindingg

If V1'Tag /= V2'Tag raises Constraint_Error

Page 47: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

65http://libre.adacore.com © AdaCore under the GNU Free Documentation License

type T1 is tagged null record;

type T2 is tagged null record;

procedure Op (X : T1; Y : T2);

type T1 is tagged null record;

type T2 is tagged null record;

procedure Op (X : T1; Y : T2);

operation can be dispatching in only one type

CompilationError

What about ...What about ...

Page 48: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

75http://libre.adacore.com © AdaCore under the GNU Free Documentation License

• Object-Oriented Organization– inheritance (simple)

– polymorphism• tags & class wide types

• dynamic dispatching

• using access parameters

• redispatching

– abstract types & subprograms

– modifying an OO system

– when to use OO organization

Page 49: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

76http://libre.adacore.com © AdaCore under the GNU Free Documentation License

procedure Handle (A : in out Alert) isbegin A.Time_Of_Arrival := Calendar.Clock; A.Cause := Get_Cause (A);

Log (A);

end Handle;

procedure Handle (A : in out Medium_Alert) isbegin Handle (Alert (A)); -- First handle as plain Alert

A.Technician := Assign_Technician;

end Handle;

StaticStatic Binding Binding

StaticStatic Binding Binding

always calls:always calls: procedure Log (A : Alert);always calls:always calls: procedure Log (A : Alert);

Page 50: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

77http://libre.adacore.com © AdaCore under the GNU Free Documentation License

What if …What if …… we override Log… we override Log

package Alerts is type Alert is tagged private; procedure Handle (A : in out Alert); procedure Log (A : Alert);

type Medium_Alert is new Alert with private; procedure Handle (A : in out Medium_Alert);

procedure Log (A : Medium_Alert);

private

….

end Alerts;

Page 51: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

78http://libre.adacore.com © AdaCore under the GNU Free Documentation License

procedure Handle (A : in out Alert) isbegin A.Time_Of_Arrival := Calendar.Clock; A.Cause := Get_Cause (A);

Log (Alert’Class (A));

end Handle;

procedure Handle (A : in out Medium_Alert) isbegin Handle (Alert (A)); -- First handle as plain Alert

A.Technician := Assign_Technician;

end Handle;

Dynamic BindingDynamic BindingRedispatchingRedispatching

Dynamic BindingDynamic BindingRedispatchingRedispatching

Page 52: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

79http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Dispatching PhilosophyDispatching Philosophy

• Ada:– All primitive operations are potentially dispatching – Decide when to have a dispatching call

• C++:– Decide which methods are dispatching (virtual methods)– All calls to these functions are dispatching by default

• Java:– All primitive operations are dispatching – all calls are dispatching

Page 53: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

80http://libre.adacore.com © AdaCore under the GNU Free Documentation License

• Object-Oriented Organization– inheritance (simple)

– polymorphism

– abstract types & subprograms

– modifying an OO system

– when to use OO organization

Page 54: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

81http://libre.adacore.com © AdaCore under the GNU Free Documentation License

In the In the AlertAlert example ... example ...

• One could create objects of type Alert rather than – Low_Alert, Medium_Alert, High_Alert

• Undesirable if plain Alert has no significance but is used only to transmit:– Fields: Time_Of_Arrival & Cause– Methods: Handle & Log

Page 55: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

82http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Make Make AlertAlert an an abstractabstract type type

package Alerts is type Alert is abstract tagged private;

procedure Handle (A : in out Alert); procedure Log (A : Alert);

private type Alert is tagged record Time_Of_Arrival : Calendar.Time; Cause : String (1 .. 200); end record;end Alerts;

Page 56: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

83http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Cannot create objects of an Cannot create objects of an abstract typeabstract type

type Alert is abstract tagged private;

A : Alert; Compilation errorAlert is an

abstract type

Page 57: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

84http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Can have Can have abstractabstract operations operations

package Alerts is type Alert is abstract tagged private;

procedure Handle (A : in out Alert); procedure Log (A : Alert) is abstract;

private type Alert is tagged record Time_Of_Arrival : Calendar.Time; Cause : String (1 .. 200); end record;end Alerts;

Page 58: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

85http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Rules for abstract operationsRules for abstract operations

• Do not provide the body of an abstract operation

• Every non abstract type derived from an abstract type must provide the body of all abstract operations

package Alerts is type Alert is abstract tagged private; procedure Handle (A : in out Alert); procedure Log (A : Alert) is abstract;

type Low_Alert is new Alert with private; procedure Log (A : Low_Alert);

package Alerts is type Alert is abstract tagged private; procedure Handle (A : in out Alert); procedure Log (A : Alert) is abstract;

type Low_Alert is new Alert with private; procedure Log (A : Low_Alert);

Must provide Must provide LogLog or or make make Low_AlertLow_Alert abstract abstract

Must provide Must provide LogLog or or make make Low_AlertLow_Alert abstract abstract

Page 59: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

86http://libre.adacore.com © AdaCore under the GNU Free Documentation License

• Only dispatching calls to abstract routines allowed

procedure Handle (A : in out Alert) isbegin . . .

Log (A);

end Handle;

Compilation error

procedure Log (A : Alert);does not existdoes not exist

procedure Log (A : Alert);does not existdoes not exist

procedure Handle (A : in out Alert) isbegin . . .

Log (Alert’Class (A));

end Handle; Dispatching Dispatching CallCall

Dispatching Dispatching CallCall

OK

Page 60: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

87http://libre.adacore.com © AdaCore under the GNU Free Documentation License

• Object-Oriented Organization– inheritance (simple)

– polymorphism

– abstract types & subprograms

– modifying an OO system

– when to use OO organization

Page 61: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

88http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Adding a NEW Type...Adding a NEW Type...

• Do not modify what is working already– No need to retest what you already did

since you do not need to touch it

• Just add the data type in a separate package (regular or child package)

Page 62: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

89http://libre.adacore.com © AdaCore under the GNU Free Documentation License

package Alerts is type Alert is abstract tagged private; procedure Handle (A : in out Alert); procedure Log (A : Alert);

private . . . end Alerts;

package Alerts is type Alert is abstract tagged private; procedure Handle (A : in out Alert); procedure Log (A : Alert);

private . . . end Alerts;

with Alerts; use Alerts;package Alerts.Medium is type Medium_Alert is new Alert with private; procedure Handle (A : in out Alert); procedure Log (A : Alert);private . . .end Alerts.Medium;

with Alerts; use Alerts;package Alerts.Medium is type Medium_Alert is new Alert with private; procedure Handle (A : in out Alert); procedure Log (A : Alert);private . . .end Alerts.Medium;

Page 63: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

90http://libre.adacore.com © AdaCore under the GNU Free Documentation License

Adding NEW Functionality...Adding NEW Functionality...

• Have to modify the spec containing tagged type T to which we add the functionality

• Have to modify all the packages containing types derived from T to implement the new functionality

– Error Prone & labor intensive

– need to retest everything for regressions

Page 64: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

91http://libre.adacore.com © AdaCore under the GNU Free Documentation License

ExampleExample

• Suppose you want to add a new functionality

• that behaves DIFFERENTLY for all alert types

Page 65: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

92http://libre.adacore.com © AdaCore under the GNU Free Documentation License

package Alerts is type Alert is abstract tagged private; procedure New_Functionality (A : Alert); . . .

package Alerts is type Alert is abstract tagged private; procedure New_Functionality (A : Alert); . . .

with Alerts; use Alerts;package Alerts.Medium is type Medium_Alert is new Alert with private; procedure New_Functionality (A : Medium_Alert); . . .

with Alerts; use Alerts;package Alerts.Medium is type Medium_Alert is new Alert with private; procedure New_Functionality (A : Medium_Alert); . . .

with Alerts; use Alerts;package Alerts.High is type High_Alert is new Alert with private; procedure New_Functionality (A : High_Alert); . . .

with Alerts; use Alerts;package Alerts.High is type High_Alert is new Alert with private; procedure New_Functionality (A : High_Alert); . . .

Page 66: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

93http://libre.adacore.com © AdaCore under the GNU Free Documentation License

• Object-Oriented Organization– inheritance (simple)

– polymorphism

– abstract types & subprograms

– modifying an OO system

– when to use OO organization

Page 67: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

94http://libre.adacore.com © AdaCore under the GNU Free Documentation License

• System Functionalities are well

understood before starting the design

• Adding new functionality will happen

infrequently

• Will add lots of new data types with the

same functionality over the life time of

the system

Page 68: 1  © AdaCore under the GNU Free Documentation License Franco Gasperoni gasperoni@adacore.com

95http://libre.adacore.com © AdaCore under the GNU Free Documentation License

?? UseObject

Oriented

use Functionality-Oriented

UseObject

Oriented

New functionalities can be factored in few tagged typesNew functionalities can be

factored in few tagged typesData type

changes

Functionality changes