プロジェクトで、元従業員が書いたコードを見たことがあります。その人はそれをアダプターパターンの実装と名付けましたが、よくわかりません。コードは次のとおりです。
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
}
インターフェイスのすべてのメソッドがクラスで使用できるわけではないため、クラスRowSetAdaptor
がインターフェイスへのアクセスを制限していることがわかります。それは本当にアダプターパターンですか?そうでない場合、ここで使用されている設計パターンはどれですか?CachedRowSet
CachedRowSet
RowSetAdaptor
更新 [2015 年 2 月 24 日]
@JB Nizet、@Fuhrmanator、@Günther Franke、@vikingsteve、@Giovanni Botta に感謝します。
以下のような修正を加えて Adapter パターンにするとどうなりますか?
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();
}
...
}
ティア