background image

 
  Bridge 模式中参与者还需要有行为接口的具体实现(ConcreteImplementor),在本例中是
CatalogDAOImpl,虽然在目前宠物店中只有一个 ConcreteImplementor,但是可扩展为到
Mysql XML 等数据源访问,比如你可以自己新增一个叫 CatalogDAOImplMysql,也是作为
CatalogDAO 的子类。
  看看 CatalogDAO 的一个子类 CatalogDAOImpl 的代码:
  

public

 

class

 CatalogDAOImpl 

implements

 CatalogDAO {

  

protected

 

static

 DataSource getDataSource()

  

throws

 CatalogDAOSysException {

  

try

 {

  InitialContext ic = 

new

 InitialContext();

  

return

 (DataSource) ic.lookup(JNDINames.CATALOG_DATASOURCE);

  }
  

catch

 (NamingException ne) {

  

throw

 

new

 CatalogDAOSysException("NamingException while looking "

  + "up DB context : "
  + ne.getMessage());
  }
  }
  

//具体 Select 语句在这里出现,这里主要是 Oracle 数据库的访问语句

 
  

public

 Category getCategory(String categoryID, Locale l)

  

throws

 CatalogDAOSysException {

  Connection c = 

null

;

  PreparedStatement ps = 

null

;

  ResultSet rs = 

null

;

  Category ret = 

null

;

  

try

 {

  c = getDataSource().getConnection();
  ps = c.prepareStatement("select a.catid, name, descn "
  + "from (category a join "
  + "category_details b on "
  + "a.catid=b.catid) "
  + "where locale = ? "
  + "and a.catid = ?",
  ResultSet.TYPE_SCROLL_INSENSITIVE,
  ResultSet.CONCUR_READ_ONLY);
  ps.setString(

1

, l.toString());

  ps.setString(

2

, categoryID);

  rs = ps.executeQuery();
  

if

 (rs.first()) {

  ret = 

new

 Category(rs.getString(

1

).trim(),

  rs.getString(

2

),

  rs.getString(

3

));