服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java模式设计之数据访问对象模式

ad_qqread_mid_big">
  六.范例代码
 
  1.实现数据访问对象模式

  范例9-4时表示customer信息的持久性对象的dao范例代码。当findcustomer()被调用时,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;
   }
  }
 }

  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 thus shielded from knowing
// these implementation details.

import java.sql.*;

public class cloudscapecustomerdao implements
customerdao {

 public cloudscapecustomerdao() {
  // initialization
 }

 // the following methods can use
 // cloudscapedaofactory.createconnection()
 // to get a connection as required

 public int insertcustomer(...) {
  // implement insert customer here.
  // return newly created customer number
  // or a -1 on error
 }

 public boolean deletecustomer(...) {
  // implement delete customer here
  // return true on success, false on failure
 }

 public customer findcustomer(...) {
  // implement find a customer here using supplied
  // argument values as search criteria
  // return a value object if found,
  // return null on error or if not found
 }

 public boolean updatecustomer(...) {
  // implement update record here using data
  // from the customerdata value object
  // return true on success, false on failure or
  // error
 }

 public rowset selectcustomersrs(...) {
  // implement search customers here using the
  // supplied criteria.
  // return a rowset.
 }

 public collection selectcustomersvo(...) {
  // implement search customers here using the
  // supplied criteria.
  // alternatively, implement to return a collection
  // of value objects.
 }
 ...
}

  example 9.5 customer value object

public class customer implements java.io.serializable {
 // member variables
 int customernumber;
 string name;
 string streetaddress;
 string city;
 ...

 // getter and setter methods...
 ...
}

  example 9.6 using a dao and dao factory ?client code

...
// create the required dao factory
daofactory cloudscapefactory =
daofactory.getdaofactory(daofactory.daocloudscape);

// create a dao
customerdao custdao = cloudscapefactory.getcustomerdao();

// create a new customer
int newcustno = custdao.insertcustomer(...);

// find a customer object. get the value object.
customer cust = custdao.findcustomer(...);

// modify the values in the value object.
cust.setaddress(...);
cust.setemail(...);
// update the customer object using the dao
custdao.updatecustomer(cust);

// delete a customer object
custdao.deletecustomer(...);
// select all customers in the same city
customer criteria=new customer();
criteria.setcity("广州");
collection customerslist =
custdao.selectcustomersvo(criteria);
// returns customerslist - collection of customer
// value objects. iterate through this collection to
// get values.

...
>>>更多专题请看java对象专题
上一页 1 2 3 4 
相关内容:数据库  设计  解决方案  服务器  存储  
【收藏此页】【大 中 小】【打印】【关闭】
上一篇:java布局管理器使用方法探讨
下一篇:使用cmp2和xml处理动态数值对象


10万个软件免费高速下载
教育教学 安全相关 游戏娱乐 源码下载 编程开发 数码软件 其它类别
网络软件 联络聊天 系统工具 媒体工具 图形图像 应用软件 行业软件
·.net 数据访问架构指南
·了解 xml实现通用的数据访问
·在互联网上配置access数据访问页实例
·java数据访问对象(dao)编程模入门
·dottext源码阅读(4)--dto和数据访问
·体验asp.net 2.0 中的数据访问控件
·vb数据访问接口
·关于数据访问组件中的 80004005 错误
·数据访问技术的演变
·sql server 2005 mobile edition 3.0
 
·接口和抽象类的定义方式举例说明
·用maven制作java项目发行包
·java 基础入门 pom.xml 元素描述
·用jbuilder2007开发扩展jsf标签的插
·抽象类对象类和对象包装类
·使用java实现在文件中添加字符串
·instanceof和回调概念
·java变量的赋值与传递
·java se6调用java编译器的两种新方法
·weblogic运用db的java控件访问数据库

扫描关注微信公众号