The Artima Developer Community
Sponsored Link

Weblogs Forum
The Generic Adapter Pattern

10 replies on 1 page. Most recent reply: Feb 18, 2008 6:13 PM by Michael Smit

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 10 replies on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

The Generic Adapter Pattern (View in Weblogs)
Posted: Aug 15, 2005 9:51 AM
Reply to this message Reply
Summary
C++ templates enable a powerful variant of the Adapter Pattern, which I call the Generic Adapter Pattern. I think it helps illustrate how OO design patterns go together well with generic programming techniques.
Advertisement
The Generic Adapter Pattern, shows off a bit of the power of C++ templates. The way it works is as follows: say you have a client (DuckListener) which requires an object which implements a specific interface (DuckInterface). If you want to use another object (Dog) which is not an explicit subtype you have to create an adapter. Here is example of how you could use the GoF Adapter Design Pattern:
struct DuckInterface {
  virtual string Quack() = 0;
}

struct DuckListener {
  void Listen(DuckInterface& d) {
    cout << "the duck quacks like this " << d.Quack() << endl;
  }
}

struct Dog {
  void Quack {
    return "woof";
  }
}

struct DogToDuckAdapter : DuckInterface {
  DogToDuckAdapter(Dog* x) : m(x) { }
  virtual string Quack() { return m->Quack(); }
  Dog* m;
}

int main() {
  Dog dog;
  DuckListener listener;  
  DogToDuckAdapter ducklike(&dog);
  listener.Listen(ducklike);
}
Now here is how the Generic Adapter Pattern works:
template<typename T>
struct GenericDuckAdapter : DuckInterface {
  GenericDuckAdapter(T* x) : m(x) { }
  virtual string Quack() { return m->Quack(); }
  T* m;
}

int main() {
  Dog dog;
  DuckListener listener;
  GenericDuckAdapter<dog> ducklike(&dog); 
  listener.Listen(ducklike);
}
The usefulness of the generic approach hopefully becomes more apparent when you consider that when you need to make other things duck-like, the non-generic approach becomes quickly very redundant.

I wonder who is researching this kind of stuff, any ideas? It seems to me that someone should write a book about generic design patterns in C++. Out of curiosity would you be interested in such a book?


Volodya Orlenko

Posts: 2
Nickname: volodya
Registered: Aug, 2005

Re: The Generic Adapter Pattern Posted: Aug 15, 2005 2:54 PM
Reply to this message Reply
Isn't Alexandrescu solving a set of similar problems in his "Modern C++ Design" book (http://www.amazon.ca/exec/obidos/ASIN/0201704315/qid=1124132414/sr=8-2/ref=sr_8_xs_ap_i2_xgl14/701-1289432-2137909)?

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: The Generic Adapter Pattern Posted: Aug 15, 2005 3:03 PM
Reply to this message Reply
> Isn't Alexandrescu solving a set of similar problems in
> his "Modern C++ Design" book
> (http://www.amazon.ca/exec/obidos/ASIN/0201704315/qid=11241
> 32414/sr=8-2/ref=sr_8_xs_ap_i2_xgl14/701-1289432-2137909)?

Yes it sure looks like it! Thanks for pointing that out.

Michael Feathers

Posts: 448
Nickname: mfeathers
Registered: Jul, 2003

Re: The Generic Adapter Pattern Posted: Aug 15, 2005 5:44 PM
Reply to this message Reply
This looks very close to the External Polymorphism Pattern from the PLOPD3 book.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: The Generic Adapter Pattern Posted: Aug 15, 2005 6:40 PM
Reply to this message Reply
> This looks very close to the External Polymorphism Pattern
> from the PLOPD3 book.

Such is the curse of creativity and ignorance. I am doomed to roam the internet inventing stuff which has been invented before. *grin*

Thanks a lot for pointing that out Michael!

Calvin Mathew Spealman

Posts: 13
Nickname: ironfroggy
Registered: Aug, 2005

Re: The Generic Adapter Pattern Posted: Aug 15, 2005 9:39 PM
Reply to this message Reply
I don't mean to berate your post or seem ignorant, but what exact benefit does this pattern give us? It appears to work only (or mostly?) in adapting to a class from another class which implements the correct methods with matching signatures. How often would that be the case where one didn't have access to the creation of both classes, and could then simply make both inherit the abstract class? Without the ability to add specific code to deal with a specific class, wrapping its methods and data structures into what the interface expects, it seems very impractical.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: The Generic Adapter Pattern Posted: Aug 15, 2005 10:17 PM
Reply to this message Reply
> I don't mean to berate your post or seem ignorant,

No worries.

> but
> what exact benefit does this pattern give us? It appears
> to work only (or mostly?) in adapting to a class from
> another class which implements the correct methods with
> matching signatures.

Voila, that's exactly what it does.

> How often would that be the case
> where one didn't have access to the creation of both
> classes, and could then simply make both inherit the
> abstract class?

There are several possible scenarios: you don't have access to the classes (in some kind of library), or you don't want the class to have a vtable pointer, or you don't want dynamic dispatching in the class itself most of the time, or you don't want to break something which already works, ... those are the first few that jump to mind.

> Without the ability to add specific code
> to deal with a specific class, wrapping its methods and
> data structures into what the interface expects, it seems
> very impractical.

As general practice the pattern is not advised, just something useful in some circumstances.

Michael Feathers

Posts: 448
Nickname: mfeathers
Registered: Jul, 2003

Re: The Generic Adapter Pattern Posted: Aug 16, 2005 8:12 AM
Reply to this message Reply
> > This looks very close to the External Polymorphism
> Pattern
> > from the PLOPD3 book.
>
> Such is the curse of creativity and ignorance. I am doomed
> to roam the internet inventing stuff which has been
> invented before. *grin*
>
> Thanks a lot for pointing that out Michael!

We're all in that boat. It's the fact that things like this are continually reinvented that *make* them patterns.

Somedays I thank my lucky stars that we developed a pattern culture in our industry before patent-mania hit software. Imagine if someone had patented the Observer pattern, for instance.

S. Fanchiotti

Posts: 10
Nickname: impatient
Registered: Nov, 2003

Re: The Generic Adapter Pattern Posted: Aug 18, 2005 12:18 PM
Reply to this message Reply
Perhaps I am missing something but should't the Adapter derive from the public interface one is trying to adapt? Or is this a smart feature of some compilers?

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: The Generic Adapter Pattern Posted: Aug 18, 2005 12:24 PM
Reply to this message Reply
> Perhaps I am missing something but should't the Adapter
> derive from the public interface one is trying to adapt?

Yes! I totally forgot, thanks a lot. I have fixed the example.

Michael Smit

Posts: 1
Nickname: namidim
Registered: Feb, 2008

Re: The Generic Adapter Pattern Posted: Feb 18, 2008 6:13 PM
Reply to this message Reply
If you happen to have libraries which share the same shared pointer implementation you can also do use the following technique http://4thmouse.com/index.php/2008/02/16/automatic-adaptation-in-c/
to automatically adapt objects as needed.

Flat View: This topic has 10 replies on 1 page
Topic: The Generic Adapter Pattern Previous Topic   Next Topic Topic: How Does Language Impact Framework Design?


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us