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
Using the try-except block we can guard a block of code:
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 try frmAuxiliary.Hide; frmAuxiliary.Free; frmAuxiliary := nil; except ShowMessage ('Auxiliary Form does not exist') ; 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.
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;
Ex 1.3 step 8 Lifetimes in a sequence diagramIn 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 objects lifetime explicitly in a sequence diagram. The message to the objects 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 objects 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 well see in a future example) by using the FreeAndNil procedure.
When needed, we can show the start and end of an objects lifeline on a sequence diagram. A message to the objects 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.