Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Adapter pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Adapter pattern" Watch "Adapter pattern" New topic

Adapter pattern

Rob van Oostveen

Joined: Aug 14, 2006
Posts: 21
Fellow ranchers,

I received the URLyBird 1.2.1 assignment recently. I'm considering the Adapter pattern for Data objects. Let me be more specific;

Data class implements DBAccess and is wrapped in an adapter class. Every client thread in the server application will create it's own adapter object. The Data object will be passed to each new adapter object. This way, only one instance of the Data object will exist. The adapter class will have a small number of some specific methods.

Is this a correct implementation of the adapter pattern?

Please let me know your thoughts.

Thank you.

Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Rob,

I think you are confusing the Adapter Pattern with the Composition Pattern.

I think everything you said above is better applied to the Composition Pattern once you have a scenario where one class wrappers another class to add some functionality. (If I did not misunderstand you of course!)

Think about the classes : BufferedWriter takes in its constructor a FileWriter which in turn takes in its constructor a File class.

BufferedWriter = new BufferedWriter(new FileWriter(new File("myfile.txt")));

If you have in your app the scenario I explained above, for sure you are using the Composition Pattern instead of Adapter Pattern.

On the other hand, the Adapter Pattern is for those situations where you want to convert a class reference for another class type expected by your program and that is not on the same class hierarchy of the class being converted.

This pattern does not add functionality at all. It only changes some class interface to another identical interface always maintaing the same functionalities.

Example :

Supposing you have the following class

Sometimes we can get confused with the Composition Pattern because of the wrapped properties needed to implement the Adapter class, in this case the private generalDuck property. But the key point here is : No functionality was added.

Did you get the point ?

I think anyone else can give us a better explanation but I think I'm on the right track.

Best Regards.
[ September 05, 2006: Message edited by: Edisandro Bessa ]

"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Rob van Oostveen

Joined: Aug 14, 2006
Posts: 21

Thank you for your answer. I think I understand what you are saying. And in my opinion I am not adding functionality, I'm just simplifying it. Here's an example of what I'm trying to do;

And if I'm correct. It's even better to have an interface defining
how to implement the AdapterData class. This way the class can be
substituted by another whenever the database source changes in the
future as long as the new AdapterData class implements the interface.

Best regards,

[ September 05, 2006: Message edited by: Rob van Oostveen ]
Rob van Oostveen

Joined: Aug 14, 2006
Posts: 21
I'd like to add that the new AdapterData object holds the client cookie as well. And is used by the methods who need the client cookie.

This doesn't change functionality in my opinion.


[ September 05, 2006: Message edited by: Rob van Oostveen ]
Oricio Ocle
Ranch Hand

Joined: Nov 30, 2004
Posts: 284

Adapter pattern is also known as Wrapper (@see GOF).

But IMO there is a slight difference:
- Adapting suposes have in mind a fixed client interface that must be implemented. Clients already exist.

- In wrapping: Clients dont exist yet. You are not restricted to a fixed interface.

Once you see the overall design is difficult or even imposible to distinguish between them. The difference is in the analisis process, not in the result...

That is how i understand both.

So in your case i would name it "wrapper" not "adapter"

On the other hand... i dont know any composition pattern...
Are you,Edisandro, refearing to composite pattern?

[ September 06, 2006: Message edited by: Oricio Ocle ]

Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Some authors give different names to the same things.

In my explanation the Composition Patterns would be considered as a kind of Wrapper Pattern.
[ September 06, 2006: Message edited by: Edisandro Bessa ]
I agree. Here's the link:
subject: Adapter pattern
It's not a secret anymore!