• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

What is pluggable adapter?

 
Ranch Hand
Posts: 229
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've read the GoF book and I can understand the most of the Adapter pattern. What I don't understand is the pluggable adapter. What exactly is that? A example with code will be helpful.
Thanks in advance!
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pluggable adapters were one of my favorite concepts from Smalltalk. Basically, it allows you to put in an adapter when the adaptee (receiver) protocol is not known at compile time by using reflection. When you create the adapter instance, you pass it the name of the adaptee's method to call, and also any metadata that's necessary to translate input types. When the adapter receives a method call of the target interface, it uses reflection to call the corresponding method specified on the adaptee.
Honestly, I've not seen an example of this in Java, but it sure sounds like a good idea for a JavaRanch article
Kyle
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.guydavis.ca/seng/seng609.04/adapter.shtml#7
 
Kyle Brown
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Geez -- I'd consider that link nearly plagiarism. All they've done is to copy diagrams and code directly out of Gamma and have so nearly duplicated the text that they've certainly violated what I think would be the "fair use" limit.
Kyle
 
Kyle Brown
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that I've had a chance to think about this, I think the reason why we've not seen pluggable adapters in Java is because of the availability and light overhead of inner classes.
Think about it -- the most common way to implement an adapter in Java is to implement the adapter interface, and then wrap the appropriate adaptee methods in the implemented methods of the inner class. This is, for instance, the way in which AWT events are normally handled.
The patterns depot has an excerpt from James Cooper's book in which he talks a little bit about pluggability, and gives example code of using Event adapters.
Kyle
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another reason for not seeing those might be that I for one couldn't really come up with a good example scenario where such an adapter would be appropriate. I guess there is one place in a million lines of code where such flexibility is required but mostly I would hate looking at the client code which needs to "configure" the pluggable adapter...
 
Kyle Brown
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, the scenario in Smalltalk where they were nearly exclusively used was in handling window events. This is, of course, the same way that inner classes that are adapters are used now in Java.
Kyle
 
Edmund Yong
Ranch Hand
Posts: 229
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the responses guys, but sorry, I still don't get what a pluggable adapter is. How different is it from the normal adapter?
 
Kyle Brown
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regular Adapter -- No reflection needed. Call the adaptee method directly from the Adapter.
Pluggable Adapter -- Use reflection to look up the adaptee method and invoke it indirectly in the Adapter.

Kyle
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kyle Brown:
Pluggable Adapter -- Use reflection to look up the adaptee method and invoke it indirectly in the Adapter.


How does the adapter know for which method to look?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
How does the adapter know for which method to look?

The adapter needs to be configured upon creation. A simple example would be to hand it a Map which maps interface A's methods to interface B's methods.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lasse Koskela:
The adapter needs to be configured upon creation. A simple example would be to hand it a Map which maps interface A's methods to interface B's methods.


Ah, ok, that makes sense... Thanks!
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic