ada 95 - object orientation

98
1 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License Franco Gasperoni [email protected] http://libre.act-europe.fr

Upload: gneuromante-canaladaorg

Post on 25-Jan-2015

731 views

Category:

Technology


0 download

DESCRIPTION

Author: Franco Gasperoni. License: GFDL

TRANSCRIPT

Page 1: Ada 95 - Object orientation

1http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Franco [email protected]://libre.act-europe.fr

Page 2: Ada 95 - Object orientation

2http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Copyright NoticeCopyright Notice

• © ACT Europe under the GNU Free Documentation License

• Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; provided its original author is mentioned and the link to http://libre.act-europe.fr/ is kept at the bottom of every non-title slide. A copy of the license is available at:

• http://www.fsf.org/licenses/fdl.html

Page 3: Ada 95 - Object orientation

3http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Page 4: Ada 95 - Object orientation

4http://libre.act-europe.fr © ACT Europe 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 5: Ada 95 - Object orientation

5http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Software System OrganizationSoftware System Organization

• Around its functionalities(functionality-oriented / structured

programming)

• around its data types(object-oriented programming)

Page 6: Ada 95 - Object orientation

6http://libre.act-europe.fr © ACT Europe 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 7: Ada 95 - Object orientation

7http://libre.act-europe.fr © ACT Europe 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 8: Ada 95 - Object orientation

8http://libre.act-europe.fr © ACT Europe 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_ArrivalCause

Handle ()Log ()

Medium_Alert

Time_Of_ArrivalCause

Handle ()Log ()

High_Alert

Time_Of_ArrivalCause

Handle ()Log ()

High_Alert

Time_Of_ArrivalCause

Handle ()Log ()

Page 9: Ada 95 - Object orientation

9http://libre.act-europe.fr © ACT Europe 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_ArrivalCause

Handle ()Log ()

Medium_Alert

Time_Of_ArrivalCause

Handle ()Log ()

redefined

Page 10: Ada 95 - Object orientation

10http://libre.act-europe.fr © ACT Europe 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_ArrivalCause

Technician

Handle ()Log ()

Medium_Alert

Time_Of_ArrivalCause

Technician

Handle ()Log ()

added

Page 11: Ada 95 - Object orientation

11http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Are 4 Are 4 Different Different TypesTypes

• Alert• Low_Alert• Medium_Alert• High_Alert

Page 12: Ada 95 - Object orientation

12http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with …;

package Alerts is

type Alert is tagged recordTime_Of_Arrival : Calendar.Time;Cause : String (1 .. 200);

end record;

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

...end Alerts;

Primitiveoperations(methods)

Alert is a tagged type

Page 13: Ada 95 - Object orientation

13http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Alerts is

type Alert is tagged recordTime_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

Low_Alert is a tagged type derived from Alert

Page 14: Ada 95 - Object orientation

14http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Alerts is

type Alert is tagged recordTime_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 recordTechnician : Person;

end record;

procedure Handle (A : in out Medium_Alert);

...end Alerts;

added

redefined

inherited

Page 15: Ada 95 - Object orientation

15http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Alerts is

type Alert is tagged recordTime_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 recordEngineer : 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

redefined

inherited

Page 16: Ada 95 - Object orientation

16http://libre.act-europe.fr © ACT Europe 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 17: Ada 95 - Object orientation

17http://libre.act-europe.fr © ACT Europe 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;

beginA . 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 18: Ada 95 - Object orientation

18http://libre.act-europe.fr © ACT Europe 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;

beginA . Engineer := …;A_L . Engineer := …;A_M . Engineer := …;

A_H . Engineer := …;end Client;

Compilation Error

Engineerdefined only

for High_Alert

Added AttributesAdded Attributes

Page 19: Ada 95 - Object orientation

19http://libre.act-europe.fr © ACT Europe 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 20: Ada 95 - Object orientation

20http://libre.act-europe.fr © ACT Europe 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;

beginHandle (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;

beginHandle (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 21: Ada 95 - Object orientation

21http://libre.act-europe.fr © ACT Europe 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;

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

Set_Alarm (A_H, 1800);end Client;

Compilation Error

Set_Alarmdefined only

for High_Alert

Added OperationsAdded Operations

Page 22: Ada 95 - Object orientation

22http://libre.act-europe.fr © ACT Europe 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 23: Ada 95 - Object orientation

23http://libre.act-europe.fr © ACT Europe 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 InheritanceProgramming with Inheritance

procedure Handle (A : in out Medium_Alert) isbegin

Handle (Alert (A)); -- First handle as plain Alert

A.Technician := Assign_Technician;end Handle;

Page 24: Ada 95 - Object orientation

24http://libre.act-europe.fr © ACT Europe 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 25: Ada 95 - Object orientation

25http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Centralized Centralized vs vs Distributed CodeDistributed 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 26: Ada 95 - Object orientation

26http://libre.act-europe.fr © ACT Europe 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 27: Ada 95 - Object orientation

27http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with …;

package Alerts istype Alert is tagged private;

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

privatetype Alert is tagged record

Time_Of_Arrival : Calendar.Time;Cause : String (1 .. 200);

end record;end Alerts;

Page 28: Ada 95 - Object orientation

28http://libre.act-europe.fr © ACT Europe 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 29: Ada 95 - Object orientation

29http://libre.act-europe.fr © ACT Europe 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);

privatetype 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);

privatetype 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 30: Ada 95 - Object orientation

30http://libre.act-europe.fr © ACT Europe 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);

privatetype 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);

privatetype 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 31: Ada 95 - Object orientation

31http://libre.act-europe.fr © ACT Europe 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 32: Ada 95 - Object orientation

32http://libre.act-europe.fr © ACT Europe 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 33: Ada 95 - Object orientation

33http://libre.act-europe.fr © ACT Europe 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 34: Ada 95 - Object orientation

34http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

ObjectiveObjective

with Alerts; use Alerts;with Get_Alert;procedure Process_Alerts isbegin

loop -- infinite loopHandle (Get_Alert);

end loop;end Process_Alerts;

with Alerts; use Alerts;with Get_Alert;procedure Process_Alerts isbegin

loop -- infinite loopHandle (Get_Alert);

end loop;end Process_Alerts;

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

Page 35: Ada 95 - Object orientation

35http://libre.act-europe.fr © ACT Europe 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 36: Ada 95 - Object orientation

36http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

PolymorphismPolymorphism

• Variables can name objects with common properties but different types

• Can select dynamically the right operationfor the underlying object

Page 37: Ada 95 - Object orientation

37http://libre.act-europe.fr © ACT Europe 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 38: Ada 95 - Object orientation

38http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Inheritance & InterfacesInheritance & Interfaces

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

– procedure Handle (A : T);– procedure Log (A : T);

• Cannot remove inherited operations, you can only redefine their implementation

Page 39: Ada 95 - Object orientation

39http://libre.act-europe.fr © ACT Europe 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 40: Ada 95 - Object orientation

40http://libre.act-europe.fr © ACT Europe 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 41: Ada 95 - Object orientation

41http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Generally Speaking ...Generally Speaking ...

For any tagged type TT’Class

denotes ANY type D derived from T

}|{'

TDDClassTT

→=

∀from derives

Page 42: Ada 95 - Object orientation

42http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

UTD

DValuesClassTValuesT

=

)()'(

Page 43: Ada 95 - Object orientation

43http://libre.act-europe.fr © ACT Europe 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 typeT

set of operationsimplemented

for objects of typeT

set of operationsimplemented

for objects of typeD

set of operationsimplemented

for objects of typeD

Page 44: Ada 95 - Object orientation

44http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

...) D; : (X Op...) T; : (X Op '

∃⇒∃∈∀ ClassTD

)()'( TOperationsClassTOperations =

Page 45: Ada 95 - Object orientation

45http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

CLASS = Values+ Operations+ Inheritance

Page 46: Ada 95 - Object orientation

46http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

T’Class T’Class in Adain Ada

• Conversions:– A value of any type derived from T can be

implicitly converted to type T’Class– conversion from T’Class to a type derived from T

checks the tag

• T’Class is treated like an unconstrained type

• A variable of type T’Class must be initialized

Page 47: Ada 95 - Object orientation

47http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

A_L1 : Low_Alert;A_L2 : Low_Alert;A_M : Medium_Alert;A_H : High_Alert;

V1 : Alert’Class := A_L1;V2 : Alert’Class := A_M;V3 : Alert’Class := A_H;

V1 := A_L2;

A_L1 := Low_Alert (V1);

A_L1 : Low_Alert;A_L2 : Low_Alert;A_M : Medium_Alert;A_H : High_Alert;

V1 : Alert’Class := A_L1;V2 : Alert’Class := A_M;V3 : Alert’Class := A_H;

V1 := A_L2;

A_L1 := Low_Alert (V1);

ClassClass--Wide ObjectsWide Objects

OK - the type of the object named by V1 and A_L2 is the same (a Low_Alert)

OK - V1 names an objectof type Low_Alert

Page 48: Ada 95 - Object orientation

48http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

A_L1 : Low_Alert;A_L2 : Low_Alert;A_M : Medium_Alert;A_H : High_Alert;

V1 : Alert’Class := A_L1;V2 : Alert’Class := A_M;V3 : Alert’Class := A_H;

V3 := A_L1;

A_L1 := Low_Alert (V2);

A_L1 : Low_Alert;A_L2 : Low_Alert;A_M : Medium_Alert;A_H : High_Alert;

V1 : Alert’Class := A_L1;V2 : Alert’Class := A_M;V3 : Alert’Class := A_H;

V3 := A_L1;

A_L1 := Low_Alert (V2);

Constraint_Errorobjects named by

the 2 variableshave different

types

Page 49: Ada 95 - Object orientation

49http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

V1 : Alert’Class;V1 : Alert’Class;Compilation Error

objects of a class-wide type

MUST be initialized

Page 50: Ada 95 - Object orientation

50http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

type All_Alert_Ptr is access all Alert’Class;

Ptr_1 : All_Alert_Ptr; -- no need to default initialize

Ptr_1 := new Alert;Ptr_1 := new Low_Alert’(Get_Time, Get_Cause);Ptr_1 := new Medium_Alert;Ptr_1 := new High_Alert;

type All_Alert_Ptr is access all Alert’Class;

Ptr_1 : All_Alert_Ptr; -- no need to default initialize

Ptr_1 := new Alert;Ptr_1 := new Low_Alert’(Get_Time, Get_Cause);Ptr_1 := new Medium_Alert;Ptr_1 := new High_Alert;

ClassClass--Wide AccessWide Access

OK

Page 51: Ada 95 - Object orientation

51http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

type All_Alert_Ptr is access all Alert’Class;type Low_Alert_Ptr is access all Low_Alert’Class;

Ptr_M : All_Alert_Ptr;Ptr_L : Low_Alert_Ptr := new Low_Alert;

Ptr_M := new High_Alert;

Ptr_L := Low_Alert_Ptr (Ptr_M);

type All_Alert_Ptr is access all Alert’Class;type Low_Alert_Ptr is access all Low_Alert’Class;

Ptr_M : All_Alert_Ptr;Ptr_L : Low_Alert_Ptr := new Low_Alert;

Ptr_M := new High_Alert;

Ptr_L := Low_Alert_Ptr (Ptr_M);Constraint_Errorobject pointed by Ptr_M must be inLow_Alert’Class

General access typeto all conversions

Page 52: Ada 95 - Object orientation

52http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Given a class-wide variable

V : T’Class := …;

you can ask whether

V in D’Class

for all type D derived from T

ClassClass--Wide MembershipWide Membership

Page 53: Ada 95 - Object orientation

53http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

A_L : Low_Alert;

V1 : Alert’Class := A_L;

B : Boolean;

B := (V1 in Alert ’ Class);

B := (V1 in Low_Alert ’ Class);

B := (V1 in Medium_Alert ’ Class);

A_L : Low_Alert;

V1 : Alert’Class := A_L;

B : Boolean;

B := (V1 in Alert ’ Class);

B := (V1 in Low_Alert ’ Class);

B := (V1 in Medium_Alert ’ Class);

True

True

False

Page 54: Ada 95 - Object orientation

54http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Tag = ID of a tagged type

• For every type T– T’Tag returns the tag of T

• For every class wide variable V– V’Tag returns the tag of the type of the

object named by V

Run Time Type IdentificationRun Time Type Identification

Page 55: Ada 95 - Object orientation

55http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with Ada.Tags; use Ada.Tags;procedure Client is

A_L1 : Low_Alert;A_L2 : Low_Alert;A_M : Medium_Alert;

V1 : Alert’Class := A_L1;V2 : Alert’Class := A_L2;V3 : Alert’Class := A_M;

B : Boolean;begin

B := (V1 ’ Tag = V2 ’ Tag);B := (V2 ’ Tag = V3 ’ Tag);B := (V1 ’ Tag = Low_Alert ’ Tag);

with Ada.Tags; use Ada.Tags;procedure Client is

A_L1 : Low_Alert;A_L2 : Low_Alert;A_M : Medium_Alert;

V1 : Alert’Class := A_L1;V2 : Alert’Class := A_L2;V3 : Alert’Class := A_M;

B : Boolean;begin

B := (V1 ’ Tag = V2 ’ Tag);B := (V2 ’ Tag = V3 ’ Tag);B := (V1 ’ Tag = Low_Alert ’ Tag);

‘Tag Attribute‘Tag Attribute

TrueFalse

True

Page 56: Ada 95 - Object orientation

56http://libre.act-europe.fr © ACT Europe 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 57: Ada 95 - Object orientation

57http://libre.act-europe.fr © ACT Europe 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 58: Ada 95 - Object orientation

58http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with Alerts; use Alerts;with Get_Alert;procedure Process_Alerts isbegin

loop -- infinite loopdeclare

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 loopdeclare

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 59: Ada 95 - Object orientation

59http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Which Handle () is Called ?Which Handle () is Called ?

- A’Tag = Alert’Tag Handle (A: Alert)

- A’Tag = Low_Alert’Tag Handle (A: Low_Alert)

- A’Tag = Medium_Alert’Tag Handle (A: Medium_Alert)

- A’Tag = High_Alert’Tag Handle (A: High_Alert)

- A’Tag = Alert’Tag Handle (A: Alert)

- A’Tag = Low_Alert’Tag Handle (A: Low_Alert)

- A’Tag = Medium_Alert’Tag Handle (A: Medium_Alert)

- A’Tag = High_Alert’Tag Handle (A: High_Alert)

Page 60: Ada 95 - Object orientation

60http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Static Static vs vs Dynamic BindingDynamic Binding

STATIC BINDING = call known at compile time

DYNAMIC BINDING = call known only at run time

Page 61: Ada 95 - Object orientation

61http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

How do you know if call How do you know if call Op (V, …)Op (V, …)

is dispatching ?is dispatching ?

- Type of V is T’Class for some tagged type T

- Op is a primitive operation of Ttype T is … end record;

procedure Op (P : T; …) (or function)procedure Op (P : in out T; …)procedure Op (P : access T; …) (or function)

Page 62: Ada 95 - Object orientation

62http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

A : Alert’Class := Get_Alert;

Handle (A);

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

Handle (A);Handle (A);DynamicDynamicBindingBinding

AL : Low_Alert;

Handle (AL);

AL : Low_Alert;AL : Low_Alert;

Handle (AL);Handle (AL);StaticStaticBindingBinding

A : High_Alert’Class := …;

Handle (A);

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

Handle (A);Handle (A);DynamicDynamicBindingBinding

Page 63: Ada 95 - Object orientation

63http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

type Low_Alert_Class_Ptr is access all Low_Alert’Class;Ptr : Low_Alert_Class_Ptr := …;

Handle (Ptr.all);

type Low_Alert_Class_type Low_Alert_Class_PtrPtr is access all Low_is access all Low_Alert’ClassAlert’Class;;Ptr Ptr : Low_Alert_Class_: Low_Alert_Class_PtrPtr := …;:= …;

Handle (Handle (PtrPtr.all);.all);

Dynamic Dynamic BindingBinding

type Low_Alert_Ptr is access all Low_Alert;Ptr : Low_Alert_Ptr := …;

Handle (Ptr.all);

type Low_Alert_type Low_Alert_Ptr Ptr is access all Low_Alert;is access all Low_Alert;Ptr Ptr : Low_Alert_: Low_Alert_PtrPtr := …;:= …;

Handle (Handle (PtrPtr.all);.all);

Static Static BindingBinding

Page 64: Ada 95 - Object orientation

64http://libre.act-europe.fr © ACT Europe 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);Dynamic Dynamic BindingBinding

???? ????Low_Alert

Handle()

High_Alert

Handle()

Page 65: Ada 95 - Object orientation

65http://libre.act-europe.fr © ACT Europe 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_AlertMedium_AlertHigh_Alert

HandleHandleLogLog

HandleHandleHandleHandle

Set_AlarmSet_Alarm

Tables of pointers to primitive operations

’Tag is a pointer

Page 66: Ada 95 - Object orientation

66http://libre.act-europe.fr © ACT Europe 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 callindirect callto operation to operation pointed bypointed byA’Tag A’Tag (2)(2)

indirect callindirect callto operation to operation pointed bypointed byA’Tag A’Tag (1)(1)

Page 67: Ada 95 - Object orientation

67http://libre.act-europe.fr © ACT Europe 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);DynamicDynamicBindingBinding If V1'Tag /= V2'Tag raises Constraint_Error

Page 68: Ada 95 - Object orientation

68http://libre.act-europe.fr © ACT Europe 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 69: Ada 95 - Object orientation

69http://libre.act-europe.fr © ACT Europe 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

skip

Page 70: Ada 95 - Object orientation

70http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Alerts istype Alert is tagged private;

procedure Handle (A : access Alert);procedure Log (A : access Alert);

privatetype Alert is tagged record

Time_Of_Arrival : Calendar.Time;Cause : String (1 .. 200);

end record;end Alerts;

Sometime it is convenient to use pointers:Sometime it is convenient to use pointers:access parametersaccess parameters

Page 71: Ada 95 - Object orientation

71http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Alerts.Medium istype Medium_Alert is new Alert with private;

procedure Handle (A : access Alert);

privatetype Medium_Alert is new Alert with record

Technician : Person;end record;

end Alerts.Medium;

package Alerts.Medium istype Medium_Alert is new Alert with private;

procedure Handle (A : access Alert);

privatetype Medium_Alert is new Alert with record

Technician : Person;end record;

end Alerts.Medium;

Page 72: Ada 95 - Object orientation

72http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

type Alert_Class_Ptr is access all Alert ’ Class;

function Get_Alert return Alert_Class_Ptr;

type Alert_Class_Ptr is access all Alert ’ Class;

function Get_Alert return Alert_Class_Ptr;

declareA : Alert_Class_Ptr := Get_Alert;

beginHandle (A); -- could have written Handle (Get_Alert);

declareA : Alert_Class_Ptr := Get_Alert;

beginHandle (A); -- could have written Handle (Get_Alert);

Handling Alerts using Handling Alerts using access parametersaccess parameters

DynamicDynamicBindingBinding

Page 73: Ada 95 - Object orientation

73http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Polymorphism is powerfulPolymorphism is powerful

• Alerts are buffered in a linked list

type Alert_Node;type Alert_List is access Alert_Node;

type Alert_Node is recordAn_Alert : Alert_Class_Ptr;Next : Alert_List;

end record;

function Get_Alerts return Alert_List;

type Alert_Node;type Alert_List is access Alert_Node;

type Alert_Node is recordAn_Alert : Alert_Class_Ptr;Next : Alert_List;

end record;

function Get_Alerts return Alert_List;

Page 74: Ada 95 - Object orientation

74http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• All the alerts are processed uniformly

Ptr : Alert_List := Get_Alerts;

while Ptr /= null loopHandle (Ptr.An_Alert);Ptr := Ptr.Next;

end loop;

Ptr : Alert_List := Get_Alerts;

while Ptr /= null loopHandle (Ptr.An_Alert);Ptr := Ptr.Next;

end loop;

Page 75: Ada 95 - Object orientation

75http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Rules for Rules for access parameter access parameter

conversionsconversions

type T is tagged … end record;type T_Ptr is access all T;

procedure Op (X : access T);

type T is tagged … end record;type T_Ptr is access all T;

procedure Op (X : access T);

P : T_Ptr := …;

Op (P);

P : T_Ptr := …;

Op (P);

T is some tagged type

implicitconversionStaticStatic

Binding Binding

Page 76: Ada 95 - Object orientation

76http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

type T is … end record;type T_Class_Ptr is access all T’Class;

procedure Op (X : access T);

type T is … end record;type T_Class_Ptr is access all T’Class;

procedure Op (X : access T);

PC : T_Class_Ptr := ...;

Op (PC);

PC : T_Class_Ptr := ...;

Op (PC);DynamicDynamicBindingBinding

Page 77: Ada 95 - Object orientation

77http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

procedure Op (X : access T) is

P : T_Ptr := T_Ptr (X);

PC : T_Class_Ptr := T_Class_Ptr (X);

begin…

end Op;

procedure Op (X : access T) is

P : T_Ptr := T_Ptr (X);

PC : T_Class_Ptr := T_Class_Ptr (X);

begin…

end Op;

explicitconversionneeded

Page 78: Ada 95 - Object orientation

78http://libre.act-europe.fr © ACT Europe 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 79: Ada 95 - Object orientation

79http://libre.act-europe.fr © ACT Europe 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;

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

Page 80: Ada 95 - Object orientation

80http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

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

package Alerts istype 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 81: Ada 95 - Object orientation

81http://libre.act-europe.fr © ACT Europe 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

Page 82: Ada 95 - Object orientation

82http://libre.act-europe.fr © ACT Europe 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 83: Ada 95 - Object orientation

83http://libre.act-europe.fr © ACT Europe 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 84: Ada 95 - Object orientation

84http://libre.act-europe.fr © ACT Europe 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 85: Ada 95 - Object orientation

85http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Make Make AlertAlert an an abstractabstract typetype

package Alerts istype Alert is abstract tagged private;

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

privatetype Alert is tagged record

Time_Of_Arrival : Calendar.Time;Cause : String (1 .. 200);

end record;end Alerts;

Page 86: Ada 95 - Object orientation

86http://libre.act-europe.fr © ACT Europe 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 87: Ada 95 - Object orientation

87http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Can have Can have abstractabstract operationsoperations

package Alerts istype Alert is abstract tagged private;

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

privatetype Alert is tagged record

Time_Of_Arrival : Calendar.Time;Cause : String (1 .. 200);

end record;end Alerts;

Page 88: Ada 95 - Object orientation

88http://libre.act-europe.fr © ACT Europe 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 istype 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 istype 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 make or make Low_AlertLow_Alert abstractabstract

Page 89: Ada 95 - Object orientation

89http://libre.act-europe.fr © ACT Europe 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

errorprocedure Log (A : Alert);

does not existdoes not exist

procedure Handle (A : in out Alert) isbegin

. . .Log (Alert’Class (A));

end Handle;DispatchingDispatchingCallCall

OK

Page 90: Ada 95 - Object orientation

90http://libre.act-europe.fr © ACT Europe 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 91: Ada 95 - Object orientation

91http://libre.act-europe.fr © ACT Europe 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 92: Ada 95 - Object orientation

92http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

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

private. . .

end Alerts;

package Alerts istype 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 93: Ada 95 - Object orientation

93http://libre.act-europe.fr © ACT Europe 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 94: Ada 95 - Object orientation

94http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

ExampleExample

• Suppose you want to add a new functionality

• that behaves DIFFERENTLY for all alert types

Page 95: Ada 95 - Object orientation

95http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

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

package Alerts istype 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 96: Ada 95 - Object orientation

96http://libre.act-europe.fr © ACT Europe 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 97: Ada 95 - Object orientation

97http://libre.act-europe.fr © ACT Europe 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 98: Ada 95 - Object orientation

98http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

?? UseObject

Oriented

use Functionality-Oriented

UseObject

Oriented

New functionalities can be factored in few tagged types

Data type

changes

Functionality changes