1. Home
  2. Computing & Technology
  3. Delphi Programming

The Adapter and Facade Patterns - Delphi OOP Part 10 - Chapter 23

By Zarko Gajic, About.com

Materials written by John Barrow. Modifications by Zarko Gajic

Back to Chapter 22

The Adapter pattern for polymorphism

In examples 6.1 & 6.3 we demonstrated polymorphism through a hierarchy of different types of furniture. For this example we extend that hierarchy to incorporate an existing class, TTheCupboard. How can we persuade an existing class to operate polymorphically within a different hierarchy?

We’re discussing the Adapter here, so you have probably guessed that an Adapter will solve this problem.

Note: full chapter text along with Delphi source code examples is available in the PDF available for download.

Another kind of Adapter

The adapter in the examples above is an object adapter, and is implemented through composition. Sometimes, and more often in languages like C++ that allow multiple inheritance, one also sees a class adapter, implemented through inheritance.

Note: full chapter text along with Delphi source code examples is available in the PDF available for download.

Class Adapter for Polymorphism

Have ClassA, the Adapter, delegate the required operations to ClassB, the Adaptee. The Adapter’s delegating methods operate polymorphically in hierarchy A while the Adaptee’s delegate methods perform the required tasks. The Adapter’s and the Adaptee’s methods may have different signatures. The Adapter’s clients are unaware of the delegation and the Adaptee is unaware of the Adapter. The Adapter may have to perform some local processing and store some local data in addition to delegating the operations.

This describes the Object Adapter, often referred to simply as the Adapter. A Class Adapter is also possible (example 10.3), but is mainly justified in languages with multiple inheritance, and so we do not present it as a pattern here.

An Adapter is sometimes called a Wrapper, but the term Wrapper is also used in other ways. So for clarity we use the term Adapter when referring to this pattern.

Note: full chapter text along with Delphi source code examples is available in the PDF available for download.

Introducing the Facade Pattern

At an implementation level, the Facade pattern is very similar to the Adapter. Conceptually, it provides a simple interface with controlled visibility to a group of objects or to a complex object. So where the purpose of an Adapter is to adapt an object in some way or other, the purpose of a Facade is to reduce the coupling between a set of objects (or a single complex object) and the rest of the system. We’ll explore this by developing a Facade for a RADgenerated user interface object.

At times, a program may need to interact in a relatively limited way with several related objects or with a single very complex object. It would be preferable to provide a simple view of these objects or this object and so reduce the potential coupling and/or simplify the coding.

Therefore, create a Facade, which is an intermediate class with the simplified interface and which delegates operations implicitly to the set of objects or to the complex object. Because of the delegation, clients need not engage with the full complexity of the encapsulated object(s) and the Facade can accommodate any changes needed in the encapsulated object(s) without involving the clients.

The Facade pattern reduces complexity in several ways. It provides a single access class to a group of objects with all access by clients to the group through itself, so reducing coupling. It enhances ing encapsulation by exposing only the required data and behaviour and keeping all other data and behaviour hidden. It provides a simple interface to a complex object in cases where the full complexity is not needed.

Chapter Download

Here's the full text of the Delphi OOP part 10 available as PDF, along with the Delphi source code examples.

This ends Part 10.

OOP Part / Chapter Download

PDF TextSource Code

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. OOP in Delphi
  6. Free Online OOP Course
  7. The Adapter and Facade Patterns - Delphi OOP Part 10 - Chapter 23

©2009 About.com, a part of The New York Times Company.

All rights reserved.