Introducing (in this Chapter):
- The concept of Patterns in object orientation
- The Immutable Pattern
- Creating a list of object references
- Homework Problems
- Part download: PDF, Code, Homework Solutions
Example 4.6 Object names as references
So far in this chapter we have looked at ways of accessing objects data. In summary, this data is declared as private and so is encapsulated: we provide and control access through access methods and properties.In this example we look at accessing the objects themselves. As we have already seen, an objects name is a reference to the object. Whenever we use the name of an object, Delphi (and Java and C# too) automatically dereferences the name to get the objects address. Often we dont need to bother about the dereferencing. But it becomes important at times, for example when creating an association between two objects, and we look at some of the implications of a reference model in this example, which extends example 4.5. It creates a series of Client objects, adds a reference to each object to a list, in this case a TListBox component, and displays the contents of each object by dereferencing the matching item in the list.
You may wish either to edit the program from example 4.5. Otherwise, to start a new application, build the user interface, and add unit ClientU from example 4.5 to the new project.
Ex 4.6 step 1 Creating and adding objects to a list
Well use btnCreates OnClick event handler to create an object of type TClient (line 38 below) and then add a reference to this object to the ListBox (line 40).1 unit ObjAccessU;Note that the NewClient reference is no longer part of the user interface (as in example 4.1 step 2 lines 1819) and is instead declared locally to the event handler (lines 2728 above). This also means that the global uses clause reference to ClientU (example 4.1 step 2 line 5) can move to a local uses clause (line 23 above), thus restricting visibility into ClientU.
2 interface
{ Standard RAD interface declaration }
22 implementation
23 uses ClientU; // Move from global uses clause
24 {$R *.dfm}
25 {TfrmAccessObject}
26 procedure TfrmAccessObject.btnCreateClick(Sender: TObject) ;
27 var
28 NewClient: TClient;
29 Dummy: integer;
30 begin
31 // Exit if input invalid
32 if (edtCName.Text = '') or (not TryStrToInt(edtAccNo.Text, Dummy)) then
33 begin
34 ShowMessage ('Please enter a valid name and/or number') ;
35 Exit;
36 end;
37 NewClient := TClient.Create(edtCName.Text, edtAccNo.Text) ;
38 // Add object to ListBox and create an additional reference
39 lstClients.AddItem(NewClient.CName, NewClient) ;
40 edtCName.Clear;
41 edtAccNo.Clear;
42 edtCName.SetFocus;
43 end;
We mentioned earlier that TClient assumes that the initialisation values it receives for CName and AccNo are valid. The user interface therefore takes the responsibility of checking that the value entered by the user for the clients name is not an empty string (line 32) and that the account number is a valid integer (line 33 refer to online Help if you are unfamiliar with the TryIntToStr function).
Line 40 introduces a ListBox method, AddItem, that may be new. We often use the Add method to add a string to a ListBox. AddItem allows us to add a string and an associated object reference to a ListBox. If you consult the online Help you will see that it is declared as:
procedure AddItem(Item: String; AObject: TObject) ;
So in line 40 we add the clients name as a string to the ListBox and we also add a reference to the object we have just created by passing the name we assigned in line 38. Well use this object reference in the next step. When this method terminates, it loses the NewClient reference since this is a locally declared variable (lines 2728). The only reference we now have to this object is the reference stored in the ListBox by line 40.


