Summary
As mentioned in Chapter 1, abstraction in various forms seems to be the key to the development of programming languages. In this chapter we survey various aspects of abstraction with particular emphasis on the object oriented paradigm.
Packages and private types
In the previous chapter we declared a type for the manipulation of a buffer as follows
type Buffer is
record
Data: String(1 .. 80);
Start: Integer;
Finish: Integer;
end record;
in which the component Data actually holds the characters in the buffer and Start and Finish index the ends of the part of the buffer containing useful information. We also saw how the various components might be updated and read using normal assignment.
However, such direct assignment is often unwise since the user could inadvertently set inconsistent values into the components or read nonsense components of the array.
A much better approach is to create an Abstract Data Type (ADT) so that the user cannot see the internal details of the type but can only access it through various subprogram calls which define an appropriate protocol.
This can be done using a package containing a private type. Let us suppose that the protocol allows us to reload the buffer (possibly not completely full) and to read one character at a time. Consider the following
package Buffer_System is -- visible part
type Buffer is private;
procedure Load(B: out Buffer; S: in String);
procedure Get(B: in out Buffer; C: out Character);
private -- private part
Max: constant Integer := 80;
type Buffer is
record
Data: String(1 .. Max);
Start: Integer := 1;
Finish: Integer := 0;
end record;
end Buffer_System;
package body Buffer_System is -- package body
procedure Load(B: out Buffer; S: in String) is
begin
B.Start := 1;
B.Finish := S'Length;
B.Data(B.Start .. B.Finish) := S;
end Load;
procedure Get(B: in out Buffer; C: out Character) is
begin
C := B.Data(B.Start);
B.Start := B.Start + 1;
end Get;
end Buffer_System;
Note how the package comes in two parts, the specification and the body. Basically the specification describes the interface to other parts of the program and the body gives implementation details.
With this formulation the client can only access the information in the visible part of the specification which is the bit before the word private.
- Type
- Chapter
- Information
- Programming in Ada 2012 , pp. 27 - 46Publisher: Cambridge University PressPrint publication year: 2014