File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Is this an  Adapter Pattern? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Is this an  Adapter Pattern?" Watch "Is this an  Adapter Pattern?" New topic
Author

Is this an Adapter Pattern?

jyothi sunke
Ranch Hand

Joined: Sep 11, 2002
Posts: 65
Hi,
Here is the part of description of my design..
I implemented all three methods(criteriaFind, lock and unlock) in Data class itself(This will be in db package).

In client package,
I wrote an interface(DataInterface) which has all the public methods of Data class. And there is a LocalData class.
When application runs in the local mode, the application connect to database through LocalData class. This implements DataInterface and as it adapts the Data class and simply invokes corresponding methods in Data class while implementing the methods of DataInterface. like this...
public class LocalData implements DataInterface{
Data data;
LocalData(String databaseFileName){
data = new Data(databaseFileName);
}
public int getRecordCount(){
return data.getRecordCount();
}
public void add(String[] newData){
data.add(newData);
}
}
my question is that can I say that LocalData class Adapted the Data class in implemeting the methods of DataInterface???
[ November 13, 2002: Message edited by: jyothi sunke ]

jyothi
Dave Teare
Ranch Hand

Joined: Oct 09, 2002
Posts: 80
That is a good question. I will post my 2 cents, and hopefully others will validate (or disprove) my thoughts.
First, lets get out the text book for what a structural object is (adapter is a child of this class of patterns):
Structural object patterns create composite objects by aggregating individual objects to build larger structures.

To me, this means an adapter needs to contain more than one ojbect, as is the case with your example. Since your "adapter" implements the exact same interface as the object it contains, I do not think it is a true adapter.
I would suggest that your code is actually a Data Access Object since it is "hiding" the underlying data accesses . However, since the requirements state the client must implement all the public methods of the Data class, it is hard to see how the DAO is "hiding" the actual underlying database.
As a side note, I have a class DBConnection which is very similar to yours, except it also contains a lock manager. In this case, I have said the class uses the Adapter pattern (because it is comprised of multiple objects) as well as a DAO (because is 'hides' the underlying database.)
In my opinion, in many cases the exact pattern used can be interpreted in many ways. For example, is my DBConnection class also a Facade because it

resides on the business tier, acts as an entry point into the business tier, and manages the workflow?

I would say yes! Therefore, in my example, my single class DBConnection is a DAO, Adapter, and a Facade!!!
Perhaps my DBConnection is too large, or not proper OOD because it matches so many patterns. However, IMO, my DBConnection provides a clean, clear interface which accomplishes a focused set of tasks. Therefore, I don't mind documenting that this class uses multiple patterns.
I hope this helps!
--Dave.
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
Hi Jyothi,
I'd say it's a clear case of a Delegation pattern, since your class seems to delegate everything to the Data.class.
Besides, your naming (class name "LocalData", and member "localData") does not seem very intuitive to me. I named it something like LocalDataAccess.
Hope this helps.


Mag
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
Hi Dave,
Originally posted by Dave Teare:
Perhaps my DBConnection is too large, or not proper OOD because it matches so many patterns. However, IMO, my DBConnection provides a clean, clear interface which accomplishes a focused set of tasks. Therefore, I don't mind documenting that this class uses multiple patterns.

Patterns are abstractions that are intended to make clear how a number of classes interact. A pattern usually describes how a particular class interacts with other classes. Thus a class can play the role of an Adapter to another class, and at the same time be a Facade to a complete system.
If you're afraid of having one fat multi-purpose class, check if it is possible to replace it by a small hierarchy, with each subclass adding a more specialized aspect.
Hope this helps.
jyothi sunke
Ranch Hand

Joined: Sep 11, 2002
Posts: 65
Hi Mag & Dave,
Thank you very much for ur responces. But I didn't get the clear picture.
Mag, u r saying that it is delegation right!! how difference is it from adapter pattern??
I went through two articles in Javaworld and then only I posted the above question..
http://www.javaworld.com/javaworld/jw-05-1999/jw-05-networked.html
http://www.javaworld.com/javaworld/jw-01-2001/jw-0112-rmi.html
I implemented my design on the basis of these two articles. Not only in the local mode, I used this adapter pattern in the implemetation class of remote interface on the serverside.
please guide.. as I am thinking that I used an adapter pattern and wrote the same in DesignChoices document..
Thank you..
jyothi sunke
Ranch Hand

Joined: Sep 11, 2002
Posts: 65
Look at my posting...and give me ur opinion..
Thanks in advance..
Mag Hoehme
Ranch Hand

Joined: Apr 07, 2002
Posts: 194
Hi Jyothi,
what's the difference between Delegation and Adapter... I'm pretty bad at giving good explanations, but I'll try.
Delegation
Delegation means that you have a class (or a method) that delegates the job to some other class (or method):

Adapter
The Adapter is more complex than Delegation, and in fact, the Adapter uses Delegation. For an Adapter you need three parties: One class that does the job, one class that wants the job to be done. But for some reason the second class cannot be called directly. To solve this problem, you can use an Adapter.
A typical situation for using an Adapter is the following one: You have an app that implements - let's say - some home-made logging. Now you come across a really good logging framework and you want to use it instead of your own one. However, the interfaces of your own logging and the logging framework do not match. Just as with an electric device, you put an Adapter between your app and the logging framework (instead of revamping your app). The Adapter implements the interface of your own logging and uses Delegation to connect it to the logging framework.

And this could be the Adapter between thw two:

So, when you speak about Delegation, you simply state that the job is delegated to some other class. You do not exactly say why. When you speak about an Adapter, you imply that there are two incompatible interfaces involved.
Hope this helps.
[ November 14, 2002: Message edited by: Mag Hoehme ]
jyothi sunke
Ranch Hand

Joined: Sep 11, 2002
Posts: 65
Thank you very much Mag... Now I understood the difference between delegation and adapter patterns..From ur explanation, I understood that in localmode implementation I used delegation and in networkmode implemantation I used adapter pattern...Thanks once again..
Nicolas Duchenne
Greenhorn

Joined: Nov 18, 2002
Posts: 1
Hi folks ! First post on JavaRanch.
Would not all this look more like a proxy in both local and network mode ?
In either case, you provide your client with an instance of DataInterface (which the Data class could implement) without your client knowing that the chain of responsibilities may differ between the two situations.
By the way, if your LocalData class does not add any service to the Data class and is only a "method forwarder", I do not see a real need for it. Having Data explicitely implement DataInterface seems just fine with me. In the future, if you happen to need some layer of treatments on top of your Data class in local mode, you would always be able to provide your client with a relevant... proxy.
Being as new at all this as you are, I think that my opinion would need a confirmation.
Proxy
Provide a surrogate or placeholder for another object to control access to it.

Rules of thumb
Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.
Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests.

Nicolas
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is this an Adapter Pattern?