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 well write a program that creates a TCustomer and a TSale class:unit Comp_AssocDemoU;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.
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.
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 ThisSales 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 customers 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 5256), which is immutable, and yet the changed values of ThisCustomer are displayed. Why?
Find the answer in the following chapter .. stay tuned!


