服务热线:13616026886

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

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

用factorymethod模式扩展mymsdntvlibrary


  最近又复习了一遍设计模式,我开始重新审视原有的应用程序结构,发现了一些应该改进的地方。比如说,我在写mymsdntvlibrary的第一个版本时就非常想让它能够很方便地支持不同种类的数据源,例如access、sql server,甚至是单纯的xml.我知道实现起来并不难,但怎样做才能最有效最有利于代码复用?想来想去,我决定在数据访问这一部分应用factory method模式。
  
  factory method是gof在design pattersn一书中给出的一种模式,gof为它做出的定义是:
  
  define an interface for creating an object, but let subclasses decide which class to instantiate. factory method lets a class defer instantiation to subclasses.
  
  简单来说,factory method的目的是想创建几个相似的(实现同一接口或继续同一父类)类中的某一个,为了达到这一目的,需要创建几个相似的creator类,通过creator类来决定创建哪一个所需的对象类。它的uml图示如下:
  
  具体到这个项目来说,我需要针对不同的数据源来创建几个不同的dbhelper(我的个人习惯是通过dbhelper来封装针对特定的数据源的访问动作),如oledbhelper、sqldbhelper等,这些helper有非常相近的结构,因此可以让它们继续于同一接口?d?didbhelper.idbhelper的定义如下:
  
  using system;
  
  using system.data;
  
  namespace musicland.msdntvlibrary.component
  
  {
  
  public interface idbhelper
  
  {
  
  dataset getall();
  
  }
  
  }
  
  注意其中给出了一个有待实现的方法getall,通过实现类对该方法的调用,可以获得应用程序所需的全部数据。
  
  接下来就是从idbhelper继续而来的两个具体数据访问辅助类。
  
  oledbhelper:
  
  using system;
  
  using system.configuration;
  
  using system.data;
  
  using system.data.oledb;
  
  namespace musicland.msdntvlibrary.component
  
  {
  
  // this is the helper class that will interact with oledb for info.
  
  internal class oledbhelper: idbhelper
  
  {
  
  private oledbconnection conn;
  
  public oledbhelper()
  
  {
  
  conn=new oledbconnection(configurationsettings.appsettings["oleconnectionstring"]);
  
  }
  
  public dataset getall()
  
  {
  
  oledbdataadapter da=new oledbdataadapter("select * from episode order by date desc", conn);
  
  dataset ds=new dataset();
  
  try
  
  {
  
  da.fill(ds, "episode");
  
  }
  
  catch (oledbexception ex)
  
  {
  
  throw ex;
  
  }
  
  finally
  
  {
  
  if (conn.state!=connectionstate.closed)
  
  conn.close();
  
  }
  
  return ds;
  
  }
  
  }
  
  }
  
  sqldbhelper:
  
  using system;
  
  using system.configuration;
  
  using system.data;
  
  using system.data.sqlclient;
  
  namespace musicland.msdntvlibrary.component
  
  {
  
  // this is the helper class that will interact with sqlserver for info.
  
  internal class sqldbhelper: idbhelper
  
  {
  
  sqlconnection conn;
  
  public sqldbhelper()
  
  {
  
  conn=new sqlconnection(configurationsettings.appsettings["sqlconncectionstring"]);
  
  }
  
  public dataset getall()
  
  {
  
  sqlcommand cmd=new sqlcommand("getall", conn);
  
  cmd.commandtype=commandtype.storedprocedure;
  
  sqldataadapter da=new sqldataadapter(cmd);
  
  dataset ds=new dataset();
  
  try
  
  {
  
  conn.open();
  
  da.fill(ds, "episode");
  
  }
  
  catch (sqlexception ex)
  
  {
  
  throw ex;
  
  }
  
  finally
  
  {
  
  if (conn.state!=connectionstate.closed)
  
  conn.close();
  
  }
  
  return ds;
  
  }
  
  }
  
  }
  
  代码很简单,分别是针对不同的数据源来获取episode表的全部内容(因为这个应用程序所需要的数访内容非常少,结构也很简单,因此一张episode表就足够了)。其中,针对access的oledbhelper我直接使用sql语句进行查询,而针对sql server的sqldbhelper里这一部分则改用了存储过程,这也就非常清晰地体现出了不同数据源的访问方式的差别。
  
  上面已经把数据访问辅助类构建完毕,接下来就该创建creator和concretecreator来动态调用这些辅助类了。ok,创建一个idbcreatro接口:
  
  using system;
  
  namespace musicland.msdntvlibrary.component
  
  {
  
  public interface idbcreator
  
  {
  
  idbhelper createdbhelper();
  
  }
  
  }
  
  通过实现该接口,我们可以获得用于创建不同的数据访问辅助类的具体类:
  
  oledbcreator:
  
  using system;
  
  namespace musicland.msdntvlibrary.component
  
  {
  
  // this is the concretecreator class that helps to
  
  // create and return the oledbhelper class.
  
  public class oledbcreator: idbcreator
  
  {
  
  public oledbcreator() {}
  
  public idbhelper createdbhelper()
  
  {
  
  return new oledbhelper();
  
  }
  
  }
  
  }
  
  sqldbcreator:
  
  using system;
  
  namespace musicland.msdntvlibrary.component
  
  {
  
  // this is the concretecreator class helps to
  
  // create and return the sqldbhelper class.
  
  public class sqldbcreator: idbcreator
  
  {
  
  public sqldbcreator() {}
  
  public idbhelper createdbhelper()
  
  {
  
  return new sqldbhelper();
  
  }
  
  }
  
  }
  
  以上两个类均实现了idbcreator接口。通过实现createdbhelper方法,就可以创建一个数据访问辅助类(oledbhelper或sqldbhelper)的实例并传回调用方,这正是我们所需要的。
  
  ok,主要框架已经搭建完毕,接下来我们只要在应用程序中通过适当的逻辑来进行调用了。我的初步设想是在。config文件中增加一个自定义的dbtype键,通过为dbtype设值来决定应用程序需要哪一种数据访问方式。配置文件部分内容如下:
  
  <appsettings>
  
  note: dbtype indicates which db need to interact with.
  
  currently:
  
  0: sqlserver
  
  1: oledb
  
  ?d?d>
  
  <add key="dbtype" value="1" />
  
  <add key="oleconnectionstring" value="provider=microsoft.jet.oledb.4.0; data source=msdntv.mdb;" />
  
  <add key="sqlconncectionstring" value="server=(local); database=mymsdntvlibrary; integrated security=sspi" />
  
  ……
  
  </appsettings>
  
  设置好dbtype以后,我们要做的只是在应用程序中读出这一配置值,然后去创建相应的dbcreator即可,代码如下:
  
  // get the corresponding idbcreator class
  
  public static idbcreator getdbcreator()
  
  {
  
  int dbtype=convert.toint32(configurationsettings.appsettings["dbtype"]);
  
  switch (dbtype)
  
  {
  
  case 0:
  
  return new sqldbcreator();
  
  case 1:
  
  return new oledbcreator();
  
  default:
  
  return new oledbcreator();
  
  }
  
  }
  
  以上代码可返回一个实现了idbcreator接口的具体dbcreator,即oledbcreator或sqldbcreator,通过调用该类所实现的createdbhelper方法即可获得应用程序所需的dbhelper:
  
  // get the corresponding idbhelper class
  
  public static idbhelper getdbhelper(idbcreator dbcreator)
  
  {
  
  return dbcreator.createdbhel

扫描关注微信公众号