wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes What is difference between Class Adapter and Object Adapter pattern? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "What is difference between Class Adapter and Object Adapter pattern?" Watch "What is difference between Class Adapter and Object Adapter pattern?" New topic
Author

What is difference between Class Adapter and Object Adapter pattern?

Avijeet Dash
Ranch Hand

Joined: Jan 21, 2001
Posts: 148
What is difference between Class Adapter and Object Adapter pattern?
Laojar Chuger
Ranch Hand

Joined: Dec 20, 2000
Posts: 111
Pattern name is always a very confuse. Can you give a little bit more info on both patterns you mentioned.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I just had to do a presentation on this so it is funny you asked:
The difference is that an object adapter contains the adaptee while the class adapter inherits from the adaptee. Which to use is dependent on the specific requirements of your application. For example, if there are multiple adaptees, then you would use the object adapter.
Here is some sample code. In this case the old class is called OldReporter (and has been converted into an interface), the new class (the adaptee) is called NewReportWriter, and the adapter is named ReportGenerator.

BTW, The GoF book does a nice job of explaining this pattern.
[This message has been edited by Thomas Paul (edited March 22, 2001).]


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Avijeet Dash
Ranch Hand

Joined: Jan 21, 2001
Posts: 148
makes sense.
Thanks.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
As a college professor, "makes sense" are the words I always long to here! Thanks!
------------------
Moderator of the JDBC Forum
Gurumurthy Ramamurthy
Ranch Hand

Joined: Feb 13, 2003
Posts: 272
Is this what the object adapter?
this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
regards,
Guru
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
No, the AWT adapter classes do have nothing in common with the GOF Adapter patterns - they are simply empty stub implementations of interfaces. In fact I don't know why the Sun programmers decided to call them adapters - it's awkward, in my humble opinion...
Imagine you do have an Enumeration object and want to pass it to a method which expects an Iterator. You could do this by wrapping the Enumeration object into an implementation of the Iterator interface which just delegates calls to the appropriate methods of the Enumeration. That wrapper object would be an Object Adapter.
Did that help?


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
RanjithSekar SekarRanjith
Greenhorn

Joined: Jul 05, 2013
Posts: 1
public class AdapteePrintString {
public void printString(String printStr) {
System.out.println("inside AdapteePrintString>> " + printStr);
}
}

import java.util.List;
public interface TargetPrintList {
void printList(List<String> list);
}

Class Adapter:
import java.util.List;

public class AdapterPrintable extends AdapteePrintString implements TargetPrintList {
@Override
public void printList(List<String> list) {
String printString = "";
for (String str : list) {
printString = printString + " " + str;
}
printString(printString);
}
}


Object Adapter:
import java.util.List;
public class AdapterPrintable implements TargetPrintList {
@Override
public void printList(List<String> list) {
String printString = "";
for (String str : list) {
printString = printString + " " + str;
}
AdapteePrintString adapteePrintString = new AdapteePrintString();
adapteePrintString.printString(printString);
}
}

Testing the Adapters:
import java.util.ArrayList;
import java.util.List;
public class ClientTestAdapter {
public static void main(String[] args) {
TargetPrintList print = new AdapterPrintable();
List<String> myList = new ArrayList<String>();
myList.add("Ranjith");
myList.add("Sekar");
print.printList(myList);
}
}

Hope helps.......

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is difference between Class Adapter and Object Adapter pattern?
 
Similar Threads
Proxy Vs Decorative pattern
difference between classes??
adapter-pattern
Design Patterns Question
Why anonymous inner class?