Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

What is difference between Class Adapter and Object Adapter pattern?

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is difference between Class Adapter and Object Adapter pattern?
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pattern name is always a very confuse. Can you give a little bit more info on both patterns you mentioned.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Avijeet Dash
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
makes sense.
Thanks.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a college professor, "makes sense" are the words I always long to here! Thanks!
------------------
Moderator of the JDBC Forum
 
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this what the object adapter?
this.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
regards,
Guru
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.......

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic