PetStore中EJB的设计模式 目录 刘兵(bliu76 aveah. net) 目录 PetStore中EJB的设计模式 概述 2.模式分类 21. Data Access Object模式 2.1.1.意图 2.1.2.动机 2.1.3.结构 2.14. PetStore的例子 2.14.1 概述 2. 1.4.2 CatalogEJB 4456689 2.1.4.3 CatalogDAO 2. 1.4.4 Catalog DAOlmpl 2. 1.4.5 Catalog DAOFactory 2.1.5.GOF模式关系 2146 Bridge模式 2147 Adapter模式 0011 2.2 Value Object模式 2.2 意图 222.动机 22.3.结构 224. PerStore的例子 2.3 Session Facade模式 23.1.意图 232.动机 233.结构 34. PetStore中的例子 2.14.8 概述 667888 2. 1. 4.9 Shopping Client FacadelocalEJB 235.GOF中 Fade模式 24 Fast- Lane reader模式 241.意图 242.动机 243.结构 244. PetStore中的例子
PetStore 中 EJB 的设计模式 1/1 目录 刘兵(bliu76@yeah.net) 目录...............................................................................................................................................1 PetStore 中 EJB 的设计模式............................................................................................................3 1. 概述...........................................................................................................................................3 2. 模式分类...................................................................................................................................4 2.1. Data Access Object 模式 ..........................................................................................4 2.1.1. 意图...................................................................................................................4 2.1.2. 动机...................................................................................................................4 2.1.3. 结构...................................................................................................................5 2.1.4. PetStore 的例子 ................................................................................................6 2.1.4.1 概述...................................................................................................6 2.1.4.2 CatalogEJB................................................................................................8 2.1.4.3 CatalogDAO..............................................................................................9 2.1.4.4 CatalogDAOImpl ......................................................................................9 2.1.4.5 CatalogDAOFactory................................................................................10 2.1.5. GOF 模式关系................................................................................................10 2.1.4.6 Bridge 模式.............................................................................................10 2.1.4.7 Adapter 模式...........................................................................................11 2.2. Value Object 模式...................................................................................................11 2.2.1. 意图.................................................................................................................11 2.2.2. 动机.................................................................................................................13 2.2.3. 结构.................................................................................................................14 2.2.4. PerStore 的例子..............................................................................................14 2.3. Session Façade 模式 ...............................................................................................16 2.3.1. 意图.................................................................................................................16 2.3.2. 动机.................................................................................................................16 2.3.3. 结构.................................................................................................................17 2.3.4. PetStore 中的例子 ..........................................................................................18 2.1.4.8 概述.................................................................................................18 2.1.4.9 ShoppingClientFacadeLocalEJB.............................................................18 2.3.5. GOF 中 Facde 模式 ........................................................................................19 2.4. Fast-Lane Reader 模式 ...........................................................................................20 2.4.1. 意图.................................................................................................................20 2.4.2. 动机.................................................................................................................20 2.4.3. 结构.................................................................................................................21 2.4.4. PetStore 中的例子 ..........................................................................................21
pest中EB的设计模式 2.1 410 Catalogdao 2 2.14.11 Catalog Weblmpl ProductList'tag 2.5 Page-by- Page Iterator模式 意图 252.动机 253.结构 3422 254. PetStore的例子 2.1.4.13 Catalog DAOlmp 2.1.4.14 ProductltemListTag… 2.55.GOF的 Iterator模式 2.6 Servicelocator模式 2.6.1.意图 动结 机 构 264. PetStore中的例子 2. 1.4.15 ServiceLocatorlmp 2.1.4.16 我们项目的例子 2.6.5.GOF模式 21417 Singleton模式 27.Loca模式 222 777 意动结 图机构 333 274. PetStore例子 2.8 CMR模式 2.8.1.意图 282.动机 283.结构 284. PetStore中的例子 业务代表模式 1.意图 29.2.动机 29.3.结构 294. PetStore中的例子 29.5.GOF模式 3344 21418 Proxy模式 3.EJB模式关系 4.相关的资料 5.总结
PetStore 中 EJB 的设计模式 2/2 2.1.4.10 CatalogDAO....................................................................................21 2.1.4.11 CatalogWebImpl..............................................................................21 2.1.4.12 ProductListTag ................................................................................22 2.5. Page-by-Page Iterator 模式.....................................................................................23 2.5.1. 意图.................................................................................................................23 2.5.2. 动机.................................................................................................................24 2.5.3. 结构.................................................................................................................25 2.5.4. PetStore 的例子 ..............................................................................................25 2.1.4.13 CatalogDAOImp .............................................................................25 2.1.4.14 ProductItemListTag.........................................................................26 2.5.5. GOF 的 Iterator 模式......................................................................................27 2.6. ServiceLocator 模式 ...............................................................................................27 2.6.1. 意图.................................................................................................................28 2.6.2. 动机.................................................................................................................28 2.6.3. 结构.................................................................................................................28 2.6.4. PetStore 中的例子 ..........................................................................................29 2.1.4.15 ServiceLocatorImpl.........................................................................29 2.1.4.16 我们项目的例子.............................................................................30 2.6.5. GOF 模式........................................................................................................32 2.1.4.17 Singleton 模式 ................................................................................32 2.7. Local 模式...............................................................................................................33 2.7.1. 意图.................................................................................................................33 2.7.2. 动机.................................................................................................................33 2.7.3. 结构.................................................................................................................33 2.7.4. PetStore 例子 ..................................................................................................35 2.8. CMR 模式...............................................................................................................35 2.8.1. 意图.................................................................................................................35 2.8.2. 动机.................................................................................................................35 2.8.3. 结构.................................................................................................................35 2.8.4. PetStore 中的例子 ..........................................................................................36 2.9. 业务代表模式.........................................................................................................37 2.9.1. 意图.................................................................................................................38 2.9.2. 动机.................................................................................................................38 2.9.3. 结构.................................................................................................................39 2.9.4. PetStore 中的例子 ..........................................................................................39 2.9.5. GOF 模式........................................................................................................41 2.1.4.18 Proxy 模式 ......................................................................................41 3. EJB 模式关系.........................................................................................................................42 4. 相关的资料.............................................................................................................................43 5. 总结.........................................................................................................................................43
PetStore中EJB的设计模式 Petstore中EJB的设计模式 Liubing(bliu76@yeah.net) 1.概述 通过分析数据模型,和实际操作 PetStore我们大概对它的数据模型有所了解。现在开始 分析系统EJB层的设计模式。首先什么是模式?看Gof的《设计模式》的定义,就是对被 用来在特定场景下解决一般设计问题的类和相互通信的对象描述。唉!太深沉了。一头雾水。 其实,就是一些经验,并且经过科学整理,可以在某种场合下反复使用,解决一些问题。当 然在《设计模式》一书罗列出23中模式,并且进行了详细描述。今天我们所说的模式,是 PetStore中的EJB设计经验 关于设计模式,我有很多感触,Gof的书我不知看了多少遍,可总是不太明白,我几年 没有使用C艹+了,里面的例子,看着很不爽。板桥先生网上的例子很简单明了,但是经过 段时间的学习,有点不尽兴的感觉。于是又看了其他的几本书,心中有了一些感觉,可是实 际写代码很难用上,并且现在项目使用J2EE,于是我决定分析 PetStore源码,看看这些牛 人是怎样使用模式的。在我们的项目开发中也照葫芦画瓢,使用一把。 关于 Petstore的模式,SUN的站点列出了以下几种模式,当然其中的 Frontcontroller属 于Web层的 Pattern Inte Data Access Decouple business logic from data access logic and adapt the resource being ec accessed, so that the type of resource can change easily and independently accelerate read-only data access by not using enterprise beans Reader Front Controller Centralize view management(navigation, templating, security, etc. ) for a Web application in a single object that handles incoming client requests Page-bY-Page Efficiently access a large, remote list by retrieving its elements one sublist of Iterator value objects at a time Session Facade Provide a unified, workflow-oriented interface to a set of enterprise beans Efficiently transfer remote, fine-grained data by sending a coarse-grained Value object view of the data 在《J2EE核心模式》一书中列出了以下几种模式,当然包括了以上的几种模式。这本书不 错,可惜翻译的很次。 业务代表 值对象 值对象组装器
PetStore 中 EJB 的设计模式 3/3 PetStore 中 EJB 的设计模式 Liubing (bliu76@yeah.net) 1. 概述 通过分析数据模型,和实际操作 PetStore 我们大概对它的数据模型有所了解。现在开始 分析系统 EJB 层的设计模式。首先什么是模式?看 Gof 的《设计模式》的定义,就是对被 用来在特定场景下解决一般设计问题的类和相互通信的对象描述。唉!太深沉了。一头雾水。 其实,就是一些经验,并且经过科学整理,可以在某种场合下反复使用,解决一些问题。当 然在《设计模式》一书罗列出 23 中模式,并且进行了详细描述。今天我们所说的模式,是 PetStore 中的 EJB 设计经验。 关于设计模式,我有很多感触,Gof 的书我不知看了多少遍,可总是不太明白,我几年 没有使用 C++了,里面的例子,看着很不爽。板桥先生网上的例子很简单明了,但是经过一 段时间的学习,有点不尽兴的感觉。于是又看了其他的几本书,心中有了一些感觉,可是实 际写代码很难用上,并且现在项目使用 J2EE,于是我决定分析 PetStore 源码,看看这些牛 人是怎样使用模式的。在我们的项目开发中也照葫芦画瓢,使用一把。 关于 PetStore 的模式,SUN 的站点列出了以下几种模式,当然其中的 FrontController 属 于 Web 层的。 Pattern Intent Data Access Object Decouple business logic from data access logic and adapt the resource being accessed, so that the type of resource can change easily and independently. Fast-Lane Reader Accelerate read-only data access by not using enterprise beans. Front Controller Centralize view management (navigation, templating, security, etc.) for a Web application in a single object that handles incoming client requests. Page-by-Page Iterator Efficiently access a large, remote list by retrieving its elements one sublist of value objects at a time. Session Facade Provide a unified, workflow-oriented interface to a set of enterprise beans. Value Object Efficiently transfer remote, fine-grained data by sending a coarse-grained view of the data. 在《J2EE 核心模式》一书中列出了以下几种模式,当然包括了以上的几种模式。这本书不 错,可惜翻译的很次。 业务代表 值对象 值对象组装器
PetStore中EJB的设计模式 值对象列表 会话外观 服务器定位 数据访问对象 服务激发器 我们首先知道EJB层有这些模式,我把这些模式简单介绍一下,包括它的意图,动机 结构,然后看在 PetStore中使用了那些,又是怎样使用的。我想把我所理解的写出,如果不 对的地方,请大家指出。 2.模式分类 21. Data Access obiect模式 这个模式SUN上有详细的描述,并且《程序员6期》中有板桥先生的一遍文章介绍了它。 这模式使用了GoF中 Bridge和 Adapter模式。如果大家不熟悉,最好看下Gof的介绍,现 在我感觉GoF的书当作一个模式参考手册比较不错。一般刚开始就看这本,很难看懂,这 样就打消了大家的积极性,这年头,有学习上进的想法就很不错了。在被人打击,太不好了。 211.意图 Decouple business logic from data access logic and adapt the resource being accessed, so that the type of resource can change easily and independently.(算了我不翻译了,实际就是分离业 务逻辑和数据读取,把数据的读取写到了DAO中。这样增强了系统的灵活性。如果你看过 GoF的书,这个口号你一定记忆有心(针对接口编程,而不是针对实现编程),当然了老米 的态度决定一切也很有哲理。设计模式就是把调用与实现分离,如果你感觉,很罗嗦被分的 乱七八糟,实现一个东西要好多类和接口,这样你就对设计模式有了一定的了解了。) 212.动机 These problems can be avoided by removing data access logic from enterprise beans and abstracting data access functionality into a separate interface. Enterprise beans carry out their business logic in terms of operations on that interface, which is implemented by a data access object(DAO) appropriate for the type of resource being used.(就是业务逻辑中不负责数据的存取,把它放到DA0中,这些数据的 格式,提供商一旦发生改变,只需修改DAO即可) For example, the Java Pet Store applications OrderEJB component accesses a database through its associated OrderDAO class, which knows how to perform load, save, and find operations on order data in the
PetStore 中 EJB 的设计模式 4/4 值对象列表 会话外观 服务器定位 数据访问对象 服务激发器 我们首先知道 EJB 层有这些模式,我把这些模式简单介绍一下,包括它的意图,动机, 结构,然后看在 PetStore 中使用了那些,又是怎样使用的。我想把我所理解的写出,如果不 对的地方,请大家指出。 2. 模式分类 2.1. Data Access Object 模式 这个模式 SUN 上有详细的描述,并且《程序员 6 期》中有板桥先生的一遍文章介绍了它。 这模式使用了 GoF 中 Bridge 和 Adapter 模式。如果大家不熟悉,最好看下 Gof 的介绍,现 在我感觉 GoF 的书当作一个模式参考手册比较不错。一般刚开始就看这本,很难看懂,这 样就打消了大家的积极性,这年头,有学习上进的想法就很不错了。在被人打击,太不好了。 2.1.1. 意图 Decouple business logic from data access logic and adapt the resource being accessed, so that the type of resource can change easily and independently.(算了我不翻译了,实际就是分离业 务逻辑和数据读取,把数据的读取写到了 DAO 中。这样增强了系统的灵活性。如果你看过 GoF 的书,这个口号你一定记忆有心(针对接口编程,而不是针对实现编程),当然了老米 的态度决定一切也很有哲理。设计模式就是把调用与实现分离,如果你感觉,很罗嗦被分的 乱七八糟,实现一个东西要好多类和接口,这样你就对设计模式有了一定的了解了。) 2.1.2. 动机 These problems can be avoided by removing data access logic from enterprise beans and abstracting data access functionality into a separate interface. Enterprise beans carry out their business logic in terms of operations on that interface, which is implemented by a data access object (DAO) appropriate for the type of resource being used. (就是业务逻辑中不负责数据的存取,把它放到 DAO 中,这些数据的 格式,提供商一旦发生改变,只需修改 DAO 即可) For example, the Java Pet Store application's OrderEJB component accesses a database through its associated OrderDAO class, which knows how to perform load, save, and find operations on order data in the
pest中EB的设计模式 persistent store. Because the bean delegates its persistence-related asks to the DAo, it can concentrate on implementing business method At deployment time, the application administrator configures the implementation of OrderdAo to be one of OrderDAOCS, OrderDAOOracle, or OrderDAOSybase, depending on whether an Cloudscape, Oracle, or Sybase database is used, No matter what choice is made. the orderejb is unaffected, because it is programmed to an interface, not to an implementation.(很简单,我就不翻译了吧,要不就被我糟蹋原意了)。 不过我现在使用的是JB6.0+ Weblogic6。1开发bmp,我们大多使用了JB的 向导,它自动生成了代码,可是它没有实现这种模式.在 Sessionbean中我们 封装了JDBC的数据读取,有点向这种模式 213.结构 它使用了GOF中的 Bridge和 Adapter模式。整个结构图如下 Brdge: Implementor Bda:b cion Budge: Concrutamplamenbor idoe: Conen almplamanbor oid dataAccessMethod 2 roid data accesskath 6 id data.Acc色 大家可能看明白,其实就是在 Businessobject中提供了业务逻辑方法,假如它必须要读取 数据,但是这个数据可能存在 Oracle数据库中,又有可能存在exce文件中,也许存在XML 文件中,如果我们在这个方法中直接读取数据,那就要写这样的语句 witch (DATA TYPE) CLE. 通过JDBC读取 Oracle数据 通过专门的AP进行读取, Excel文件
PetStore 中 EJB 的设计模式 5/5 persistent store. Because the bean delegates its persistence-related tasks to the DAO, it can concentrate on implementing business methods. At deployment time, the application administrator configures the implementation of OrderDAO to be one of OrderDAOCS, OrderDAOOracle, or OrderDAOSybase, depending on whether an Cloudscape, Oracle, or Sybase database is used. No matter what choice is made, the OrderEJB is unaffected, because it is programmed to an interface, not to an implementation.(很简单,我就不翻译了吧,要不就被我糟蹋原意了)。 不过我现在使用的是 JB6.0+Weblogic6。1 开发 bmp,我们大多使用了 JB 的 向导,它自动生成了代码,可是它没有实现这种模式.在 SessionBean 中我们 封装了 JDBC 的数据读取,有点向这种模式。 2.1.3. 结构 它使用了 GOF 中的 Bridge 和 Adapter 模式。整个结构图如下: 大家可能看明白,其实就是在 BusinessObject 中提供了业务逻辑方法,假如它必须要读取 数据,但是这个数据可能存在 Oracle 数据库中,又有可能存在 excel 文件中,也许存在 XML 文件中,如果我们在这个方法中直接读取数据,那就要写这样的语句 switch (DATA_TYPE){ case ORACLE: 通过 JDBC 读取 Oracle 数据 break; case Excel: 通过专门的 API 进行读取,Excel 文件