Pages

Friday, November 9, 2012

Use Custom Events to remove dependencies

I have used Delphi a lot but until now not realized how powerful events are. During the last week I got an Aha experience when I realize what can be done. And how clean code can be written with events.

An event is the same a a function pointer for those that use for example C or C++. Delphi have a lot of predefined events. For example a button can have OnClick, OnExit etc. It's easy to use them as Delphi IDE helps you with that.

If you use custom events it can help you break dependencies between classes. This make it easier to write tests for the code and refactor it.

An Example
This illustrate a form that create and show a dialog. This dialog may also be called from 2 other forms. In the dialog there are special cases to handle this.
First look at the old code before the change:

procedure TFindParcelForm.MakeOrderClick(Sender: TObject);begin // Block1 of code here if owner is TBookForm) then (Owner as TBookForm).OrderNew(self) else if Owner is TPlanForm then (Owner as TPlanForm).OrderNew(self) else if Owner is TOrgForm then (Owner as TOrgForm).OrderNew(self)

// Block2 of code hereend;

Let's now use the predefined event TNotifyEvent. It is defined in VCL Classes.pas:

So what happened was that we have broken the dependency from FrmFindParcel.pas to the owners TBookForm, TPlanForm and TOrgForm. This means that it is now easier to test frmFindParcel as a separate unit. Method MakeOrderClick is now simplified and is not aware of the owner. If else is also gone.

But we don't need to stop here. With a small addition we can make own custom events as the blog title suggests.