Ada 95 Quality and Style Guide Chapter 8

Chapter 8: Reusability - TOC - 8.4 INDEPENDENCE

8.4.8 Tagged Type Hierarchies


  • Consider using hierarchies of tagged types to promote generalization of software for reuse.
  • Consider using a tagged type hierarchy to decouple a generalized algorithm from the details of dependency on specific types.

  • example

    with Wage_Info;
    package Personnel is
       type Employee is abstract tagged limited private;
       type Employee_Ptr is access all Employee'Class;
       procedure Compute_Wage (E : Employee) is abstract;
       type Employee is tagged limited record
          Name  : ...;
          SSN   : ... ;
          Rates : Wage_Info.Tax_Info;
       end record;
    end Personnel;
    package Personnel.Part_Time is
       type Part_Timer is new Employee with private;
       procedure Compute_Wage (E : Part_Timer);
    end Personnel.Part_Time;
    package Personnel.Full_Time is
       type Full_Timer is new Employee with private;
       procedure Compute_Wage (E : Full_Timer);
    end Personnel.Full_Time;

    Given the following array declaration:

    type Employee_List is array (Positive range <>) of Personnel.Employee_Ptr;

    you can write a procedure that computes the wage of each employee, regardless of the different types of employees that you create. The Employee_List consists of an array of pointers to the various kinds of employees, each of which has an individual Compute_Wage procedure. (The primitive Compute_Wage is declared as an abstract procedure and, therefore, must be overridden by all descendants.) You will not need to modify the payroll code as you specialize the kinds of employees:

    procedure Compute_Payroll (Who : Employee_List) is
    begin -- Compute_Payroll
       for E in Who'Range loop
          Compute_Wage (Who(E).all);
       end loop;
    end Compute_Payroll;


    The general algorithm can depend polymorphically on objects of the class-wide type of the root tagged type without caring what specialized types are derived from the root type. The generalized algorithm does not need to be changed if additional types are added to the type hierarchy. See also Guideline 5.4.2. Furthermore, the child package hierarchy then mirrors the inheritance hierarchy.

    A general root tagged type can define the common properties and have common operations for a hierarchy of more specific types. Software that depends only on this root type will be general, in that it can be used with objects of any of the more specific types. Further, the general algorithms of clients of the root type do not have to be changed as more specific types are added to the type hierarchy. This is a particularly effective way to organize object-oriented software for reuse.

    Separating the hierarchy of derived tagged types into individual packages enhances reusability by reducing the number of items in package interfaces. It also allows you to with only the capabilities needed.

    See also Guidelines 9.2, 9.3.1, 9.3.5, and 9.4.1.

    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    Appendix References Bibliography