1

I know how to implement basic Adapter design pattern and also knows how C# using delegation to implement Pluggable Adapter design. But I could not find anything implemented in Java. Would you mind pointing out an example code.

Thanks in advance.

Juliyanage Silva
  • 2,529
  • 1
  • 21
  • 33

1 Answers1

1

The pluggable adapter pattern is a technique for creating adapters that doesn't require making a new class for each adaptee interface you need to support.

In Java, this sort of thing is super easy, but there isn't any object involved that would actually correspond to the pluggable adapter object you might use in C#.

Many adapter target interfaces are Functional Interfaces -- interfaces that contain just one method.

When you need to pass an instance of such an interface to a client, you can easily specify an adapter using a lambda function or method reference. For example:

interface IRequired
{
   String doWhatClientNeeds(int x);
}

class Client
{
   public void doTheThing(IRequired target);
}

class Adaptee
{
    public String adapteeMethod(int x);
}

class ClassThatNeedsAdapter
{
    private final Adaptee m_whatIHave;

    public String doThingWithClient(Client client)
    {
       // super easy lambda adapter implements IRequired.doWhatClientNeeds
       client.doTheThing(x -> m_whatIHave.adapteeMethod(x));
    }

    public String doOtherThingWithClient(Client client)
    {
       // method reference implements IRequired.doWhatClientNeeds
       client.doTheThing(this::_complexAdapterMethod);
    }

    private String _complexAdapterMethod(int x)
    {
        ...
    }
}

When the target interface has more than one method, we use an anonymous inner class:

interface IRequired
{
   String clientNeed1(int x);
   int clientNeed2(String x);
}

class Client
{
   public void doTheThing(IRequired target);
}


class ClassThatNeedsAdapter
{
    private final Adaptee m_whatIHave;

    public String doThingWithClient(Client client)
    {
       IRequired adapter = new IRequired() {
           public String clientNeed1(int x) {
               return m_whatIHave.whatever(x);
           }
           public int clientNeed2(String x) {
               return m_whatIHave.whateverElse(x);
           }
       };
       return client.doTheThing(adapter);
    }
}
Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
  • How is PluggableAdapter special or different from usual Adapter. `ClassThatNeedsAdapter` will be different for each client and each client will create an Adapter (anonymous class is still a class). Whats the "Pluggable" part in it ? – nits.kk Apr 19 '20 at 20:07
  • @nits.kk It's different because it's written inline - it doesn't have its own name, you don't have to write it in a different place, and you don't have to follow a reference to see it. That the same as the important differences in the C# version. Whether or not it's "still a class" is only pedantically relevant. – Matt Timmermans Apr 19 '20 at 20:15
  • okk but if you see the GOF book about the Pluggable adapter you can see Explicit Separate concrete classes for `DirectoryBrowser` / `DirectoryTreeDisplay` . So Still my question is how is this special than usual adapters. This is coming from the GOF: Elements of Reusable Object-Oriented Software book so I guess it is the authentic stuff without stories or interpretations added, its just that I need help in understanding it. I want to understand the example in it rather than external different examples..I have also put another https://stackoverflow.com/q/61311594/504133. Please help – nits.kk Apr 19 '20 at 21:20
  • It's not particularly special, but pluggable adapters are also not particularly special. They are just using a little C#-specific sugar to accomplish something that is done in other ways in other languages. – Matt Timmermans Apr 19 '20 at 21:31
  • I agree to this but it was mentioned in the book without any reference to language specific sugar... They also provided the umls for them ....this caused the doubts and confusion – nits.kk Apr 19 '20 at 21:33
  • or may be same term "Pluggable Adapter" is being used to refer to 2 different concepts , one presented by you in your answer and the other present in book by GOF. Can you please refer my question, may be it will give more clarity. – nits.kk Apr 19 '20 at 21:42
  • 1
    The technique they refer to is the same thing, but with smalltalk sugar. I'll answer your other question, though – Matt Timmermans Apr 19 '20 at 21:52