SlideShare una empresa de Scribd logo
1 de 98
Descargar para leer sin conexión
Franco Gasperoni
                             gasperon@act-europe.fr
                             http://libre.act-europe.fr

                                                                                           1
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
Copyright 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




                                                                                            2
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
3
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
When creating a new system
                 you must identify its ...


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


             • Functionalities
                             (what kind of manipulations are allowed)


                                                                                               4
http://libre.act-europe.fr             © ACT Europe under the GNU Free Documentation License
Software System Organization


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

        • around its data types
             (object-oriented programming)


                                                                                     5
http://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



                                                                                             6
http://libre.act-europe.fr           © ACT Europe under the GNU Free Documentation License
Often types have
                                some but not all
                             properties in common...

      • Create completely different types

      • Use variant programming to factor commonalties

      • Use inheritance


                                                                                           7
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
Alert
                                                    Alert
                                        Time_Of_Arrival
                                        Time_Of_Arrival
                                             Cause
                                            Cause
                                                Handle ()
                                                Handle ()
                                                  Log ()
                                                 Log ()



                                Medium_Alert                                          High_Alert
                                Medium_Alert                                          High_Alert
              Low_Alert
              Low_Alert
                                Time_Of_Arrival                                      Time_Of_Arrival
                                Time_Of_Arrival                                      Time_Of_Arrival
          Time_Of_Arrival
          Time_Of_Arrival
                                                                                          Cause
                                     Cause
               Cause                                                                     Cause
                                    Cause
              Cause
                 Handle ()
                 Handle ()
                   Log ()                Handle ()
                  Log ()                 Handle ()
                                                                                        Handle ()
                                                                                        Handle ()
                                           Log ()
                                          Log ()                                          Log ()
                                                                                         Log ()
      inherited                                                                                     8
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
Alert
                                                    Alert
                                        Time_Of_Arrival
                                        Time_Of_Arrival
                                             Cause
                                            Cause
                                                Handle ()
                                                Handle ()
                                                  Log ()
                                                 Log ()



                                Medium_Alert                                          High_Alert
                                Medium_Alert                                          High_Alert
              Low_Alert
              Low_Alert
                                  Time_Of_Arrival                                    Time_Of_Arrival
                                  Time_Of_Arrival                                    Time_Of_Arrival
           Time_Of_Arrival
           Time_Of_Arrival
                                                                                         Cause
                                      Cause
               Cause                                                                     Cause
                                      Cause
               Cause
                 Handle ()
                 Handle ()
                   Log ()
                  Log ()               Handle ()
                                       Handle ()
                                                                                       Handle ()
                                                                                       Handle ()
                                              Log ()
                                             Log ()
                                                                                          Log ()
                                                                                         Log ()    9
       redefined
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
Alert
                                                    Alert
                                        Time_Of_Arrival
                                        Time_Of_Arrival
                                             Cause
                                            Cause
                                                Handle ()
                                                Handle ()
                                                  Log ()
                                                 Log ()



                                Medium_Alert                                          High_Alert
                                Medium_Alert                                          High_Alert
              Low_Alert
              Low_Alert
                                  Time_Of_Arrival                                    Time_Of_Arrival
                                  Time_Of_Arrival                                    Time_Of_Arrival
           Time_Of_Arrival
           Time_Of_Arrival
                                                                                          Cause
                                       Cause
               Cause                                                                     Cause
                                      Cause
               Cause
                                                                                        Engineer
                                     Technician                                         Engineer
                                    Technician
                 Handle ()                                                           Ring_Alarm_At
                 Handle ()                                                           Ring_Alarm_At
                   Log ()                Handle ()
                  Log ()                 Handle ()
                                                                                        Handle ()
                                                                                        Handle ()
                                           Log ()
                                          Log ()                                          Log ()
                                                                                         Log ()
                                                                                       Set_Alarm
                                                                                       Set_Alarm 10
           added
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
•    Alert
           •    Low_Alert
           •    Medium_Alert
           •    High_Alert



                         Are 4 Different Types

                                                                                       11
http://libre.act-europe.fr     © ACT Europe under the GNU Free Documentation License
with …;
                                                                     Alert is a tagged type
         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);

                                                                                           Primitive
            ...
                                                                                          operations
         end Alerts;
                                                                                          (methods)
                                                                                                       12
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
package Alerts is

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

                 type Low_Alert is new Alert with null record;

            ...
                                                                             Derived type
         end Alerts;
                                                                          inherits everything
                 Low_Alert is a tagged type
                                                                              by default
                 derived from Alert
                                                                                                  13
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
package Alerts is

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

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

            ...                                                                           redefined
                                                                                                  14
         end Alerts;
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
package Alerts is

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

          type High_Alert is new Alert with record
                                                             added
             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);
                                                                                         redefined
                                                                                                 15
  end Alerts;
http://libre.act-europe.fr     © ACT Europe under the GNU Free Documentation License
Attributes (record fields)

           • Are always inherited

           • Can never be redefined or deleted

           • You can add new attributes




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

               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                    :=      …;
                                                                                          OK
                  A_L . Time_Of_Arrival                     :=      …;
                  A_M . Time_Of_Arrival                     :=      …;
                  A_H . Time_Of_Arrival                     :=      …;
               end Client;                                                                     17
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
Added Attributes
   with Alerts; use Alerts;
   procedure Client is
      A    : Alert;
      A_L : Low_Alert;
      A_M : Medium_Alert;
                                                                           Compilation
      A_H : High_Alert;
                                                                               Error
                                                                             Engineer
   begin
                                                                           defined only
             A   . Engineer := …;
                                                                                for
             A_L . Engineer := …;
                                                                            High_Alert
             A_M . Engineer := …;

           A_H . Engineer := …;
   end Client;                                                                            18
http://libre.act-europe.fr      © ACT Europe under the GNU Free Documentation License
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


                                                                                          19
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
Inherited &
 with Alerts; use Alerts;
 with Alerts; use Alerts;
 procedure Client is
 procedure Client is
                                                        Redefined
    A ::Alert;
           Alert;
    A
                                                       Operations
    A_L ::Low_Alert;
    A_L Low_Alert;
    A_M ::Medium_Alert;
    A_M Medium_Alert;
    A_H ::High_Alert;
    A_H High_Alert;

 begin
 begin                                 type Alert is tagged record ... end record;
                                      type Alert is tagged record ... end record;
    Handle (A);                       procedure Handle (A ::in out Alert);
                                      procedure Handle (A in out Alert);
    Handle (A);
                                       type Low_Alert is tagged record ... end record;
                                      type Low_Alert is tagged record ... end record;
      Handle (A_L);                   -- procedure Handle (A ::in out Low_Alert);
                                       -- procedure Handle (A in out Low_Alert);
      Handle (A_L);
                                       type Medium_Alert is new Alert with ... end record;
                                      type Medium_Alert is new Alert with ... end record;
      Handle (A_M);                   procedure Handle (A ::in out Medium_Alert);
                                      procedure Handle (A in out Medium_Alert);
      Handle (A_M);

                                       type High_Alert is new Alert with ... end record;
                                      type High_Alert is new Alert with ... end record;
             Handle (A_H);
            Handle (A_H);
                                      procedure Handle (A ::in out High_Alert);20
                                      procedure Handle (A in out High_Alert);
    end Client;
   end Client;
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
Added Operations
   with Alerts; use Alerts;
   procedure Client is
      A    : Alert;
      A_L : Low_Alert;
      A_M : Medium_Alert;
                                                                           Compilation
      A_H : High_Alert;
                                                                               Error
   begin                                                                    Set_Alarm
             Set_Alarm (A, 1800);                                          defined only
             Set_Alarm (A_L, 1800);                                             for
             Set_Alarm (A_M, 1800);                                         High_Alert

             Set_Alarm (A_H, 1800);
   end Client;                                                                            21
http://libre.act-europe.fr      © ACT Europe under the GNU Free Documentation License
Variant Programming
         procedure Handle (A : in out Alert) is
         begin
            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;
                                                                                            22
         end Handle;
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Programming with Inheritance
                                                                                      procedure Handle (…) is
                                                                                     procedure Handle (…) is
                                                                                      begin
                                                                                     begin
        procedure Handle (A : in out Alert) is                                           A.Time_Of_Arrival := …;
                                                                                        A.Time_Of_Arrival := …;
                                                                                         A.Cause            := …;
                                                                                        A.Cause            := …;
        begin                                                                            Log (A);
                                                                                        Log (A);
           A.Time_Of_Arrival := Calendar.Clock;                                          case A.P is
                                                                                        case A.P is
           A.Cause           := Get_Cause (A);                                              when Low =>   =>
                                                                                           when Low
                                                                                                null;
                                                                                              null;
           Log (A);                                                                          when Medium =>
                                                                                            when Medium =>
                                                                                                A.Technician := …;
        end Handle;                                                                           A.Technician := …;
                                                                         when High =>
                                                                        when High =>
                                                                            A.Engineer := …;
                                                                           A.Engineer := …;
                                                                            Set_Alarm (A, ...);
                                                                           Set_Alarm (A, ...);
        procedure Handle (A : in out                 Medium_Alert) is end case;
                                                                         end case;
                                                                   end Handle;
        begin                                                     end Handle;
           Handle (Alert (A)); -- First handle as plain Alert

          A.Technician := Assign_Technician;
        end Handle;
                                                                                                             23
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
procedure Handle (…) is
                                                                                         procedure Handle (…) is
                                                                                          begin
                                                                                         begin
       procedure Handle (A : in out Alert) is                                                A.Time_Of_Arrival := …;
                                                                                            A.Time_Of_Arrival := …;
                                                                                             A.Cause            := …;
                                                                                            A.Cause            := …;
       begin                                                                                 Log (A);
                                                                                            Log (A);
          A.Time_Of_Arrival := Calendar.Clock;                                               case A.P is
                                                                                            case A.P is
          A.Cause           := Get_Cause (A);                                                   when Low =>   =>
                                                                                               when Low
                                                                                                    null;
                                                                                                  null;
          Log (A);                                                                               when Medium =>
                                                                                                when Medium =>
                                                                                                    A.Technician := …;
       end Handle;                                                                                A.Technician := …;
                                                                                                when High =>
                                                                                               when High =>
                                                                                                   A.Engineer := …;
                                                                                                  A.Engineer := …;
                                                                                                   Set_Alarm (A, ...);
                                                                                                  Set_Alarm (A, ...);
       procedure Handle (A : in out High_Alert) is                                              end case;
                                                                                               end case;
                                                                                          end Handle;
       begin                                                                             end Handle;
          Handle (Alert (A)); -- First handle as plain Alert

                A.Engineer := Assign_Engineer;
                Set_Alarm (A, Wait => 1800);
                                                                                                                  24
          end Handle;
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
Centralized 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




                                                                                     25
http://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



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

                                                                                               27
http://libre.act-europe.fr             © ACT Europe under the GNU Free Documentation License
Two possibilities to extend Alert

           • Child package to access fields
                  – Time_Of_Arrival
                  – Cause


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



                                                                                        28
http://libre.act-europe.fr      © ACT Europe under the GNU Free Documentation License
Child Package
                  with Alerts; use Alerts;
                  with Alerts; use Alerts;
                  with …;
                  with …;
                  package Alerts.Medium is
                  package Alerts.Medium is
                       type Medium_Alert is new Alert with private;
                      type Medium_Alert is new Alert with private;
                      procedure Handle (A ::in out Medium_Alert);
                       procedure Handle (A in out Medium_Alert);
                   private
                   private
                       type Medium_Alert is new Alert with record
                      type Medium_Alert is new Alert with record
                         Technician :: Person;
                          Technician Person;
                       end record;
                      end record;
                  end Alerts.Medium;
                  end Alerts.Medium;
     package body Alerts.Medium is
     package body Alerts.Medium is
                                                                  Can access fields
                                                                   - Time_Of_Arrival
                                                                   - Cause
     end Alerts.Medium;
     end Alerts.Medium;                                                                  29
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
Regular Package
   with Alerts; use Alerts;
   with Alerts; use Alerts;
   package High_Importance is
   package High_Importance is
        type High_Alert is new Alert with private;
       type High_Alert is new Alert with private;
       procedure Handle (A ::in out High_Alert);
        procedure Handle (A in out High_Alert);
       procedure Set_Alarm (A ::in out High_Alert; W ::Duration);
        procedure Set_Alarm (A in out High_Alert; W Duration);
    private
    private
        type High_Alert is new Alert with record
       type High_Alert is new Alert with record
          Engineer ::Person;
           Engineer Person;
          Ring_Alarm_At ::Calendar.Time;
           Ring_Alarm_At Calendar.Time;
        end record;
       end record;
   end High_Importance;        package body High_Importance is
   end High_Importance;        package body High_Importance is

          Cannot access fields
           - Time_Of_Arrival
                                             end High_Importance;
           - Cause                           end High_Importance;                       30
http://libre.act-europe.fr      © ACT Europe under the GNU Free Documentation License
Important 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)


                                                                                        31
http://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




                                                                                            32
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Handling an Alert

           • You have a Get_Alert routine
           • Connected to the sensors in the factory
           • Collects the alerts

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



                                                                                        33
http://libre.act-europe.fr      © ACT Europe under the GNU Free Documentation License
Objective
           • Be able to mimic the code used in the variant
             programming case


                             with Alerts; use Alerts;
                             with Alerts; use Alerts;
                             with Get_Alert;
                             with Get_Alert;
                             procedure Process_Alerts is
                             procedure Process_Alerts is
                             begin
                             begin
                                 loop -- infinite loop
                                loop -- infinite loop
                                    Handle (Get_Alert);
                                   Handle (Get_Alert);
                                 end loop;
                                end loop;
                             end Process_Alerts;
                             end Process_Alerts;
                                                                                               34
http://libre.act-europe.fr             © ACT Europe under the GNU Free Documentation License
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;
             type Alert is tagged record ... end record;
             procedure Handle (A :: in out Alert);
              procedure Handle (A in out Alert);

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

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

              type High_Alert is new Alert with ... end record;
             type High_Alert is new Alert with ... end record;
             procedure Handle (A :: in out High_Alert);
              procedure Handle (A in out High_Alert);                                  35
http://libre.act-europe.fr     © ACT Europe under the GNU Free Documentation License
Polymorphism

           • Variables can name objects with common
             properties but different types

           • Can select dynamically the right operation
             for the underlying object




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

                                                                                   Handle()
          Handle()               Handle()

                                                                                         Log()
                Log()                 Log()

                       Private                Private                            Set_Alarm()
                        stuff                  stuff
                                                                                            Private
                                                                                             stuff

                    The exact same interface because
                    they all derive from type Alert
                                                                                                       37
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
Inheritance & 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
                                                                                                 38
http://libre.act-europe.fr      © ACT Europe under the GNU Free Documentation License
Idea: select the operation
                            dynamically
                       Obj ::some unknown type derived from Alert;
                       Obj some unknown type derived from Alert;

                       Handle (Obj);
                       Handle (Obj);


                             Handle()                                             Handle()




                                        ?                                                       ?

                                                                                                    39
http://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
                                                                                            40
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Generally Speaking ...

                                For any tagged type T
                                  T’Class
                       denotes ANY type D derived from T



        ∀T
                                                                            derives from
        T ' Class = {D | D   → T }
                               
                                                                                            41
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
∀T
                                                                  UValues( D)
     Values(T ' Class ) =
                                                               D →T




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

                                    For all type D
                                    For all type D
                                     derived from
                                    derived from
                                                    T
                                                    T




                                                 ⊆
                                                                                   set of operations
              set of operations                                                   set of operations
             set of operations
                                                                                     implemented
                implemented                                                         implemented
               implemented
                                                                                  for objects of type
             for objects of type                                                 for objects of type
            for objects of type
                                                                                           D
                             T                                                             D
                             T
                                                                                                        43
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
∀ D ∈ T ' Class
       ∃ Op (X : T; ...) ⇒ ∃ Op (X : D; ...)


     Operations (T ' Class ) = Operations (T )

                                                                                     44
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
CLASS = Values
                                   + Operations
                                   + Inheritance



                                                                                          45
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
T’Class in 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
                                                                                         46
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
Class-Wide Objects
                       A_L1 :: Low_Alert;
                       A_L1 Low_Alert;
                       A_L2 :: Low_Alert;
                       A_L2 Low_Alert;
                       A_M ::Medium_Alert;
                       A_M Medium_Alert;
                       A_H ::High_Alert;
                       A_H High_Alert;

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

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


                       A_L1 := Low_Alert (V1); OK - V1 names an object
                       A_L1 := Low_Alert (V1);
                                               of type Low_Alert
                                                                                            47
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
A_L1 :: Low_Alert;
                       A_L1 Low_Alert;
                       A_L2 :: Low_Alert;
                       A_L2 Low_Alert;
                       A_M ::Medium_Alert;
                       A_M Medium_Alert;
                       A_H ::High_Alert;
                       A_H High_Alert;

                       V1 ::Alert’Class := A_L1;
                       V1 Alert’Class := A_L1;
                       V2 ::Alert’Class := A_M;
                       V2 Alert’Class := A_M;
                       V3 ::Alert’Class := A_H;
                       V3 Alert’Class := A_H;
                                                                             Constraint_Error
                                                                             objects named by
                                                                              the 2 variables
                       V3 := A_L1;
                       V3 := A_L1;
                                                                              have different
                                                                                   types
                       A_L1 := Low_Alert (V2);
                       A_L1 := Low_Alert (V2);
                                                                                                48
http://libre.act-europe.fr           © ACT Europe under the GNU Free Documentation License
Compilation Error
                                                                       objects of a
                  V1 ::Alert’Class;
                  V1 Alert’Class;
                                                                      class-wide type
                                                                          MUST
                                                                       be initialized




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

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

              Ptr_1 :: All_Alert_Ptr; -- no need to default initialize
              Ptr_1 All_Alert_Ptr; -- no need to default initialize

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

                                                                                 OK


                                                                                         50
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
General access type
                                                                      to all conversions

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

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

       Ptr_M := new High_Alert;
       Ptr_M := new High_Alert;
                                                                                    Constraint_Error
                                                                                   object pointed by
       Ptr_L := Low_Alert_Ptr (Ptr_M);
       Ptr_L := Low_Alert_Ptr (Ptr_M);
                                                                                    Ptr_M must be in
                                                                                     Low_Alert’Class

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

           Given a class-wide variable

                                V : T’Class := …;

           you can ask whether

                                V in D’Class

           for all type D derived from T


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

                       V1 ::Alert’Class := A_L;
                       V1 Alert’Class := A_L;

                       B ::Boolean;
                       B Boolean;


                       B := (V1 in Alert Class);
                       B := (V1 in Alert ’’Class); True

                       B := (V1 in Low_Alert Class);
                       B := (V1 in Low_Alert ’’Class); True

                       B := (V1 in Medium_Alert Class);
                       B := (V1 in Medium_Alert ’’Class); False
                                                                                              53
http://libre.act-europe.fr            © ACT Europe under the GNU Free Documentation License
Run Time Type Identification

           • 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
                                                                                     54
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
‘Tag Attribute
                             with Ada.Tags; use Ada.Tags;
                             with Ada.Tags; use Ada.Tags;
                             procedure Client is
                             procedure Client is
                               A_L1 ::Low_Alert;
                                A_L1 Low_Alert;
                               A_L2 ::Low_Alert;
                                A_L2 Low_Alert;
                               A_M ::Medium_Alert;
                                A_M Medium_Alert;

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

                               B ::Boolean;
                               B Boolean;
                             begin
                             begin
                               B := (V1 ’’Tag = V2 ’’Tag); True
                               B := (V1 Tag = V2 Tag);
                                                              False
                               B := (V2 ’’Tag = V3 ’’Tag);
                               B := (V2 Tag = V3 Tag);
                               B := (V1 ’’Tag = Low_Alert ’’Tag); True
                               B := (V1 Tag = Low_Alert Tag);                                   55
http://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
                                                                                            56
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Handling an Alert

           • You have a Get_Alert routine
           • Connected to the sensors in the factory
           • Collects the alerts

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



                                                                                        57
http://libre.act-europe.fr      © ACT Europe under the GNU Free Documentation License
Dynamic Dispatching
  with Alerts; use Alerts;
  with Alerts; use Alerts;
  with Get_Alert;
  with Get_Alert;
  procedure Process_Alerts is
  procedure Process_Alerts is
  begin
  begin
      loop -- infinite loop
     loop -- infinite loop
         declare
        declare
           A ::Alert’Class := Get_Alert;
            A Alert’Class := Get_Alert;
         begin
        begin
           Handle (A); -- could have written Handle (Get_Alert);
           Handle (A); -- could have written Handle (Get_Alert);
         end;
        end;
      end loop;
     end loop;
  end Process_Alerts;
  end Process_Alerts;
                                                         Dispatching Call                 58
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
Which Handle () is Called ?
                             -- A’Tag = Alert’Tag
                                A’Tag = Alert’Tag
                                  Handle (A: Alert)
                                  Handle (A: Alert)

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

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

                             -- A’Tag = High_Alert’Tag
                                A’Tag = High_Alert’Tag
                                  Handle (A: High_Alert)
                                  Handle (A: High_Alert)
                                                                                            59
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Static vs Dynamic Binding

  STATIC BINDING = call known at compile time


  DYNAMIC BINDING = call known only at run time




                                                                                     60
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
How do you know if call
                                    Op (V, …)
                                is dispatching ?

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


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

                 procedure Op (P : T; …)                                                   (or function)
                 procedure Op (P : in out T; …)
                 procedure Op (P : access T; …)                                            (or function)
                                                                                                           61
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
AL ::Low_Alert;
           AL Low_Alert;

           Handle (AL);
           Handle (AL);
                Static
                                                 A ::Alert’Class := Get_Alert;
                                                 A Alert’Class := Get_Alert;
                Binding
                                               Handle (A);
                                               Handle (A);
                                                 Dynamic
                                                 Binding

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

          Handle (A);
          Handle (A);
            Dynamic
            Binding                                                                  62
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
type Low_Alert_Ptr is access all Low_Alert;
             type Low_Alert_Ptr is access all Low_Alert;
             Ptr ::Low_Alert_Ptr := …;
              Ptr Low_Alert_Ptr := …;

             Handle ((Ptr.all);
             Handle Ptr.all);

                      Static
                      Binding


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

        Handle ((Ptr.all);
        Handle Ptr.all);

               Dynamic
               Binding
                                                                                          63
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
Where is the magic ?
                                 A ::Alert’Class := Get_Alert;
                                 A Alert’Class := Get_Alert;

                                 Handle (A);
                                 Handle (A);
                                  Dynamic
                                  Binding


                                            ?                                                         ?
                             Handle()                                             Handle()

                                  Low_Alert
                                                                                                High_Alert




                                                                                                             64
http://libre.act-europe.fr              © ACT Europe under the GNU Free Documentation License
Tables of pointers to primitive operations

                                                1                                                1
                                Handle                                     Handle                             Handle
           1
           2                                    2                                                2              Log
           3                 Set_Alarm



                                             ’Tag is a pointer


                    ’Tag                                     ’Tag                                      ’Tag
           Time_Of_Arrival                        Time_Of_Arrival                                Time_Of_Arrival

                    Cause                                   Cause                                      Cause
                  Engineer                            Technician
            Ring_Alarm_At

               High_Alert                         Medium_Alert                                       Low_Alert65
http://libre.act-europe.fr               © ACT Europe under the GNU Free Documentation License
A ::Alert’Class := Get_Alert;
                                        A Alert’Class := Get_Alert;

                                        Handle (A);
                                        Handle (A);
                                   indirect call
                                   to operation
                                    pointed by
                                     A’Tag (1)

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

                Log (A);
                Log (A);
            indirect call
            to operation
             pointed by
              A’Tag (2)                                                                 66
http://libre.act-europe.fr      © ACT Europe under the GNU Free Documentation License
Can we DOUBLE dispatch ?
         type Base is tagged null record;
        type Base is tagged null record;

        procedure Op (X ::Base; Y ::Base);
        procedure Op (X Base; Y Base);
         type Deriv is new Base with null record;
        type Deriv is new Base with null record;

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

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

        Op (V1, V2);
        Op (V1, V2);
         Dynamic
                             If V1'Tag /= V2'Tag raises Constraint_Error
         Binding                                                                           67
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
What about ...

         type T1 is tagged null record;
        type T1 is tagged null record;
         type T2 is tagged null record;
        type T2 is tagged null record;

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



           operation can be dispatching in only one type


                                                                                            68
http://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                                    skip
                               • redispatching
                             – abstract types & subprograms
                             – modifying an OO system
                             – when to use OO organization
                                                                                                   69
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Sometime it is convenient to use pointers:
                               access parameters
                             package Alerts is
                                type Alert is tagged private;

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

                              private
                                 type Alert is tagged record
                                    Time_Of_Arrival : Calendar.Time;
                                    Cause            : String (1 .. 200);
                                 end record;
                             end Alerts;
                                                                                                 70
http://libre.act-europe.fr               © ACT Europe under the GNU Free Documentation License
package Alerts.Medium is
                  package Alerts.Medium is
                      type Medium_Alert is new Alert with private;
                     type Medium_Alert is new Alert with private;

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

                   private
                   private
                       type Medium_Alert is new Alert with record
                      type Medium_Alert is new Alert with record
                         Technician :: Person;
                          Technician Person;
                       end record;
                      end record;
                  end Alerts.Medium;
                  end Alerts.Medium;


                                                                                                71
http://libre.act-europe.fr              © ACT Europe under the GNU Free Documentation License
Handling Alerts using
                              access parameters
                  type Alert_Class_Ptr is access all Alert ’’ Class;
                   type Alert_Class_Ptr is access all Alert Class;

                   function Get_Alert return Alert_Class_Ptr;
                  function Get_Alert return Alert_Class_Ptr;

         declare
         declare
           A ::Alert_Class_Ptr := Get_Alert;
            A Alert_Class_Ptr := Get_Alert;
         begin
         begin
            Handle (A); -- could have written Handle (Get_Alert);
           Handle (A); -- could have written Handle (Get_Alert);
                  Dynamic
                  Binding
                                                                                          72
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
Polymorphism is powerful
         • Alerts are buffered in a linked list

                      type Alert_Node;
                     type Alert_Node;
                      type Alert_List is access Alert_Node;
                     type Alert_List is access Alert_Node;

                      type Alert_Node is record
                     type Alert_Node is record
                        An_Alert ::Alert_Class_Ptr;
                         An_Alert Alert_Class_Ptr;
                         Next    ::Alert_List;
                                   Alert_List;
                        Next
                      end record;
                     end record;

                      function Get_Alerts return Alert_List;
                     function Get_Alerts return Alert_List;

                                                                                           73
http://libre.act-europe.fr         © ACT Europe under the GNU Free Documentation License
• All the alerts are processed uniformly

                             Ptr ::Alert_List := Get_Alerts;
                             Ptr Alert_List := Get_Alerts;

                             while Ptr /= null loop
                             while Ptr /= null loop
                                Handle (Ptr.An_Alert);
                               Handle (Ptr.An_Alert);
                                Ptr := Ptr.Next;
                               Ptr := Ptr.Next;
                             end loop;
                             end loop;




                                                                                                 74
http://libre.act-europe.fr               © ACT Europe under the GNU Free Documentation License
Rules for
                             access parameter
                               conversions
         type T is tagged … end record;
        type T is tagged … end record;                                 T is some tagged type
         type T_Ptr is access all T;
        type T_Ptr is access all T;

        procedure Op (X :: access T);
        procedure Op (X access T);

           P ::T_Ptr := …;
           P T_Ptr := …;

                                                                   implicit
           Op (P);
           Op (P);
                                                                  conversion
         Static
         Binding
                                                                                           75
http://libre.act-europe.fr      © ACT Europe under the GNU Free Documentation License
type T is … end record;
        type T is … end record;
         type T_Class_Ptr is access all T’Class;
        type T_Class_Ptr is access all T’Class;

        procedure Op (X :: access T);
        procedure Op (X access T);

          PC ::T_Class_Ptr := ...;
          PC T_Class_Ptr := ...;

          Op (PC);
          Op (PC);
        Dynamic
        Binding
                                                                                      76
http://libre.act-europe.fr    © ACT Europe under the GNU Free Documentation License
explicit
                                                                                       conversion
                                                                                        needed
   procedure Op (X :: access T) is
   procedure Op (X access T) is

         P ::T_Ptr
         P   T_Ptr            := T_Ptr (X);
                             := T_Ptr (X);

         PC ::T_Class_Ptr := T_Class_Ptr (X);
         PC T_Class_Ptr := T_Class_Ptr (X);

   begin
   begin
     …
     …
   end Op;
   end Op;


                                                                                                    77
http://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
                                                                                            78
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
procedure Handle (A : in out Alert) is
        begin
           A.Time_Of_Arrival := Calendar.Clock;
           A.Cause           := Get_Cause (A);
           Log (A);
          Static
        end Handle;
                     always calls: procedure Log (A : Alert);
            Binding

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

          A.Technician := Assign_Technician;
        end Handle;
                                                                                     79
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
What if …
                             … 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;
                                                                                         80
http://libre.act-europe.fr       © ACT Europe under the GNU Free Documentation License
procedure Handle (A : in out Alert) is
        begin
           A.Time_Of_Arrival := Calendar.Clock;
           A.Cause           := Get_Cause (A);
           Log (Alert’Class (A));
        end Dynamic Binding
            Handle;
                    Redispatching

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

          A.Technician := Assign_Technician;
        end Handle;
                                                                                       81
http://libre.act-europe.fr     © ACT Europe under the GNU Free Documentation License
Dispatching 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                                                 82
http://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




                                                                                            83
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
In the Alert 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



                                                                                            84
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
Make Alert an abstract 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;
                                                                                                 85
http://libre.act-europe.fr               © ACT Europe under the GNU Free Documentation License
Cannot create objects of an
                       abstract type


                             type Alert is abstract tagged private;


                             A : Alert;                                      Compilation error
                                                                                Alert is an
                                                                               abstract type



                                                                                                  86
http://libre.act-europe.fr                © ACT Europe under the GNU Free Documentation License
Can have abstract 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;
                                                                                                 87
http://libre.act-europe.fr               © ACT Europe under the GNU Free Documentation License
Rules 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
                  package Alerts is
                      type Alert is abstract tagged private;
                     type Alert is abstract tagged private;
                     procedure Handle (A ::in out Alert);
                      procedure Handle (A in out Alert);
                      procedure Log (A ::Alert) is abstract;
                                          (A Alert) is abstract;
                     procedure Log

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

                             Must provide Log or make
                                Low_Alert abstract                                               88
http://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) is
        begin
           ...
           Log (A);       Compilation procedure Log (A : Alert);
                                               does not exist
                               error
        end Handle;



         procedure Handle (A : in out Alert) is
         begin
            ...
            Log (Alert’Class (A));       OK
         endDispatching
             Handle;
                             Call                                                           89
http://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




                                                                                            90
http://libre.act-europe.fr          © ACT Europe under the GNU Free Documentation License
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)



                                                                                          91
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
package Alerts is
     package Alerts is
         type Alert is abstract tagged private;
        type Alert is abstract tagged private;
        procedure Handle (A ::in out Alert);
         procedure Handle (A in out Alert);
         procedure Log (A ::Alert);
                             (A Alert);
        procedure Log
      private
     private
        ......
      end Alerts;
     end Alerts;


                    with Alerts; use Alerts;
                    with Alerts; use Alerts;
                    package Alerts.Medium is
                    package Alerts.Medium is
                         type Medium_Alert is new Alert with private;
                        type Medium_Alert is new Alert with private;
                        procedure Handle (A ::in out Alert);
                         procedure Handle (A in out Alert);
                         procedure Log (A ::Alert);
                                             (A Alert);
                        procedure Log
                    private
                    private
                        ......
                    end Alerts.Medium;
                    end Alerts.Medium;                                                    92
http://libre.act-europe.fr        © ACT Europe under the GNU Free Documentation License
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
                                                                                     93
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
Example

           • Suppose you want to add a new functionality

           • that behaves DIFFERENTLY for all alert types




                                                                                     94
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
package Alerts is
     package Alerts is
        type Alert is abstract tagged private;
       type Alert is abstract tagged private;
       procedure New_Functionality (A :: Alert);
        procedure New_Functionality (A Alert);
       ......

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

   with Alerts; use Alerts;
   with Alerts; use Alerts;
   package Alerts.High is
   package Alerts.High is
      type High_Alert is new Alert with private;
     type High_Alert is new Alert with private;
     procedure New_Functionality (A :: High_Alert);
      procedure New_Functionality (A High_Alert);
     ......                                                                                     95
http://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




                                                                                            96
http://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
                                                                                     97
http://libre.act-europe.fr   © ACT Europe under the GNU Free Documentation License
New functionalities can be
                                                     factored in few tagged types
   Data
    type
  changes




                                                                                ??
                                      Use
                                     Object
                           Use
                                    Oriented
                          Object
                         Oriented




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

Más contenido relacionado

Más de Gneuromante canalada.org (7)

Programming Languages and Software Construction
Programming Languages and Software ConstructionProgramming Languages and Software Construction
Programming Languages and Software Construction
 
Ada 95 - Introduction
Ada 95 - IntroductionAda 95 - Introduction
Ada 95 - Introduction
 
Ada 95 - Generics
Ada 95 - GenericsAda 95 - Generics
Ada 95 - Generics
 
Developing Software That Matters I
Developing Software That Matters IDeveloping Software That Matters I
Developing Software That Matters I
 
Developing Software that Matters II
Developing Software that Matters IIDeveloping Software that Matters II
Developing Software that Matters II
 
Introduction to Ada
Introduction to AdaIntroduction to Ada
Introduction to Ada
 
Ada in Debian GNU/Linux
Ada in Debian GNU/LinuxAda in Debian GNU/Linux
Ada in Debian GNU/Linux
 

Último

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 

Último (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 

Ada 95 - Object orientation

  • 1. Franco Gasperoni gasperon@act-europe.fr http://libre.act-europe.fr 1 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 2. Copyright 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 2 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 3. 3 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 4. When creating a new system you must identify its ... • Data types (what kind of data will be manipulated) • Functionalities (what kind of manipulations are allowed) 4 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 5. Software System Organization • Around its functionalities (functionality-oriented / structured programming) • around its data types (object-oriented programming) 5 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 6. • Object-Oriented Organization – inheritance (simple) – polymorphism – abstract types & subprograms – modifying an OO system – when to use OO organization 6 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 7. Often types have some but not all properties in common... • Create completely different types • Use variant programming to factor commonalties • Use inheritance 7 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 8. Alert Alert Time_Of_Arrival Time_Of_Arrival Cause Cause Handle () Handle () Log () Log () Medium_Alert High_Alert Medium_Alert High_Alert Low_Alert Low_Alert Time_Of_Arrival Time_Of_Arrival Time_Of_Arrival Time_Of_Arrival Time_Of_Arrival Time_Of_Arrival Cause Cause Cause Cause Cause Cause Handle () Handle () Log () Handle () Log () Handle () Handle () Handle () Log () Log () Log () Log () inherited 8 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 9. Alert Alert Time_Of_Arrival Time_Of_Arrival Cause Cause Handle () Handle () Log () Log () Medium_Alert High_Alert Medium_Alert High_Alert Low_Alert Low_Alert Time_Of_Arrival Time_Of_Arrival Time_Of_Arrival Time_Of_Arrival Time_Of_Arrival Time_Of_Arrival Cause Cause Cause Cause Cause Cause Handle () Handle () Log () Log () Handle () Handle () Handle () Handle () Log () Log () Log () Log () 9 redefined http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 10. Alert Alert Time_Of_Arrival Time_Of_Arrival Cause Cause Handle () Handle () Log () Log () Medium_Alert High_Alert Medium_Alert High_Alert Low_Alert Low_Alert Time_Of_Arrival Time_Of_Arrival Time_Of_Arrival Time_Of_Arrival Time_Of_Arrival Time_Of_Arrival Cause Cause Cause Cause Cause Cause Engineer Technician Engineer Technician Handle () Ring_Alarm_At Handle () Ring_Alarm_At Log () Handle () Log () Handle () Handle () Handle () Log () Log () Log () Log () Set_Alarm Set_Alarm 10 added http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 11. Alert • Low_Alert • Medium_Alert • High_Alert Are 4 Different Types 11 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 12. with …; Alert is a tagged type 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); Primitive ... operations end Alerts; (methods) 12 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 13. package Alerts is type Alert is tagged record Time_Of_Arrival : Calendar.Time; Cause : String (1 .. 200); inherited end record; procedure Handle (A : in out Alert); procedure Log (A : Alert); type Low_Alert is new Alert with null record; ... Derived type end Alerts; inherits everything Low_Alert is a tagged type by default derived from Alert 13 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 14. package Alerts is type Alert is tagged record Time_Of_Arrival : Calendar.Time; Cause : String (1 .. 200); inherited end record; procedure Handle (A : in out Alert); procedure Log (A : Alert); type Medium_Alert is new Alert with record Technician : Person; added end record; procedure Handle (A : in out Medium_Alert); ... redefined 14 end Alerts; http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 15. package Alerts is type Alert is tagged record Time_Of_Arrival : Calendar.Time; Cause : String (1 .. 200); inherited end record; procedure Handle (A : in out Alert); procedure Log (A : Alert); type High_Alert is new Alert with record added 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); redefined 15 end Alerts; http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 16. Attributes (record fields) • Are always inherited • Can never be redefined or deleted • You can add new attributes 16 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 17. Inherited Attributes 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 := …; OK A_L . Time_Of_Arrival := …; A_M . Time_Of_Arrival := …; A_H . Time_Of_Arrival := …; end Client; 17 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 18. Added Attributes with Alerts; use Alerts; procedure Client is A : Alert; A_L : Low_Alert; A_M : Medium_Alert; Compilation A_H : High_Alert; Error Engineer begin defined only A . Engineer := …; for A_L . Engineer := …; High_Alert A_M . Engineer := …; A_H . Engineer := …; end Client; 18 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 19. 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 19 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 20. Inherited & with Alerts; use Alerts; with Alerts; use Alerts; procedure Client is procedure Client is Redefined A ::Alert; Alert; A Operations A_L ::Low_Alert; A_L Low_Alert; A_M ::Medium_Alert; A_M Medium_Alert; A_H ::High_Alert; A_H High_Alert; begin begin type Alert is tagged record ... end record; type Alert is tagged record ... end record; Handle (A); procedure Handle (A ::in out Alert); procedure Handle (A in out Alert); Handle (A); type Low_Alert is tagged record ... end record; type Low_Alert is tagged record ... end record; Handle (A_L); -- procedure Handle (A ::in out Low_Alert); -- procedure Handle (A in out Low_Alert); Handle (A_L); type Medium_Alert is new Alert with ... end record; type Medium_Alert is new Alert with ... end record; Handle (A_M); procedure Handle (A ::in out Medium_Alert); procedure Handle (A in out Medium_Alert); Handle (A_M); type High_Alert is new Alert with ... end record; type High_Alert is new Alert with ... end record; Handle (A_H); Handle (A_H); procedure Handle (A ::in out High_Alert);20 procedure Handle (A in out High_Alert); end Client; end Client; http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 21. Added Operations with Alerts; use Alerts; procedure Client is A : Alert; A_L : Low_Alert; A_M : Medium_Alert; Compilation A_H : High_Alert; Error begin Set_Alarm Set_Alarm (A, 1800); defined only Set_Alarm (A_L, 1800); for Set_Alarm (A_M, 1800); High_Alert Set_Alarm (A_H, 1800); end Client; 21 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 22. Variant Programming procedure Handle (A : in out Alert) is begin 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; 22 end Handle; http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 23. Programming with Inheritance procedure Handle (…) is procedure Handle (…) is begin begin procedure Handle (A : in out Alert) is A.Time_Of_Arrival := …; A.Time_Of_Arrival := …; A.Cause := …; A.Cause := …; begin Log (A); Log (A); A.Time_Of_Arrival := Calendar.Clock; case A.P is case A.P is A.Cause := Get_Cause (A); when Low => => when Low null; null; Log (A); when Medium => when Medium => A.Technician := …; end Handle; A.Technician := …; when High => when High => A.Engineer := …; A.Engineer := …; Set_Alarm (A, ...); Set_Alarm (A, ...); procedure Handle (A : in out Medium_Alert) is end case; end case; end Handle; begin end Handle; Handle (Alert (A)); -- First handle as plain Alert A.Technician := Assign_Technician; end Handle; 23 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 24. procedure Handle (…) is procedure Handle (…) is begin begin procedure Handle (A : in out Alert) is A.Time_Of_Arrival := …; A.Time_Of_Arrival := …; A.Cause := …; A.Cause := …; begin Log (A); Log (A); A.Time_Of_Arrival := Calendar.Clock; case A.P is case A.P is A.Cause := Get_Cause (A); when Low => => when Low null; null; Log (A); when Medium => when Medium => A.Technician := …; end Handle; A.Technician := …; when High => when High => A.Engineer := …; A.Engineer := …; Set_Alarm (A, ...); Set_Alarm (A, ...); procedure Handle (A : in out High_Alert) is end case; end case; end Handle; begin end Handle; Handle (Alert (A)); -- First handle as plain Alert A.Engineer := Assign_Engineer; Set_Alarm (A, Wait => 1800); 24 end Handle; http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 25. Centralized 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 25 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 26. • Object-Oriented Organization – inheritance (simple) • encapsulation & inheritance – polymorphism – abstract types & subprograms – modifying an OO system – when to use OO organization 26 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 27. 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; 27 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 28. Two possibilities to extend Alert • Child package to access fields – Time_Of_Arrival – Cause • Normal package if you do not need to access – Time_Of_Arrival – Cause 28 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 29. Child Package with Alerts; use Alerts; with Alerts; use Alerts; with …; with …; package Alerts.Medium is package Alerts.Medium is type Medium_Alert is new Alert with private; type Medium_Alert is new Alert with private; procedure Handle (A ::in out Medium_Alert); procedure Handle (A in out Medium_Alert); private private type Medium_Alert is new Alert with record type Medium_Alert is new Alert with record Technician :: Person; Technician Person; end record; end record; end Alerts.Medium; end Alerts.Medium; package body Alerts.Medium is package body Alerts.Medium is Can access fields - Time_Of_Arrival - Cause end Alerts.Medium; end Alerts.Medium; 29 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 30. Regular Package with Alerts; use Alerts; with Alerts; use Alerts; package High_Importance is package High_Importance is type High_Alert is new Alert with private; type High_Alert is new Alert with private; procedure Handle (A ::in out High_Alert); procedure Handle (A in out High_Alert); procedure Set_Alarm (A ::in out High_Alert; W ::Duration); procedure Set_Alarm (A in out High_Alert; W Duration); private private type High_Alert is new Alert with record type High_Alert is new Alert with record Engineer ::Person; Engineer Person; Ring_Alarm_At ::Calendar.Time; Ring_Alarm_At Calendar.Time; end record; end record; end High_Importance; package body High_Importance is end High_Importance; package body High_Importance is Cannot access fields - Time_Of_Arrival end High_Importance; - Cause end High_Importance; 30 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 31. Important 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) 31 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 32. • Object-Oriented Organization – inheritance (simple) – polymorphism – abstract types & subprograms – modifying an OO system – when to use OO organization 32 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 33. Handling an Alert • You have a Get_Alert routine • Connected to the sensors in the factory • Collects the alerts with Alerts; use Alerts; with Alerts; use Alerts; function Get_Alert return ???; function Get_Alert return ???; 33 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 34. Objective • Be able to mimic the code used in the variant programming case with Alerts; use Alerts; with Alerts; use Alerts; with Get_Alert; with Get_Alert; procedure Process_Alerts is procedure Process_Alerts is begin begin loop -- infinite loop loop -- infinite loop Handle (Get_Alert); Handle (Get_Alert); end loop; end loop; end Process_Alerts; end Process_Alerts; 34 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 35. 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; type Alert is tagged record ... end record; procedure Handle (A :: in out Alert); procedure Handle (A in out Alert); type Low_Alert is tagged record ... end record; type Low_Alert is tagged record ... end record; -- procedure Handle (A ::in out Low_Alert); -- procedure Handle (A in out Low_Alert); type Medium_Alert is new Alert with ... end record; type Medium_Alert is new Alert with ... end record; procedure Handle (A :: in out Medium_Alert); procedure Handle (A in out Medium_Alert); type High_Alert is new Alert with ... end record; type High_Alert is new Alert with ... end record; procedure Handle (A :: in out High_Alert); procedure Handle (A in out High_Alert); 35 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 36. Polymorphism • Variables can name objects with common properties but different types • Can select dynamically the right operation for the underlying object 36 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 37. Medium_Alert High_Alert Low_Alert Handle() Handle() Handle() Log() Log() Log() Private Private Set_Alarm() stuff stuff Private stuff The exact same interface because they all derive from type Alert 37 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 38. Inheritance & 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 38 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 39. Idea: select the operation dynamically Obj ::some unknown type derived from Alert; Obj some unknown type derived from Alert; Handle (Obj); Handle (Obj); Handle() Handle() ? ? 39 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 40. • 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 40 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 41. Generally Speaking ... For any tagged type T T’Class denotes ANY type D derived from T ∀T derives from T ' Class = {D | D   → T }  41 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 42. ∀T UValues( D) Values(T ' Class ) = D →T 42 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 43. Inheritance Theorem For all type D For all type D derived from derived from T T ⊆ set of operations set of operations set of operations set of operations implemented implemented implemented implemented for objects of type for objects of type for objects of type for objects of type D T D T 43 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 44. ∀ D ∈ T ' Class ∃ Op (X : T; ...) ⇒ ∃ Op (X : D; ...) Operations (T ' Class ) = Operations (T ) 44 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 45. CLASS = Values + Operations + Inheritance 45 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 46. T’Class in 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 46 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 47. Class-Wide Objects A_L1 :: Low_Alert; A_L1 Low_Alert; A_L2 :: Low_Alert; A_L2 Low_Alert; A_M ::Medium_Alert; A_M Medium_Alert; A_H ::High_Alert; A_H High_Alert; V1 ::Alert’Class := A_L1; V1 Alert’Class := A_L1; V2 ::Alert’Class := A_M; V2 Alert’Class := A_M; V3 ::Alert’Class := A_H; V3 Alert’Class := A_H; OK - the type of the object named by V1 := A_L2; V1 := A_L2; V1 and A_L2 is the same (a Low_Alert) A_L1 := Low_Alert (V1); OK - V1 names an object A_L1 := Low_Alert (V1); of type Low_Alert 47 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 48. A_L1 :: Low_Alert; A_L1 Low_Alert; A_L2 :: Low_Alert; A_L2 Low_Alert; A_M ::Medium_Alert; A_M Medium_Alert; A_H ::High_Alert; A_H High_Alert; V1 ::Alert’Class := A_L1; V1 Alert’Class := A_L1; V2 ::Alert’Class := A_M; V2 Alert’Class := A_M; V3 ::Alert’Class := A_H; V3 Alert’Class := A_H; Constraint_Error objects named by the 2 variables V3 := A_L1; V3 := A_L1; have different types A_L1 := Low_Alert (V2); A_L1 := Low_Alert (V2); 48 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 49. Compilation Error objects of a V1 ::Alert’Class; V1 Alert’Class; class-wide type MUST be initialized 49 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 50. Class-Wide Access type All_Alert_Ptr is access all Alert’Class; type All_Alert_Ptr is access all Alert’Class; Ptr_1 :: All_Alert_Ptr; -- no need to default initialize Ptr_1 All_Alert_Ptr; -- no need to default initialize Ptr_1 := new Alert; Ptr_1 := new Alert; Ptr_1 := new Low_Alert’(Get_Time, Get_Cause); Ptr_1 := new Low_Alert’(Get_Time, Get_Cause); Ptr_1 := new Medium_Alert; Ptr_1 := new Medium_Alert; Ptr_1 := new High_Alert; Ptr_1 := new High_Alert; OK 50 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 51. General access type to all conversions type All_Alert_Ptr is access all Alert’Class; type All_Alert_Ptr is access all Alert’Class; type Low_Alert_Ptr is access all Low_Alert’Class; type Low_Alert_Ptr is access all Low_Alert’Class; Ptr_M :: Ptr_M All_Alert_Ptr; All_Alert_Ptr; Ptr_L :: Ptr_L Low_Alert_Ptr := new Low_Alert; Low_Alert_Ptr := new Low_Alert; Ptr_M := new High_Alert; Ptr_M := new High_Alert; Constraint_Error object pointed by Ptr_L := Low_Alert_Ptr (Ptr_M); Ptr_L := Low_Alert_Ptr (Ptr_M); Ptr_M must be in Low_Alert’Class 51 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 52. Class-Wide Membership Given a class-wide variable V : T’Class := …; you can ask whether V in D’Class for all type D derived from T 52 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 53. A_L ::Low_Alert; A_L Low_Alert; V1 ::Alert’Class := A_L; V1 Alert’Class := A_L; B ::Boolean; B Boolean; B := (V1 in Alert Class); B := (V1 in Alert ’’Class); True B := (V1 in Low_Alert Class); B := (V1 in Low_Alert ’’Class); True B := (V1 in Medium_Alert Class); B := (V1 in Medium_Alert ’’Class); False 53 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 54. Run Time Type Identification • 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 54 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 55. ‘Tag Attribute with Ada.Tags; use Ada.Tags; with Ada.Tags; use Ada.Tags; procedure Client is procedure Client is A_L1 ::Low_Alert; A_L1 Low_Alert; A_L2 ::Low_Alert; A_L2 Low_Alert; A_M ::Medium_Alert; A_M Medium_Alert; V1 ::Alert’Class := A_L1; V1 Alert’Class := A_L1; V2 ::Alert’Class := A_L2; V2 Alert’Class := A_L2; V3 ::Alert’Class := A_M; V3 Alert’Class := A_M; B ::Boolean; B Boolean; begin begin B := (V1 ’’Tag = V2 ’’Tag); True B := (V1 Tag = V2 Tag); False B := (V2 ’’Tag = V3 ’’Tag); B := (V2 Tag = V3 Tag); B := (V1 ’’Tag = Low_Alert ’’Tag); True B := (V1 Tag = Low_Alert Tag); 55 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 56. • 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 56 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 57. Handling an Alert • You have a Get_Alert routine • Connected to the sensors in the factory • Collects the alerts with Alerts; use Alerts; with Alerts; use Alerts; function Get_Alert return Alert’Class; function Get_Alert return Alert’Class; 57 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 58. Dynamic Dispatching with Alerts; use Alerts; with Alerts; use Alerts; with Get_Alert; with Get_Alert; procedure Process_Alerts is procedure Process_Alerts is begin begin loop -- infinite loop loop -- infinite loop declare declare A ::Alert’Class := Get_Alert; A Alert’Class := Get_Alert; begin begin Handle (A); -- could have written Handle (Get_Alert); Handle (A); -- could have written Handle (Get_Alert); end; end; end loop; end loop; end Process_Alerts; end Process_Alerts; Dispatching Call 58 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 59. Which Handle () is Called ? -- A’Tag = Alert’Tag A’Tag = Alert’Tag Handle (A: Alert) Handle (A: Alert) -- A’Tag = Low_Alert’Tag A’Tag = Low_Alert’Tag Handle (A: Low_Alert) Handle (A: Low_Alert) -- A’Tag = Medium_Alert’Tag A’Tag = Medium_Alert’Tag Handle (A: Medium_Alert) Handle (A: Medium_Alert) -- A’Tag = High_Alert’Tag A’Tag = High_Alert’Tag Handle (A: High_Alert) Handle (A: High_Alert) 59 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 60. Static vs Dynamic Binding STATIC BINDING = call known at compile time DYNAMIC BINDING = call known only at run time 60 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 61. How do you know if call Op (V, …) is dispatching ? - Type of V is T’Class for some tagged type T - Op is a primitive operation of T type T is … end record; procedure Op (P : T; …) (or function) procedure Op (P : in out T; …) procedure Op (P : access T; …) (or function) 61 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 62. AL ::Low_Alert; AL Low_Alert; Handle (AL); Handle (AL); Static A ::Alert’Class := Get_Alert; A Alert’Class := Get_Alert; Binding Handle (A); Handle (A); Dynamic Binding A ::High_Alert’Class := …; A High_Alert’Class := …; Handle (A); Handle (A); Dynamic Binding 62 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 63. type Low_Alert_Ptr is access all Low_Alert; type Low_Alert_Ptr is access all Low_Alert; Ptr ::Low_Alert_Ptr := …; Ptr Low_Alert_Ptr := …; Handle ((Ptr.all); Handle Ptr.all); Static Binding type Low_Alert_Class_Ptr is access all Low_Alert’Class; type Low_Alert_Class_Ptr is access all Low_Alert’Class; Ptr ::Low_Alert_Class_Ptr := …; Ptr Low_Alert_Class_Ptr := …; Handle ((Ptr.all); Handle Ptr.all); Dynamic Binding 63 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 64. Where is the magic ? A ::Alert’Class := Get_Alert; A Alert’Class := Get_Alert; Handle (A); Handle (A); Dynamic Binding ? ? Handle() Handle() Low_Alert High_Alert 64 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 65. Tables of pointers to primitive operations 1 1 Handle Handle Handle 1 2 2 2 Log 3 Set_Alarm ’Tag is a pointer ’Tag ’Tag ’Tag Time_Of_Arrival Time_Of_Arrival Time_Of_Arrival Cause Cause Cause Engineer Technician Ring_Alarm_At High_Alert Medium_Alert Low_Alert65 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 66. A ::Alert’Class := Get_Alert; A Alert’Class := Get_Alert; Handle (A); Handle (A); indirect call to operation pointed by A’Tag (1) A ::Alert’Class := Get_Alert; A Alert’Class := Get_Alert; Log (A); Log (A); indirect call to operation pointed by A’Tag (2) 66 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 67. Can we DOUBLE dispatch ? type Base is tagged null record; type Base is tagged null record; procedure Op (X ::Base; Y ::Base); procedure Op (X Base; Y Base); type Deriv is new Base with null record; type Deriv is new Base with null record; procedure Op (X ::Deriv; Y ::Deriv); procedure Op (X Deriv; Y Deriv); V1 ::Base '' Class := …; V1 Base Class := …; V2 ::Base '' Class := …; V2 Base Class := …; Op (V1, V2); Op (V1, V2); Dynamic If V1'Tag /= V2'Tag raises Constraint_Error Binding 67 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 68. What about ... type T1 is tagged null record; type T1 is tagged null record; type T2 is tagged null record; type T2 is tagged null record; procedure Op (X ::T1; Y ::T2); procedure Op (X T1; Y T2); Compilation Error operation can be dispatching in only one type 68 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 69. • Object-Oriented Organization – inheritance (simple) – polymorphism • tags & class wide types • dynamic dispatching • using access parameters skip • redispatching – abstract types & subprograms – modifying an OO system – when to use OO organization 69 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 70. Sometime it is convenient to use pointers: access parameters package Alerts is type Alert is tagged private; procedure Handle (A : access Alert); procedure Log (A : access Alert); private type Alert is tagged record Time_Of_Arrival : Calendar.Time; Cause : String (1 .. 200); end record; end Alerts; 70 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 71. package Alerts.Medium is package Alerts.Medium is type Medium_Alert is new Alert with private; type Medium_Alert is new Alert with private; procedure Handle (A ::access Alert); procedure Handle (A access Alert); private private type Medium_Alert is new Alert with record type Medium_Alert is new Alert with record Technician :: Person; Technician Person; end record; end record; end Alerts.Medium; end Alerts.Medium; 71 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 72. Handling Alerts using access parameters type Alert_Class_Ptr is access all Alert ’’ Class; type Alert_Class_Ptr is access all Alert Class; function Get_Alert return Alert_Class_Ptr; function Get_Alert return Alert_Class_Ptr; declare declare A ::Alert_Class_Ptr := Get_Alert; A Alert_Class_Ptr := Get_Alert; begin begin Handle (A); -- could have written Handle (Get_Alert); Handle (A); -- could have written Handle (Get_Alert); Dynamic Binding 72 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 73. Polymorphism is powerful • Alerts are buffered in a linked list type Alert_Node; type Alert_Node; type Alert_List is access Alert_Node; type Alert_List is access Alert_Node; type Alert_Node is record type Alert_Node is record An_Alert ::Alert_Class_Ptr; An_Alert Alert_Class_Ptr; Next ::Alert_List; Alert_List; Next end record; end record; function Get_Alerts return Alert_List; function Get_Alerts return Alert_List; 73 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 74. • All the alerts are processed uniformly Ptr ::Alert_List := Get_Alerts; Ptr Alert_List := Get_Alerts; while Ptr /= null loop while Ptr /= null loop Handle (Ptr.An_Alert); Handle (Ptr.An_Alert); Ptr := Ptr.Next; Ptr := Ptr.Next; end loop; end loop; 74 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 75. Rules for access parameter conversions type T is tagged … end record; type T is tagged … end record; T is some tagged type type T_Ptr is access all T; type T_Ptr is access all T; procedure Op (X :: access T); procedure Op (X access T); P ::T_Ptr := …; P T_Ptr := …; implicit Op (P); Op (P); conversion Static Binding 75 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 76. type T is … end record; type T is … end record; type T_Class_Ptr is access all T’Class; type T_Class_Ptr is access all T’Class; procedure Op (X :: access T); procedure Op (X access T); PC ::T_Class_Ptr := ...; PC T_Class_Ptr := ...; Op (PC); Op (PC); Dynamic Binding 76 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 77. explicit conversion needed procedure Op (X :: access T) is procedure Op (X access T) is P ::T_Ptr P T_Ptr := T_Ptr (X); := T_Ptr (X); PC ::T_Class_Ptr := T_Class_Ptr (X); PC T_Class_Ptr := T_Class_Ptr (X); begin begin … … end Op; end Op; 77 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 78. • 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 78 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 79. procedure Handle (A : in out Alert) is begin A.Time_Of_Arrival := Calendar.Clock; A.Cause := Get_Cause (A); Log (A); Static end Handle; always calls: procedure Log (A : Alert); Binding procedure Handle (A : in out Medium_Alert) is begin Handle (Alert (A)); -- First handle as plain Alert A.Technician := Assign_Technician; end Handle; 79 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 80. What if … … 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; 80 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 81. procedure Handle (A : in out Alert) is begin A.Time_Of_Arrival := Calendar.Clock; A.Cause := Get_Cause (A); Log (Alert’Class (A)); end Dynamic Binding Handle; Redispatching procedure Handle (A : in out Medium_Alert) is begin Handle (Alert (A)); -- First handle as plain Alert A.Technician := Assign_Technician; end Handle; 81 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 82. Dispatching 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 82 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 83. • Object-Oriented Organization – inheritance (simple) – polymorphism – abstract types & subprograms – modifying an OO system – when to use OO organization 83 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 84. In the Alert 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 84 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 85. Make Alert an abstract 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; 85 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 86. Cannot create objects of an abstract type type Alert is abstract tagged private; A : Alert; Compilation error Alert is an abstract type 86 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 87. Can have abstract 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; 87 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 88. Rules 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 package Alerts is type Alert is abstract tagged private; type Alert is abstract tagged private; procedure Handle (A ::in out Alert); procedure Handle (A in out Alert); procedure Log (A ::Alert) is abstract; (A Alert) is abstract; procedure Log type Low_Alert is new Alert with private; type Low_Alert is new Alert with private; procedure Log (A ::Low_Alert); procedure Log (A Low_Alert); Must provide Log or make Low_Alert abstract 88 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 89. • Only dispatching calls to abstract routines allowed procedure Handle (A : in out Alert) is begin ... Log (A); Compilation procedure Log (A : Alert); does not exist error end Handle; procedure Handle (A : in out Alert) is begin ... Log (Alert’Class (A)); OK endDispatching Handle; Call 89 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 90. • Object-Oriented Organization – inheritance (simple) – polymorphism – abstract types & subprograms – modifying an OO system – when to use OO organization 90 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 91. 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) 91 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 92. package Alerts is package Alerts is type Alert is abstract tagged private; type Alert is abstract tagged private; procedure Handle (A ::in out Alert); procedure Handle (A in out Alert); procedure Log (A ::Alert); (A Alert); procedure Log private private ...... end Alerts; end Alerts; with Alerts; use Alerts; with Alerts; use Alerts; package Alerts.Medium is package Alerts.Medium is type Medium_Alert is new Alert with private; type Medium_Alert is new Alert with private; procedure Handle (A ::in out Alert); procedure Handle (A in out Alert); procedure Log (A ::Alert); (A Alert); procedure Log private private ...... end Alerts.Medium; end Alerts.Medium; 92 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 93. 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 93 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 94. Example • Suppose you want to add a new functionality • that behaves DIFFERENTLY for all alert types 94 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 95. package Alerts is package Alerts is type Alert is abstract tagged private; type Alert is abstract tagged private; procedure New_Functionality (A :: Alert); procedure New_Functionality (A Alert); ...... with Alerts; use Alerts; with Alerts; use Alerts; package Alerts.Medium is package Alerts.Medium is type Medium_Alert is new Alert with private; type Medium_Alert is new Alert with private; procedure New_Functionality (A ::Medium_Alert); procedure New_Functionality (A Medium_Alert); ...... with Alerts; use Alerts; with Alerts; use Alerts; package Alerts.High is package Alerts.High is type High_Alert is new Alert with private; type High_Alert is new Alert with private; procedure New_Functionality (A :: High_Alert); procedure New_Functionality (A High_Alert); ...... 95 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 96. • Object-Oriented Organization – inheritance (simple) – polymorphism – abstract types & subprograms – modifying an OO system – when to use OO organization 96 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 97. • 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 97 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License
  • 98. New functionalities can be factored in few tagged types Data type changes ?? Use Object Use Oriented Object Oriented use Functionality-Oriented 98 Functionality changes http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License