快速业务通道

Java模式设计之数据访问对象模式 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-23
ndCustomer()被调用时,CloudscapeCustomerDAO创建一个Customer值对象。

范例9-6是使用DAO的范例代码。

2.实现数据访问对象的工厂策略

1)使用工厂方法模式

2)使用抽象工厂模式

范例代码9-2是CloudscapeDAOFactory的范例代码。

范例代码9-3中的CustomerDAO接口为Customer持久性对象定义了DAO方法,这些接口是被所有具体DAO实现来实现的,比如CloudscapeCustomerDAO、OracleCustomerDAO、已经SybaseCustomerDAO。Account和OrederDAO接口也与此类似。

Example 9.1 Abstract DAOFactory Class

// Abstract class DAO Factory public abstract class DAOFactory {   // List of DAO types supported by the factory   public static final int CLOUDSCAPE = 1;   public static final int ORACLE = 2;   public static final int SYBASE = 3;   ...   // There will be a method for each DAO that can be   // created. The concrete factories will have to   // implement these methods.   public abstract CustomerDAO getCustomerDAO();   public abstract AccountDAO getAccountDAO();   public abstract OrderDAO getOrderDAO();   ...   public static DAOFactory getDAOFactory(    int whichFactory) {       switch (whichFactory) {      case CLOUDSCAPE:       return new CloudscapeDAOFactory();      case ORACLE :       return new OracleDAOFactory();      case SYBASE :       return new SybaseDAOFactory();       ...      default :       return null;     }    }   }

Java模式设计之数据访问对象模式(5)

时间:2010-12-06

Example 9.2 Concrete DAOFactory Implementation for Cloudscape

// Cloudscape concrete DAO Factory implementation import java.sql.*; public class CloudscapeDAOFactory extends DAOFactory {   public static final String DRIVER="COM.cloudscape.core.RmiJdbcDriver";   public static final String DBURL="jdbc:cloudscape:rmi://localhost:1099/CoreJ2EEDB";   // method to create Cloudscape connections   public static Connection createConnection() {    // Use DRIVER and DBURL to create a connection    // Recommend connection pool implementation/usage   }   public CustomerDAO getCustomerDAO() {    // CloudscapeCustomerDAO implements CustomerDAO    return new CloudscapeCustomerDAO();   }   public AccountDAO getAccountDAO() {    // CloudscapeAccountDAO implements AccountDAO    return new CloudscapeAccountDAO();   }   public OrderDAO getOrderDAO() {    // CloudscapeOrderDAO implements OrderDAO    return new CloudscapeOrderDAO();   }   ... }

Example 9.3 Base DAO Interface for Customer

// Interface that all CustomerDAOs must support public interface CustomerDAO {   public int insertCustomer(...);   public boolean deleteCustomer(...);   public Customer findCustomer(...);   public boolean updateCustomer(...);   public RowSet selectCustomersRS(...);   public Collection selectCustomersVO(...);   ... }

Example 9.4 Cloudscape DAO Implementation for Customer

// CloudscapeCustomerDAO implementation of the // CustomerDAO interface. This class can contain all // Cloudscape specific code and SQL statements. // The client is

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号