Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object adapter design pattern

 
Tatiana Hyde
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to implement the object adapter design pattern but it is not
working out too well (actually, I'm back to going around in circles!)
(Leaving out the network side of things for now), I have created a new
interface (Database) that has the same methods as the Sun supplied "DbAccess" interface but also has extra "IOException" in the throws clause of each method. I have created another class (DataSource) that implements this new interface and this class has a private member variable (private DbAccess data = new Data())
i.e. using the object adapter design pattern.
In the Data class (which implements the Sun supplied DBAccess interface) I
have added some other methods (for example getContractors() which returns
an arrayList of all contractors). [Am I allowed to add methods to this class??]
Now, I am trying to access this getContractors() method in the client
GUIController class but my connection, which is "Database", can't
access this method. Where am I going wrong?
If I add the getContractors() method to the new "Database" interface, I can't seem to implement it in the "DataSource" class because in the "DataSource" class I can't access the "Data" class getContractors() method (because it is not part of the Sun supplied DBAccess interface).
To get the contractors, should I rather be using the "findByCriteria(String[] criteria)" method first to get a long[] of recordNumbers and then use the "readRecord(long recNo)" method to get a String[] for each record?
I really hope this makes sense to someone and I would greatly appreciate a point in the right direction.
Many thanks,
TH
 
Raj Shekhar
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tatiana,
Try this,
i. Add getContractors() implementation to Data class.
ii. Add getContractors() method to DBAccess interface.
iii. Add getContractors method signature to DataBase interface. Here this method should throw either 'Remote Exception' or 'IOException' in addition whatever it is throwing in data class.
iv. Provide implementation of getContractors() in Datasource.
Hope I didn't mess up with the names...
Raj.
 
George Marinkovich
Ranch Hand
Posts: 619
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tatiana,
I have created a new
interface (Database) that has the same methods as the Sun supplied "DbAccess" interface but also has extra "IOException" in the throws clause of each method. I have created another class (DataSource) that implements this new interface and this class has a private member variable (private DbAccess data = new Data())
i.e. using the object adapter design pattern.

This sounds right, EXCEPT that the private member variable should be:

Why this is true will be clearer in the discussion that follows.

In the Data class (which implements the Sun supplied DBAccess interface) I
have added some other methods (for example getContractors() which returns
an arrayList of all contractors). [Am I allowed to add methods to this class??]

Yes, you can add anything you want to the Data class. Just remember that anything you add to the Data class needs to be analyzed as to how it affects concurrent access to the database file and how it affects your locking scheme. The only restriction on the Data class is that it must implement DBAccess.

Now, I am trying to access this getContractors() method in the client
GUIController class but my connection, which is "Database", can't
access this method. Where am I going wrong?

If you want to access getContractors() method in the client GUIController class then it should be in the Database interface.

If I add the getContractors() method to the new "Database" interface, I can't seem to implement it in the "DataSource" class because in the "DataSource" class I can't access the "Data" class getContractors() method (because it is not part of the Sun supplied DBAccess interface).

The DataSource class should be able to implement the getContractors() method by calling the getContractors method on the data object in DataSource. The crux of the matter is the bolded text. The problem exists because your data variable is of type DBAccess rather than a Data object. Making data an object of Data, allows you to access the getContractors method in Data without going through the DBAccess interface (which is a very good thing because it isn't in the DBAccess interface nor should it be added to it). DataSource shouldn't care anything about DBAccess (it shouldn't reference DBAccess at all). DataSource implements Database. The object adapter design pattern is adapting the Database interface to Data class (well technically it's adapting it to the DBAccess interface as implemented by the Data class) so that you don't have to access the Data class using the DBAccess interface. Data must implement DBAccess (and I think it should do it explicitly "class Data implements DBAccess" to make it obvious to the examiner), but Data can implicitly implement Database (and for this scheme to work it must do so).

To get the contractors, should I rather be using the "findByCriteria(String[] criteria)" method first to get a long[] of recordNumbers and then use the "readRecord(long recNo)" method to get a String[] for each record?

You can do it either way you want to. Since you are aware that there are two ways to do it you should try to analyze the pros and cons of doing it each way. Having the getContractors in Data may be more efficient when you consider network traffic, but it means you have to make sure getContractors is implemented in a thread-safe manner in Data just like all the other database operations.
 
Tatiana Hyde
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a million George! That has really cleared things up for me.
Congratulations on your awesome results btw.
TH.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic