Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anybody explain the concept of pluggable adapter to me with good example?

Can anybody explain the concept of pluggable adapter to me with good example?

like image 709
Prajwal Tuladhar Avatar asked Jan 16 '09 03:01

Prajwal Tuladhar


People also ask

What is a pluggable adapter?

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.

What is an example of an adapter?

The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients. Socket wrenches provide an example of the Adapter.

What is adapter in Java with example?

Adapter is a structural design pattern, which allows incompatible objects to collaborate. The Adapter acts as a wrapper between two objects. It catches calls for one object and transforms them to format and interface recognizable by the second object. Learn more about Adapter.

What is the most common uses of the Adapter pattern?

Usage of Adapter pattern: It is used: When an object needs to utilize an existing class with an incompatible interface. When you want to create a reusable class that cooperates with classes which don't have compatible interfaces.


2 Answers

From what I understood from a quick reading of Google results, a pluggable adapter is an adapter that isn't hard-coded against a specific adaptee. On the surface (the adapter's own interface), it's all the same but it can adapt to different adaptees with different interfaces. I found this thread pretty explanatory:

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.

And this:

The main responsibility of the Viewer is to populate a widget from a domain model without making any assumptions about domain itself. JFace viewer uses the Delegating Objects mechanism in Pluggable Adapter Pattern to implement the above requirement.

Facehugger in action

Think of it as a facehugger from Alien; when it hugs a face, all you see is the slimy back of the facehugger. You can poke it with a stick and try to pry off its arms (the adapter interface). But it basically can hug the face of any human (the adaptee), regardless of the face features. Maybe I'm pushing it a bit, but, hey, I love Alien.

like image 72
Ates Goral Avatar answered Sep 28 '22 13:09

Ates Goral


You can read this article about adapter/pluggable pattern:

Table of content in this article:

* 1 Design Patterns
* 2 Intent of Adapter
* 3 Motivation
* 4 Structure
* 5 Applicability
* 6 Consequences
* 7 Implementation
      o 7.1 Known Uses and Sample Code
      o 7.2 Related Patterns
* 8 Conclusions
* 9 Appendix
      o 9.1 References
      o 9.2 Glossary

Quote:

Smalltalk introduced the concept of a "pluggable adapter" to describe classes with built-in interface adaptation. This interesting concept allows for classes to be introduced into existing systems that might expect different interfaces to the class. This technique can help promote class reuse across modules and even projects.

Here is a small example:

We have two classes - Foo & Boo that outputs some string to console. Adapter class can adapt methods from both classes to provide interface (SaySomething) required by client. Note that there is no dependency on interface name - we can easily adapt both SayHey and Bark methods.

class Foo 
{
    public static void SayHey() { Console.WriteLine("Hey!"); }
}

class Boo 
{
    public static void Bark() { Console.WriteLine("Woof!"); }
}

class Adapter 
{
    public Action SaySomething { get; private set;} // "pluggable" adapter

    public Adapter(Action saySomethingAction) 
    {
        SaySomething = saySomethingAction;
    }
}

class Program
{
    static void Main(string[] args)
    {
        (new Adapter(Foo.SayHey)).SaySomething();
        (new Adapter(Boo.Bark)).SaySomething();
    }
}
like image 41
aku Avatar answered Sep 28 '22 13:09

aku