Tutorial 10: Stacks and Queues
The Adapter Pattern
Introduction
Stacks and queues are examples of structures with special accesses. They can be
easily implemented by composing with one of the linear recursive structures studied so
far. This is called the "adapter pattern". The adapter pattern is
used to make one class look like another class with a different set of public methods.
It comes in two flavors:
- The class adapter pattern where the adapter is defined as a subclass of two
different classes, using multiple inheritance. In this case, the adapter can be used
in place of either of its two super classes and is called a two-way adapter. Using
multiple inheritance, we can have multi-way adapters as well.
- The object adapter pattern where composition is used instead of multiple
inheritance. The adapter maintains a reference to an object, called the adaptee, and
delegates all calls to this adaptee. This provides only one-way adaptation.
This tutorial consists of the simple task of implementing the abstract Stack and Queue
structures by adapting the Circular List
structures. The coding should be trivial.
Exercises
- Write the code for IStack, IQueue, CListStack, CListQueue,
CheckEmpty, CheckFull specified in the diagram below (SQDoc). CLList and ICLVisitor are defined in Circular List.
This is the object adapter pattern.
- To ensure that only specific types of objects are stored in our stacks and queues, we
can also use the object adapter pattern as shown in the diagram below (Type
Specific). Write the code for StackInt and QueueString.
- Below is a two-way stack/list adapter using multiple inheritance (Two Way).
Write the code for CListStack2Way.
D. X. Nguyen, Nov. 7, 1999.
dxnguyen@cs.rice.edu