Delphi Patterns

Joanna Carter is in the middle of writing a book on Design Patterns in Delphi, due out Q3-2003; but until it is published here is a look at some of the Design Patterns that Delphi uses.

Firstly, for the experienced Delphi user who is a relative newcomer to patterns, there is plenty that is nicely familiar. I hope this will demystify patterns a little for Delphi users.

CreationalPatterns

A Prototype is something from which an object of the appropriate class can be made, normally by calling a clone or copy method. In Delphi, a class reference can be used in much the same way.

The BuilderPattern deals with creating complex objects (often composites) in stages, and example of which is the way forms are created by Delphi, loading properties, and hence subcomponents, from a stream.

Singletons include Application and Screen.

StructuralPatterns

Composite - Delphi components are composites in terms of their Parent (not inheritance) relationship with other components

AdapterPattern - many Delphi VCL components are wrappers (adapters) around windows controls eg Listbox. This wrapping enables the Delphi TListbox to exist inside Delphi's object hierarchy under TComponent.

BehavioralPatterns

MediatorPattern - The use of forms and datamodules to handle the events dispatched by other components is an example of the mediator pattern. (It means we don't have to subclass the components themselves when we want to change their behaviour)

CommandPattern - Actions are an example of this pattern to decouple button clicks etc from the implementation of the consequences. The textbook CommandPattern would subclass actions rather than defining OnExecute? events for them in a form (mediator). Delphi actions do not support Undo.

ChainOfResponsibilityPattern - One example is the OnHint?/ParentShowHint? of TComponent. Mouseovers are passed up the parent chain until they are handled.

StrategyPattern - You can write a web server application using a TWebModule, behind the scenes this may be using the CGI, ISAPI, Apache DSO or another web application object, descended from an abstract web application object. Thus you can recompile the same code as one of the other kinds of applications. This probably also uses the TemplateMethod pattern.

Well that's something to get us started. Please add some examples that I have missed.

Contributors: JoeOtten


CategoryDelphi


EditText of this page (last edited October 10, 2004) or FindPage with title or text search