Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Design

The Adapter Pattern


Storing a Type for Later Use

As I've already noted, the result of the typeid operator cannot be used for a dynamic_cast; for instance, the result of the typeid operator cannot be used for much of anything. It is therefore typically useless, and is certainly useless in the context we're working in here.

In the context of the Munchausen Pattern, the solution involved a type-list, which was possible because the types to be supported in the the Munchausen Pattern are known aforehand. That is not the case here, which means a type-list is not really an option.

While the type of the visitor is partially needed in the context of the adapter introduced above, it would be a very meager design choice to use an adapter For instance, our Task class will need to use the visitor independantly from the data-batch to extract information from it and, eventually, but outside the scope of this article, pass that information to another object. The most straightforward way to do this is, once more, to use an adapter which will, in this case, not store a pointer to an object of type VisitorType.

A skeleton of such a visitor adapter would look like this:

struct VisitorAdapter
{
	virtual ~VisitorAdapter();
};

namespace Details
{
	template < typename VisitorType >
	struct VisitorAdapter : public ::VisitorAdapter
	{
	};
}

to which the necessary functions can be added as needed.

An In-depth Look at this Adapter Pattern

The original Adapter pattern[6] is summarized, by its authors, as follows:

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

The implementation as proposed in its original form is quite different from what I present here, however, in that in its original form, the adapter is derived from the adaptee and exposes an interface defined for the adapter, while calling functions on the adaptee accordingly. This means that common implementations of the Adapter pattern impose writing a new, independant, class for each class to be adapted and, worse in our case, tightly coupling that class with the class to be adapted[7].

The Adapter pattern I present here is the same pattern and serves the same purpose, but the implementation as presented here requires only the unification of the interface as a set of traits classes (which could be very much smaller than the number of classes to adapt) and a single, templatized, derived class which is specialized with the type to adapt.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.