0

I have seen mentioned at several places that class adapter pattern cannot be done in Java (including Head First: Design pattern and Implementing Class Adapter Pattern in Java on SO). I cannot understand why.

Consider the following adaptee, target interface and adapter code:

Object to be adapted (adaptee)

class Adaptee{

   int id = 123;

   void doStuff(){ 
     /* Doing stuff */
   }

   int getId(){
       return id;
   }

}

Target interface

interface TheNewInterface{

     void doAction();

     int getId();
}

Object adapter

class ObjectAdapter implements TheNewInterface{
    Adaptee adaptee;

    ObjectAdapter(Adaptee adaptee){
        this.adaptee = adaptee;
    }

    @Override
    void doAction(){
         adaptee.doStuff();
    }

    @Override
    int getId(){
         adaptee.getId();
    }


}

Class adapter (??)

class ClassAdapter extends Adaptee implements TheNewInterface{

    @Override
    void doAction(){
         doStuff();
    }

    /**No need to provide an implementation of getId() as super class has it implemented*/
}

Why is it said that class adapter is not possible in Java? The reason for this is mentioned that Java does not supported multiple inheritance.

But per definition the ClassAdapter class is exactly that - conforms to the new interface & at the same time IS of adaptee type.

Community
  • 1
  • 1
ps-aux
  • 11,627
  • 25
  • 81
  • 128
  • 3
    I believe it is the `Object Adapter Pattern` that is not supported in Java, because Java does not support multiple inheritance. The `Class Adapter Pattern` works fine because it uses interfaces, which is Java's answer to multiple inheritance. – christopher Apr 03 '14 at 22:40
  • Java does support the Object Adapter Pattern as described here: http://en.wikipedia.org/wiki/Adapter_pattern – Jason Apr 03 '14 at 22:54

1 Answers1

0

The Class Adapter pattern is possible in Java. Resources:

Here's a question I asked exactly on this subject: How to wrap a java.lang.Appendable into a java.io.Writer?

The answer of which is a Class Adapter.

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
  • He's talking about a specific variant of the adapter pattern, the [*class* adapter pattern](http://en.wikipedia.org/wiki/Adapter_pattern#Class_Adapter_pattern). – dcastro Apr 03 '14 at 22:42
  • No, because this variant relies on multiple inheritance. And that's why it can't be done in java (because java doesn't support multiple inheritance. christopher already answered this in the comments. – dcastro Apr 03 '14 at 22:45
  • 1
    @christopher is saying the opposite of what you are: "I believe it is the Object Adapter Pattern that is not supported in Java, because Java does not support multiple inheritance. The Class Adapter Pattern works fine because it uses interfaces, which is Java's answer to multiple inheritance." – aliteralmind Apr 03 '14 at 22:46
  • 1
    @dcastro aliteralmind is correct here; I believe you accidentally misread christopher's comment. The design is obtainable via use of interfaces. Note also [the wikipedia article](http://en.wikipedia.org/wiki/Adapter_pattern#Class_Adapter_pattern) explicitly mentions: *"It is typical for the expected interface to be created as a pure interface class, especially in languages such as Java that do not support multiple inheritance."* – Jason C Apr 03 '14 at 22:49
  • 1
    @JasonC I did misread aliteralmind's comment - but that means he's wrong then. This is straight from the [Gang of Four's book - Elements of reusable object-oriented software](http://paginas.fe.up.pt/~aaguiar/as/gof/hires/pat4afso.htm). The class adapter uses multiple inheritance, and object adapter can be implemented just fine because it uses composition. – dcastro Apr 03 '14 at 22:54
  • The Class Adapter Pattern is only achievable if the classes being adapted have interfaces defined. If you are working with an existing codebase that cannot changed and does not use interfaces, then it will not work. – Jason Apr 03 '14 at 22:57
  • @dcastro Hmm, looking at http://stackoverflow.com/questions/9978477/difference-between-object-adapter-pattern-and-class-adapter-pattern I can see your point; in that by definition the "Class Adapter Pattern" cannot use composition to achieve the effect. I'm going to step out here, I don't particularly like trying to figure out subtle semantics of design patterns. Personally, I'm more "use the right tool for the job and ignore its label"... – Jason C Apr 03 '14 at 22:59
  • I don't know the subtleties between Object and Class Adapters. I just know how to use Adapters, as I've linked in my answer. It seems there is confusion. – aliteralmind Apr 03 '14 at 23:00
  • According to the [wikipedia link](http://en.wikipedia.org/wiki/Adapter_pattern#Class_Adapter_pattern), the Class Adapter is indeed possible in Java. I trust the GOF more, but obviously it's semantics and subtleties here. Does the OP care about the specific pattern as a theoretical question, or is he trying to figure out how to implement Adapters in Java as a practical matter? – aliteralmind Apr 03 '14 at 23:03