On a project I have seen some code which was written by a former employee. The person has named it as an implementation of Adaptor pattern but I am not sure. Here is the code:
public class RowSetAdaptor implements java.io.Serializable {
private javax.sql.rowset.CachedRowSet cachedRowSet;
public RowSetAdaptor() throw SQLException {
cachedRowSet = new com.sun.rowset.CachedRowSetImpl();
}
public void populate(ResultSet resultSet) throw SQLException {
cachedRowSet.populate(resultSet);
}
public boolean next() throw SQLException {
cachedRowSet.next();
}
.... // different methods all using cachedRowSet
}
The way I see it the class RowSetAdaptor
is restricting access to CachedRowSet
interface as not all methods of CachedRowSet
interface are available in RowSetAdaptor
class. Is it really an Adaptor pattern? If not then which design pattern is being used here?
Thanks @JB Nizet , @Fuhrmanator , @Günther Franke , @vikingsteve and @Giovanni Botta for your answers.
What if I make following modifications to make it an Adapter pattern?
public interface RowSetI {
public boolean next() throws SQLException;
...
}
public class CachedRowSetAdapter implements RowSetI {
private javax.sql.rowset.CachedRowSet cachedRowSet;
public CachedRowSetAdapter() throw SQLException {
cachedRowSet = new com.sun.rowset.CachedRowSetImpl();
}
public void populate(ResultSet resultSet) throw SQLException {
cachedRowSet.populate(resultSet);
}
public boolean next() throw SQLException {
cachedRowSet.next();
}
...
}
public class JdbcRowSetAdapter implements RowSetI {
private javax.sql.rowset.JdbcRowSet jdbcRowSet;
public JdbcRowSetAdapter() throw SQLException {
jdbcRowSet = new com.sun.rowset.JdbcRowSetImpl();
}
public void populate(ResultSet resultSet) throw SQLException {
jdbcRowSet.populate(resultSet);
}
public boolean next() throw SQLException {
jdbcRowSet.next();
}
...
}
TIA
The best way to see if something's an implementation of a pattern is to map the roles/methods in the GoF definition to the implementation.
GoF Adapter has two variants (class and object adapter) which have different structures. Class adapters use multiple inheritance:
You have a class RowSetAdaptor
, which would make it a candidate for the Adapter
class in this diagram. However, RowSetAdaptor
implements only Serializable
, so I thinking it can't be this form of the Adapter pattern.
The second variant is the object adapter:
Again, RowSetAdaptor
would be the Adapter
class in this diagram. It does appear that javax.sql.rowset.CachedRowSet
would be the adaptee, since it's using that class inside of some of its methods. However, the purest GoF Adapter must implement some Target
interface and probably wrap an object Adaptee
in its constructor (CachedRowSet
is hard-coded). Some might say Serializable
is the Target
interface, but that would imply it would adapt those request()
methods. RowSetAdaptor
doesn't override anything in Serializable
that I can see.
Finally, if it's really the GoF Adapter pattern, I'd expect there to be more than one Adapter. Generally, Target
interface is designed because there is some common functionality that is implemented by different adapters (it makes little sense to have an interface with only one implementation).
Another noteworthy point is that Client
shouldn't know it's using a RowSetAdaptor
; Client
only sees an object of the Target
type. It's hard to justify using an interface with a client if the client is just going to access the implementation directly.
Perhaps you can check the code for the Client class or other adapters.
My conclusion is that based on the code you presented, it's not a convincing example of GoF Adapter pattern.
See more real examples of adapters at https://stackoverflow.com/a/13323703/1168342