Introducing (in this Chapter):
- The object name as a reference
- Creating and freeing an object: Constructors and destructors
- The need for memory management: memory leakage and dangling references
Introduction
This chapter continues the theme of Part 3, which introduced a programmer defined class and object and contrasted this to the RAD definitions. In this chapter we look at creating and freeing an object and explore the concept of a constructor and a destructor. Well look at Get and Set access methods and at defining properties.(Properties provide an alternative to access methods.) In both Delphi, an objects name is not the object itself: it is a reference to the object which the program automatically dereferences.
When writing programs similar problems and solutions seem to come up repeatedly. This leads to the question of how to take advantage of past experience when writing new programs. One way to do this is to create libraries of code for common operations and to use these wherever appropriate in new programming projects. There are also other situations where we cant reuse actual code, but where there are concepts that we have used previously with success and we would like to reuse these concepts.
Being able to reuse tried and tested concepts is useful in many other fields in addition to programming. In architecture, for example, there are similar problems that arise time and again in designing buildings. In the late 1970s a group of architects led by Christopher Alexander introduced the concept of Patterns. By the mid 1990s this concept had been adapted to programming problems (eg Gamma et al (1995), Buschmann et al (1996)).
Patterns are designed to suggest effective solutions to recurring problems and to encapsulate expert, accumulated programming experience. This chapter presents our first pattern, Immutable, which allows us to create an object whose data values cannot be changed once the object has been created.
A programming example
We use several variations on a simple program to illustrate the principles this chapter covers. Well create a class, TClient, with data fields CName for a clients name and AccNo for an account number. Well create the object, set and read its data and free it in various permutations in the examples that follow.Example 4.1 Access methods
To provide encapsulation, classes usually have private data fields with public access methods, as illustrated by the TItem class in the previous chapter. The access methods are often called Getters and Setters, or Get and Set methods, or Accessors and Mutators, or, collectively, access methods, and provide the means for the rest of the program to store, read and manipulate the values of the objects private data.Ex 4.1 step 1 Implementing access methods
Start a new application. Add a second unit to it through the menu sequence File | New | Unit. Save this additional unit as ClientU.pas and then enter the following code. 1 unit ClientU;
2 interface
3 type
4 TClient = class (TObject)
5 private
6 FAccNo: string;
7 FCName: string;
8 public
9 function GetAccNo: string;
10 function GetCName: string;
11 procedure SetAccNo(const AnAccNo: string) ;
12 procedure SetCName(const ACName: string) ;
13 end;
14 implementation
15 { TClient }
16 function TClient.GetAccNo: string;
17 begin
18 GetAccNo := FAccNo;
19 end;
20 function TClient.GetCName: string;
21 begin
22 GetCName := FCName;
23 end;
24 procedure TClient.SetAccNo(const AnAccNo: string) ;
25 begin
26 FAccNo := AnAccNo;
27 end;
281 procedure TClient.SetCName(const ACName: string) ;
29 begin
30 FCName := ACName;
31 end;
32 end.
As is normal in OO, we keep the data private (lines 57). To provide access to the data, the access methods are made public (lines 812). Getters (lines 910) are usually functions since they return a value through their name. Setters (lines 1112) are usually procedures since no return value is needed, and their incoming value is provided by a parameter.
This unit illustrates a number of Delphi conventions. A classs data field is preceded by an F , and so the data fields are called FCName and FAccNo (lines 67). Parameters are often preceded by A or An , and here we have ACName and AnAccNo (lines 12, 11).
A Get method is preceded by Get (lines 910) and a Set method by Set (lines 1112). These prefixes all modify the base names CName and AccNo to provide a consistent notation that simplifies reading the code.

