| Author |
What is pluggable adapter?
|
Edmund Yong
Ranch Hand
Joined: Nov 16, 2003
Posts: 164
|
|
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!
|
SCJP 1.2, SCWCD 1.4
|
 |
Kyle Brown
author
Ranch Hand
Joined: Aug 10, 2001
Posts: 3878
|
|
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
|
Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11945
|
|
|
http://www.guydavis.ca/seng/seng609.04/adapter.shtml#7
|
Author of Test Driven (Manning Publications, 2007) [Blog] [HowToAskQuestionsOnJavaRanch]
|
 |
Kyle Brown
author
Ranch Hand
Joined: Aug 10, 2001
Posts: 3878
|
|
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
Ranch Hand
Joined: Aug 10, 2001
Posts: 3878
|
|
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
Sheriff
Joined: Jan 23, 2002
Posts: 11945
|
|
|
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
Ranch Hand
Joined: Aug 10, 2001
Posts: 3878
|
|
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
Joined: Nov 16, 2003
Posts: 164
|
|
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
Ranch Hand
Joined: Aug 10, 2001
Posts: 3878
|
|
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
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
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?
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11945
|
|
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
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
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!
|
 |
 |
|
|
subject: What is pluggable adapter?
|
|
|