1. Computing

Learning Object Oriented Programming with Delphi: Chapter 3

Part 1 - Chapter 3

By

OOP Chapter 3 Sequence Diagram
Materials written by John Barrow. Modifications by Zarko Gajic

Back to Chapter 2

Introducing (in this Chapter) :

  • Interaction between objects: simple message passing, association and composition
  • The UML: class, object and sequence diagrams
  • Inheritance, association and composition relationships
  • Chapters 1-3: Homework
In the previous chapter, we discussed about explicitly creating and freeing non-auto create forms in a Delphi project.
Using the try-except block we can guard a block of code:
 procedure TfrmMain.btnFreeClick(Sender: TObject) ;
 begin
    try
      frmAuxiliary.Hide;
      frmAuxiliary.Free;
      frmAuxiliary := nil;
    except
      ShowMessage ('Auxiliary Form does not exist') ;
    end;
 end; 
Instead of using exception handling, we could have coded the object freeing sequence in an If statement as follows:
 procedure TfrmMain.btnFreeClick(Sender: TObject) ;
 begin
    if frmAuxiliary = nil then
      ShowMessage ('Auxiliary Form does not exist')
    else
    begin
      frmAuxiliary.Hide;
      frmAuxiliary.Free;
      frmAuxiliary := nil;
    end;
 end; 
In this variation we make sure frmAuxiliary actually does exist before we attempt an operation on it (ie to hide it). It is often possible to use either exception handling or If statements to prevent attempts at invalid object operations, and each approach has its advantages and disadvantages. In this module we generally use the exception handling approach. This gives a brief introduction to object Create and Free processes. We will return to these processes in more detail in a subsequent chapter.

Ex 1.3 step 8 Lifetimes in a sequence diagram

In step 7 we specifically create an object when we need it and free it afterwards when we are done with it, and so we are in complete control of its lifetime. We can show an object’s lifetime explicitly in a sequence diagram. The message to the object’s Name represents the constructor and a cross over its lifeline shows its destruction.

Ex 1.3 Summary
There are a number of stages to creating an object.

First we create a class definition. A class is not yet an object and does not have its own data. We cannot use it directly since it is only a template. So we declare one or more names to act as object references and then instantiate the objects from the class definition. Although each object is built to the same template, the class definition, an instantiated object is an independent entity with a life of its own. For example, each form has its own position and caption, and we can reposition and resize each one independently without affecting the others.

In UML, a class diagram shows the static structure of classes in the program. An object diagram provides a snapshot of the instantiated objects at a particular moment in the program. A sequence diagram shows dynamic behaviour between various objects.

We have investigated issues around an object’s lifetime, creating it and freeing it as needed instead of creating it when the program starts and freeing it when the program ends. We assumed responsibility for freeing the object by setting the owner to nil in the Create method call.

When freeing an object we must also set its reference to nil. We do this either by calling the Free method and then setting the reference to nil, or (as we’ll see in a future example) by using the FreeAndNil procedure.

When needed, we can show the start and end of an object’s lifeline on a sequence diagram. A message to the object’s Name indicates its construction. A cross over its lifeline shows its destruction. There are several different kinds of message and we distinguish these through different notations.

©2014 About.com. All rights reserved.