Mega Code Archive

 
Categories / Delphi / Examples
 

The adaptor pattern

Peter Hinrichsen explores the practical use of design patterns again. This time round it’s the the Adaptor Pattern which comes under scrutiny. The Delphi component palette is growing with every release of Delphi. Apart from the GUI controls we have had since Delphi 1, we now have various non-visual controls, all performing the same basic task, to choose from. For example: for SQL database access we can choose between the BDE controls (TDatabase and TQuery) and ADO. If we are targeting a specific database, we may want to bypass a generic data connection layer and go directly to the database’s API using a family of controls like IBObjects for InterBase, or DOA (see Reference 1) for Oracle. For internet connection, you may have started your project with an early version of Delphi using the FastNet components, but now want to move up to Indy. For data compression, you may have started your project using the ZLib compression algorithm supplied on the Delphi CD, and now want to change to Zip format. You may have started a project with no data encryption, but changed requirements mean you have to add encryption without breaking any existing systems. For XML parsing, you can choose between building a dependency on the MSDom DLL that comes with Internet Explorer 5, or use one of the native Delphi parsers available on the web (Delphi 6 itself now contains a native XML parser). In the next section I will discuss some of the problems of building in dependency on a single vendor’s component. I’ll then look at what the Gang of Four (or GoF, see Reference 2) say about the Adaptor Pattern and examine the various ways it can be implemented with Delphi. I’ll finish by using the Adaptor to wrap the ZLib compression library. Once we have wrapped ZLib, I’ll examine two ways of creating concrete instances of the adapted class by using a class reference and the Factory Pattern.