Difference between object adapter pattern and class adapter pattern
Asked Answered
A

4

28

How to decide when to use object adapter and when to use class adapter?

Problem statement: To create social networking web site and provide import functionality from facebook, google plus and orkut. I am unable to decide whether to use object adapter or class adapter.

I have had look at Adapter Pattern: Class Adapter vs Object Adapter, but could not understand the essence of the difference.

Autochthon answered 2/4, 2012 at 14:43 Comment(0)
W
49

The main difference:

  • Class Adapter uses inheritance and can only wrap a class. It cannot wrap an interface since by definition it must derive from some base class.

  • Object Adapter uses composition and can wrap classes or interfaces, or both. It can do this since it contains, as a private, encapsulated member, the class or interface object instance it wraps.

The difference is subtle. Usually the later approach (favoring composition over inheritance) is the preferable as explained in the link which I'll quote here:

Object-Oriented Programing (OOP) has too well known candidates for the reuse of functionality: Inheritance (whitebox reuse) and Composition (blackbox reuse). If you try to reuse code by inheriing from a class you will make the subclass dependent on the parent class. This makes a system in many cases unnecessarily complex, less testable and makes the exchange of functionality at run time unnecessarily hard. As a [Clean Code Developer] you should follow the Liskov Substitution Principle (LSP) when you need to decide if inheritance is appropriate.

Composition means that one class uses another. You will further promote decoupling by defining the interfaces clearly. That will also give you the advantage that implementations can be easily replaced. So before you start applying the Liskov Substitution pronciple, think about the Favour Composition over Inheritance concept and ask yourselve why you shouldn't prefer composition right away.

"Because inheritance exposes a subclass to details of its parent's implementation, it's often said that 'inheritance breaks encapsulation'". (Gang of Four 1995:19)

Windpipe answered 2/4, 2012 at 17:31 Comment(0)
M
20

In Simple words, Class Adapter uses Subclassing and Object Adapter uses delegation by using composition.

Example:

class MyExistingServiceClass {
    public void show() {
        System.out.println("Inside Service method show()");        
    }
}

interface ClientInterface {
    void display();
}

class MyNewClassAdapter extends MyExistingServiceClass implements ClientInterface {
    void display() {
        show();
    }
}

Above is an example of Class Adapter. We have adapted MyExistingServiceClass to ClientInterface by calling existing show() method from inside implementation of display().

To convert this to object adapter, the code will be like :

class MyNewObjectAdapter implements ClientInterface {

    MyExistingServiceClass existingClassObject;

    void display() {
        existingClassObject.show();
    }
} 

Now when to use Object adapter in place of Class Adatper,

  1. When there is no way to subclass the class which is going to be adapted as per client's interface. Examples like, when MyExistingServiceClass is declared as final.

  2. When client expects a contract which is not an inteface but an abstract class implementation. In this case, we have no other way than to subclass the client's expected class and as we can't subclass more than one class, there is no other way than using the class to be adapted as a composition.

    abstract class AbstractClientClass {
        abstract void display();
    }
    
    class MyNewObjectAdapter extends AbstractClientClass { 
    
        MyExistingServiceClass existingClassObject;
    
        void display() {
            existingClassObject.show();
        }
    }
    
  3. When you need to adapt more than one object. Such case are when you are not directly working with object to be adapted. A good example here would be JTable class in javax.swing. This class creates a GUI (graphical user interface) table component filled with the information that your adapter feeds to it. To display data from your domain, JTable provides constructors that accept an instance of the TableModel defined in javax.swing.table. JDK provides an existing abstract implementation of TableModel with AbstractTableModel.

    class MyTableModel extends AbstractTableModel {
    
    MyDomainObject[] existingDomainObjects[];
    
    public int getColumnCount() {
        return 4;
    }
    
    public int getRowCount() {
        return existingDomainObjects.length();
    }
    
    public MyDomainObject getValueAt(int i) {
        return existingDomainObjects[i];
    }
    }
    

Here, we have adapted MyDomainObject in order to be used with AbstractTableModel.

Mescal answered 26/9, 2015 at 9:7 Comment(0)
R
7

Object adapter:

$Adapter = new MyEngine(new MyAdapter($options));
$Adapter->write('something');

Class Adapter

MyAdapter extends BaseAdapter implements AdapterInterface { ... }
$Adapter = new MyAdapter($options);
$Adapter->write('something');
Rondure answered 2/4, 2012 at 14:49 Comment(0)
P
3

A class adapter uses multiple inheritance to adapt one interface to another: (depending on your programming language: Java & C# does not support multiple inheritance)

enter image description here

An object adapter depends on object composition:

enter image description here

Images Source: Design Pattern (Elements of Reusable Object-Oriented Software) book

Peaslee answered 12/10, 2014 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.