1. Home
  2. Computing & Technology
  3. Delphi Programming

Association and Composition - Delphi OOP Part 9 - Chapter 19

By Zarko Gajic, About.com

Ex 9.1 step 3 A driver program

These two classes and the way they cooperate with each other may seem a little obscure in isolation. So to see how they interact, in the first unit of the project we’ll write a program that creates a TCustomer and a TSale class:
unit Comp_AssocDemoU;

interface

uses
   CustomerU, SaleU,
   Windows, Messages, SysUtils, Variants, Classes,
   Graphics, Controls, Forms, Dialogs, StdCtrls;

type
   TfrmCompAssoc = class(TForm)
...
   private
     ThisCustomer: TCustomer;
     ThisSale: TSale;
   end;

var
   frmCompAssoc: TfrmCompAssoc;

implementation

{$R *.dfm}

procedure TfrmCompAssoc.btnCustomerClick(Sender: TObject) ;
begin
   if not assigned (ThisCustomer) then ThisCustomer := TCustomer.Create;
   ThisCustomer.Name := edtName.Text;
   ThisCustomer.PhoneNo := edtPhone.Text;
end;

procedure TfrmCompAssoc.btnSaleClick(Sender: TObject) ;
begin
   if assigned (ThisSale) then ThisSale.Free;
   ThisSale := TSale.Create(ThisCustomer, edtAmount.Text) ;
   btnDisplayClick (self) ;
end;

procedure TfrmCompAssoc.btnDisplayClick(Sender: TObject) ;
begin
   try
     lblName.Caption := 'Name: ' + ThisSale.Name;
     lblPhone.Caption := 'Phone: ' + ThisSale.Phone;
     lblAmount.Caption := 'Amount: ' + ThisSale.Amount;
   except
     ShowMessage ('Invalid customer or sale.') ;
   end;
end;

end.
Run the program. Enter a Name and a Phone number and click the Create Customer button. This first creates a ThisCustomer object (if one does not already exist) and then assigns its values.

Now enter a sales Amount and click on the Create Sale button. If a ThisSale object already exists it is destroyed: we do this because TSale is immutable and so cannot be altered.

ThisSale, an instance of our composed TSale class, is now created and initialised to refer to the ThisCustomer object and with the value entered for the Amount. As far as the user interface is concerned, retrieving the values stored in ThisSale’s associated object, ie the name and phone number, is indistinguishable from retrieving the value stored directly in ThisSale.

TCustomer is mutable; it has read-write properties and so its values can be set. TSale is immutable, so it must be destroyed and recreated each time with the appropriate initialisation values.

Creating a TSale object requires a TCustomer object as a parameter . At startup, both ThisCustomer and ThisSale carry nil references, and trying to access them before their values have been initialised will lead to an exception. btnDisplayClick therefore makes these accesses within a try … except construct.

Test this program and it should all work! Create a customer and a sale. The relevant details are displayed, showing that the association between TSale and TCustomer works correctly. Create another set of values for the customer and a new sale and, again, the correct values appear in the Display Sale box.

Now change the values for the customer’s Name and Phone, and click the Create Customer button. This changes the values of the ThisCustomer object. Without making any change to the sale, click on Display Sale. What do you expect to happen? What does happen? Display Sale displays the values of ThisSale (lines 52–56), which is immutable, and yet the changed values of ThisCustomer are displayed. Why?

Find the answer in the following chapter .. stay tuned!

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. Association and Composition - Delphi OOP Part 9 - Chapter 19

©2009 About.com, a part of The New York Times Company.

All rights reserved.