Nested Records and Composite Objects
Composite Objects and Class Line
-
Many times when modeling a system we must define data values that are
made up of fields that are other objects.
- Example: A line segment is modeled using a two Pairs:
class Pair{
private int x;
private int y;
...
}
class Line{
private Pair start;
private Pair end;
...
}
...
// CLIENT
Line myLine = new Line(new Pair(1, 2), new Pair(3, 4));
myLine.getStart().getX();
myLine.getEnd().getX();
We call these composite objects.
In OO modeling we think of this as the composition relationship.
In class based languages we model this with classes
whose fields are other objects.
Type Line: A Composite Object in Ada
- Records with fields of record type
- Example: Type Line
type Pair is record
x: Integer;
y: Integer;
end record;
type Line is record
start: Pair;
end: Pair;
end record;
myLine: Line;
...
begin
myLine.start.x := 1;
myLine.start.y := 2;
put(myLine.start);
myLine.end := (3, 4);
put(myLine.end);
Composite Objects: Reference and Value Semantics
-
In a language that has reference semantics (eg in java)
the fields of a composite object are actually references
to other objects.
-
Sometimes these structures can be fairly complex with lots of
pointers
to follow to get to a value.
- Notice the difference between what is in a Line object in Java
and Ada
- Java: Two pointers
- Ada: Four integers
- Notation for access is identical, but memory allocation and
actions required for data access are significantly different
- In java, composite objects always have reference
semantics (ie have fields that are pointers).
- In Ada and C++, composite objects can have either
reference or value semantics (ie the fields can be either
values or pointers).
An Array of Lines
- What does an array of lines look like:
- Java:
// Declaration and allocation of the array
Line[] myArray = new Line[3];
// Initialize
...
- Ada:
-- Declaration of the array type
type LineArray is array(1..3) of Line;
-- Declaration and allocation of the array
myArray: LineArray;
-- Initialize
...
- Consider both syntax and memory allocation of myArray
- Consider both syntax and actions of accessing myArray
Another Example Array of Records
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Text_IO; use Ada.Text_IO;
procedure tryArrayOfRecords is
type Triple is record
x: Integer;
y: Integer;
z: Integer;
end record;
procedure putTriple(p: Triple) is
begin
put(p.x); put(p.y); put(p.z); new_line;
end putTriple;
type TripleArray is array (1 .. 3) of Triple;
theTriples1: TripleArray;
theTriples2: TripleArray;
t1: Triple;
begin
theTriples1 := (3 => (3, 3, 3),
others => (y => 2, others => 1));
for i in theTriples1'Range loop
putTriple( theTriples1(i) );
end loop;
new_line;
t1 := theTriples1(2);
t1.x := 9;
putTriple(t1);
putTriple(theTriples1(2));
new_line;
theTriples1(2).x := 99;
putTriple(theTriples1(2));
new_line;
theTriples2 := theTriples1;
for i in theTriples2'Range loop
putTriple( theTriples2(i) );
end loop;
end tryArrayOfRecords;
-- OUTPUT:
1 2 1
1 2 1
3 3 3
9 2 1
1 2 1
99 2 1
1 2 1
99 2 1
3 3 3
A Type for Words
- How would we design a type for Words
- Let a word be a sequence of non-blank characters (ie a
special kind of String)
- A Word class in Java
class Word{
private String theLetters
private ...
public int getLength()
...
}
What would the Word type look like in Ada
Question: Does this work?
w1, w2: Word
...
if w1 = w2 then
...
This is a record containing an array
Question: How can we provide an encapsulated type for a
client?
Later we will create a Package for type Word
An Array of Words
- A problem to solve: count the number of distinct words in
the input (assume one word per line)
- What structure would prove useful?