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

Indirection - Delphi OOP Part 8 - Chapter 17

By Zarko Gajic, About.com

Using three objects to tally visitors

Example 8.1 Three separate objects

One way to write this program is to have a user interface object that uses three separate TCounter objects to tally up the three different counts:
unit UpAndDownU;

interface

uses
   Windows, Messages, SysUtils, Variants, Classes,
   Graphics, Controls, Forms, Dialogs, StdCtrls, Spin, CounterU;

type
   TfrmAttendance = class(TForm)
     sedEnter: TSpinEdit;
     sedLeave: TSpinEdit;
     gpbVisitors: TGroupBox;
     btnEnter: TButton;
     btnLeave: TButton;
     lblVisitors: TLabel;
     gpbIn: TGroupBox;
     lblIn: TLabel;
     gpbOut: TGroupBox;
     lblOut: TLabel;
     procedure btnEnterClick(Sender: TObject) ;
     procedure btnLeaveClick(Sender: TObject) ;
     procedure FormCreate(Sender: TObject) ;
   private
     VEntered, VInside, VLeft: TCounter;
   end;

var
   frmAttendance: TfrmAttendance;

implementation

{$R *.dfm}

procedure TfrmAttendance.btnEnterClick(Sender: TObject) ;
begin
   VEntered.Add (sedEnter.Value) ;
   lblIn.Caption := IntToStr (VEntered.Total) ;
   VInside.Add (sedEnter.Value) ;
   lblVisitors.Caption := IntToStr (VInside.Total) ;
   sedEnter.Value := 0;
end;

procedure TfrmAttendance.btnLeaveClick(Sender: TObject) ;
begin
   VLeft.Add (sedLeave.Value) ;
   lblOut.Caption := IntToStr (VLeft.Total) ;
   VInside.Subtract (sedLeave.Value) ;
   lblVisitors.Caption := IntToStr (VInside.Total) ;
   sedLeave.Value := 0;
end;

procedure TfrmAttendance.FormCreate(Sender: TObject) ;
begin
   VEntered := TCounter.Create;
   VInside := TCounter.Create;
   VLeft := TCounter.Create;
end;

end.
We create three counters: the number of visitors who have entered, the number inside and the number who have left. Every time a number of visitors enter, we add this number to those who have already entered and to those already inside and display these new totals.

Every time a number of visitors leave, we add this number to those who have left, subtract it from those inside and display the new totals. By declaring three TCounters as part of the structure of TfrmAttendance we are setting up direct one way links between the user interface object (ie the form) and the application objects (ie the counters). The form uses these links by sending messages to the counters by name. The counters carry no link to the form, and so they cannot refer to the form or send any messages to it.

Figure presents this class structure in UML. It shows that each TfrmAttendance class (there is only one in this case) is linked to three TCounters, that any specific TCounter is associated with exactly one TfrmAttendance, and that TfrmAttendance carries links to the TCounters, ie that one can navigate from a TfrmAttendance to any of the TCounters, but that the TCounters do not carry any link to the TfrmAttendance. (This was introduced in chapter 1, figures 5 and 10.)

Run this program and test it, and you’ll see that it works well. But we have a conceptual problem here. In terms of the application we have a single concept: visitors to the museum. But in the program we have modelled it by three objects, VEntered, VInside and VLeft, and so the user interface (the form) must interact with them ndependently to carry out the logic for the application (in addition to its user interface responsibilities).

This dual role increases the potential for error. For example, the user interface could by mistake add the number of visitors to the VEntered object but not to the VInside object, or could zero the value of VInside without keeping VEntered and VLeft synchronised. What we would prefer is for there to be a single object responsible for tallying up the various visitors with the methods Entered and Left. The user interface would then issue just the Entered or Left message as appropriate, and this intermediate object would be responsible for keeping the various counts synchronised. The user interface would no longer carry the responsibility for enforcing the application logic and the scope for error would be reduced.

Is there some way we can change the program along these lines while still reusing the existing TCounter class?

One possibility is to create an intermediate object, say TMuseumCount, which in turn uses the three TCounter objects.

As we explore in the next two examples, we can use either chaining or delegation to implement the communication through the intermediate object. Both of these approaches are implemented through programmer created links.

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. Indirection - Delphi OOP Part 8 - Chapter 17

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

All rights reserved.