Information hiding and data abstraction are both implemented in Ada 83. It also provides for a hidden state in a object. Thus an Ada object's procedures can rightly be called methods, which we shall do here, although you will not find any mention of methods in the Ada literature. An object is implemented in Ada through a package.
Let us consider the robot object of Buzzard and Mudge [Buzzard, 1985]. We will only look at outlines for declaring the objects in listing (2.1).
(2.1)
package Robot istype RobotArm is limited private;
type ArmModel is (ASEA, PUMA);
type Position is array (1..4, 1..4) of Float;
--stores the location of the RobotArm in 3-space plus the 3-dimensional
--orientation of the gripper relative to the coordinates of the arm
procedure InitializeArm (x: out RobotArm;
Kind: in ArmModel);
--places initial values in the fields of Robots-Arm for its
--position in 3-space, status of its gripper as open or closed,
--and what kind it is
procedure Move (x: in out RobotArm; Destination: in position);
--Relocates the Robot to Destination
procedure Open (x: in out RobotArm);
--Opens the gripper
procedure Close (x: in out RobotArm);
function GetPosition (x: RobotArm) return Position;
--Returns the Robot's current position
private --Not visible outside the package
type RobotArms is record
Pos : Position; -- Robot position in 3-space
Open: Boolean; -- True if gripper is open
Kind : ArmModel; -- Arm model type
end record;
end Robot;
A Robot consists of a single arm that can Open or Close its gripper. A robot object will have values for Pos, Open, and Kind, as it state. Its methods to alter the state are InitializeArm, Move Open Close, and GetPosition. We will assume along with Buzzard and Mudge that the only two kinds supported by this packages are the ASEA and the PUMA, although more may be added. This specification may be all a user will see and can be compiled separately from either its body or a program using tha package.
That RobotArm is limited private does not mean that the user cannot see the structure of its type. The record fields names-Pos for Position, Open for the boolean indicating whether the gripper is open or shut, and kind indicating the robot's model-would be listed in the specification, but a user would have no acces to them except through the four procedures InitializeArm, Move, Open, Close, and the function GetPosition listed above. The state of this sample robot indicates where it is, whether its gripper is open or shut, and its model.
In the object-oriented extension to Turbo Pascal, the declaration would be as shown in listing (2.2):
(2.2)
unit Robot;
interface
type
ArmModel = (ASEA, PUMA);
Position = array [1..4, 1..4] of real;
Arm = record
Pos : Position;
Open : boolean;
Kind : ArmModel;
end;
RobotArm = object
A: Arm;
procedure Init (Kind: ArmModel);
procedure Move (Destination: Position);
procedure Open;
procedure Close;
function GetPosition: Position;
end;
implementation
...
end;
The declaration for an object looks very much like a record declaratio, and indeed it is. A pascal object is a record, with procedures and functions as well as data allowed as fields. Each of the procedures in the object RobotArm operators implicitly on the Arm field, A. If we declare:
MyASEARobot : RobotArm;
We can initialize it using MyASEARobot.Init (ASEA); move it with MyASEARobot.Move(...); etc. Although these are called procedures in pascal syntax, they are really methods. They can only be used with variables of type Arm, which have not been declared as part of an object of type RobotArm, and they can only be activated through the object's name, in this case, MyASEARobot.
Object Pascal has no facilities for restricting acces to RobotArm. A user can assign values to the fields of a variable of type Arm without using any of the object's methods. The implementors assumed that those programming in a object-oriented style would discipline themselves to use intances of objects only through the methods included in the object definition.
No comments:
Post a Comment