Example 1.3 Object interaction
Objects interact with one another, and, as well come to see through the course of this module, one way to view an object-oriented program is as a group of encapsulated objects that cooperate, through message passing, to provide a particular set of services. To demonstrate this, in this example well create two classes, each in a separate unit, with simple unidirectional message passing (steps 1 and 2 below). This linking is an example of association, with one object requesting a service from another object, and is sometimes called a UsesA relationship.In step 3 well see how Delphi packages a program that consists of more than one unit. UML diagrams provide an important way of summarising and visualising what is happening in an OO program, and steps 4, 5 and 6 present the class, object and sequence diagrams for this program.
At times, object A may require a service from object B, but object B does not exist yet. Under these circumstances, object A specifically creates (instantiates) object B, obtains the required services from object B, and then destroys object B when it is no longer needed. Step 7 modifies the program to give a simple illustration of this process.
Ex 1.3 step 1 A main Form
To illustrate simple messages, we will create two classes, each one in its in own unit. In this step we define TfrmMain, which sends messages to TfrmAuxiliary to Show and Hide itself (lines 24 and 28 below). TfrmAuxiliary will be defined in step 2. Because TfrmMain will use TfrmAuxiliary, unit MainForm must list unit auxForm, the unit containing TfrmAuxiliary, in a uses clause. Here we choose to use a local uses clause (line 20).1 unit MainForm;The message calls appear in lines 24 and 28. They consist of the target object followed by a dot followed by a message name. (We define frmAuxiliary in the next step of this example.) Message calls look like subroutine calls (lines 24 & 28 above). The difference between the two is that message calls allow polymorphism.
2 interface
3 uses
4 Windows, Messages, SysUtils, Variants, Classes,
5 Graphics, Controls, Forms, Dialogs, StdCtrls;
6 type
7 TfrmMain = class(TForm)
8 radAuxShow: TRadioButton;
9 radAuxHide: TRadioButton;
10 procedure radAuxShowClick(Sender: TObject) ;
11 procedure radAuxHideClick(Sender: TObject) ;
12 private
13 { Private declarations }
14 public
15 { Public declarations }
16 end;
17 var
18 frmMain: TfrmMain;
19 implementation
20 uses AuxForm;
21 {$R *.dfm}
22 procedure TfrmMain.radAuxShowClick(Sender: TObject) ;
23 begin
24 frmAuxiliary.Show;
25 end;
26 procedure TfrmMain.radAuxHideClick(Sender: TObject) ;
27 begin
28 frmAuxiliary.Hide;
29 end;
30 end.
Polymorphism is an important aspect of OO programming that allows different objects to respond differently to the same message. This, for example, allows OO programs to respond appropriately to varying conditions without the need for hard-coding complex sets of conditional statements.
Delphi automatically makes provision for public and private fields in the form (lines 12 to 15). In this example there are no additional public or private fields, and so these statements do not perform any function. They remind programmers that it is possible to add private and public declarations to a RAD form, and we will do so from time to time. However quite often we will not have any private or public declarations to add, and then we will delete these lines from our code listing to avoid cluttering up the examples.
Let's now see how to add a secondary form to a project ...


