Ada 95 Quality and Style Guide | Chapter 5 |
5.4.3 Heterogeneous Polymorphic Dataguideline
Use access types to class-wide types to implement heterogeneous polymorphic data structures.
Use tagged types and type extension rather than variant records (in combination with enumeration types and case statements). example
An array of type Employee_List can contain pointers to part-time and full-time employees (and possibly other kinds of employees in the future):
----------------------------------------------------------------------------------- package Personnel is type Employee is tagged limited private; type Reference is access all Employee'Class; ... private ... end Personnel; ----------------------------------------------------------------------------------- with Personnel; package Part_Time_Staff is type Part_Time_Employee is new Personnel.Employee with record ... end record; ... end Part_Time_Staff; ----------------------------------------------------------------------------------- with Personnel; package Full_Time_Staff is type Full_Time_Employee is new Personnel.Employee with record ... end record; ... end Full_Time_Staff; ----------------------------------------------------------------------------------- ... type Employee_List is array (Positive range <>) of Personnel.Reference; Current_Employees : Employee_List (1..10); ... Current_Employees(1) := new Full_Time_Staff.Full_Time_Employee; Current_Employees(2) := new Part_Time_Staff.Part_Time_Employee; ...rationale
Polymorphism is a means of factoring out the differences among a collection of abstractions so that programs may be written in terms of the common properties. Polymorphism allows the different objects in a heterogeneous data structure to be treated the same way, based on dispatching operations defined on the root tagged type. This eliminates the need for case statements to select the processing required for each specific type. Guideline 5.6.3 discusses the maintenance impact of using case statements.
Enumeration types, variant records, and case statements are hard to maintain because the expertise on a given variant of the data type tends to be spread all over the program. When you create a tagged type hierarchy (tagged types and type extension), you can avoid the variant records, case statement, and single enumeration type that only supports the variant record discriminant. Moreover, you localize the "expertise" about the variant within the data structure by having all the corresponding primitives for a single operation call common "operation-specific" code.
See also Guideline 9.2.1 for a more detailed discussion of tagged types.
exceptions
In some instances, you may want to use a variant record approach to organize modularity around operations. For graphic output, for example, you may find it more maintainable to use variant records. You must make the tradeoff of whether adding a new operation will be less work than adding a new variant.
< Previous Page | Search | Contents | Index | Next Page > |
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC |
Appendix | References | Bibliography |