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

Analysing the RAD-generated code; A UML Class Diagram

By Zarko Gajic, About.com

Adding buttons
We added two buttons to the form and called them btnYellow and btnBlue. On this basis, Delphi automatically inserted lines 8 & 9. These declare that the TfrmStructureDemo class is composed of objects called btnYellow and btnBlue, both of type TButton (Delphi’s standard class for buttons). These objects are now part of the type frmStructureDemo as reflected in the Object TreeView (figure 2).

In composing a class, in this case TfrmStructureDemo, from other objects, in this case btnYellow and btnBlue of type TButton, we automatically gain all of the TButton class’s capability when we include it in our form. Like TForm, TButton is a standard Delphi class, and composition gives a powerful way of re-using existing components and classes. We will return in more detail to this concept of composition when we use it as a tool for creating our own classes.

Adding event-handlers
We added an OnClick event-handling method for each button by double-clicking on each in the Form Designer and adding the necessary code. Delphi automatically extends the class definition. It incorporates these event handlers as methods of TfrmStructureDemo by declaring these procedures in the type structure (lines 10 & 11). These declarations do not define what the procedures do, so Delphi also creates the skeletons for these event handlers in the implementation section (lines 21 to 28), leaving us to fill in the appropriate programming statements (lines 23 & 27). In Delphi, although methods can be either procedures or functions, event handlers are always procedures. Along with all the methods of the TForm and TButton classes, these event handlers are now also methods belonging to the TfrmStructureDemo class.

Note: for simplicity, an event-handler method is often called just an ‘event handler’.

A method declaration, as in line 10, is sometimes referred to as a method signature since it gives the method name (btnYellowClick), the type of subroutine (a procedure) and the parameter list (Sender: TObject).

Once we define and use our own classes and objects we will start by mimicking Delphi’s RAD code.

Ex 1.2 step 4 A graphical representation: the UML

When designing and documenting a program, it can be very helpful to use a graphical representation of its structure and operation. A widely used graphical OO modelling language is the Unified Modelling Language, or UML, and that is what we will use here.

The UML represents a class as a box with a number of compartments (figure 3). The top compartment gives the class name (cf line 7 in the program above). The second compartment gives the class attributes (cf lines 8&9) and the third gives the methods (cf lines 10&11).

A UML class diagram can also show relationships between classes such as inheritance and association. First we’ll look at inheritance or generalisation as it is called in UML. This is shown with an open arrow head pointing from the subclass to the superclass (figure 4, cf line 7 above).

Classes can become quite complex and so one generally shows only those attributes and methods that are of interest in a particular diagram. In figure 4, we show TForm without any of its attributes or methods.

Because the subclass is everything that the superclass is and more, inheritance is often called an IsA relationship.

In addition to the inheritance relationship, we can also use the UML to show the various forms of association. A strict form of association is composition. We mentioned earlier that the relationship between the form and the two buttons is composition. So, instead of the two buttons as attributes of the form as in figure 4, we can show the TButton class separately and then show that TfrmStructureDemo uses (ie is composed of) two TButtons as in figure 5.

The UML representation for composition is a solid diamond at the composed class connected by a line to the class or classes that the composed class uses. Although TfrmStructureDemo is composed of two TButtons: we show the TButton class only once but include a 1:2 multiplicity on the composed link. Because we show the TButtons separately, we no longer include them within the box representing TfrmStructureDemo’s attributes.

Ex 1.2 Summary: As a basis for creating our own classes in the future we have investigated how Delphi adds data fields (the buttons) and methods (the event handlers) to a class definition. The UML provides class diagrams to represent the structure of classes and the inheritance and composition between them.

We now move onto discussion on object interaction...

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. Learning Object Oriented Programming with Delphi: Chapter 2

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

All rights reserved.