ooAD大报告之 Petstore中EB的设计模式分析 PetStore中EJB的设计模式 作者:张玉平0461001吴小静0461007 范赟梦0461012 张麒0461024 罗晟0461032 张翅0461033 目录 1.概述 3 2.模式分类 21. Data Access object模式 2.1.1.意图 2.1.2.动机 2.1.3.结构 2.1.4. PetStore的例子 6 2.2. Value object模式…… 2.2.1.意图 2.2.2.动机 12 2.2.3.结构 2.2.4. PerStore的例子… 23. Session Facade模式 16 2.3.1.意图 2.3.2.动机 2.3.3.结构. 2.3.4. PetStore中的例子 17 2.3.5.GOF中 Fade模式… 2.4.Fast- Lane reader模式….. 2.4.1.意图 2.4.2.动机 24.4. PetStore中的例子 25.Page-by- Page Iterator模式 2.5.1.意图 2.5.2.动机 2.5.3.结构 2.5.4. PetStore的例子 2.5.5.GOF的 Iterator模式 2.6. Servicelocator模式 2.6.1.意图 2.6.2.动机 2.6.3.结构. 28 2.6.4. PetStore中的例子 2.6.5.GOF模式 2.6. Local模式 2.7.1.意图 2.7.2.动机 2.7.3.结构 2.7.4. PetStore例子 35 作者:张玉平,吴小静,范赟梦,张麒,罗晟,张翅
OOAD 大报告之 PetStore 中 EJB 的设计模式分析 作者: 张 玉 平 , 吴 小 静 , 范 赟 梦 , 张麒,罗晟, 张 翅 1 PetStore 中EJB 的讴计模式 作者:张玉平 0461001 吴小静 0461007 范赟梦 0461012 张 麒 0461024 罗 晟 0461032 张 翅 0461033 目录 1. 概述 ........................................................................................................................................................... 3 2. 模式分类 ................................................................................................................................................... 3 2.1. Data Access Object 模式.......................................................................................................... 3 2.1.1. 意图.................................................................................................................................... 4 2.1.2. 动机.................................................................................................................................... 4 2.1.3. 结构.................................................................................................................................... 5 2.1.4. PetStore 的例子.............................................................................................................. 6 2.2. Value Object 模式.................................................................................................................... 10 2.2.1. 意图.................................................................................................................................. 10 2.2.2. 动机.................................................................................................................................. 12 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 中的例子........................................................................................................ 17 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 2.5. Page-by-Page Iterator 模式................................................................................................. 23 2.5.1. 意图.................................................................................................................................. 24 2.5.2. 动机.................................................................................................................................. 24 2.5.3. 结构.................................................................................................................................. 25 2.5.4. PetStore 的例子............................................................................................................ 25 2.5.5. GOF 的 Iterator 模式................................................................................................... 27 2.6. ServiceLocator 模式................................................................................................................ 27 2.6.1. 意图.................................................................................................................................. 27 2.6.2. 动机.................................................................................................................................. 28 2.6.3. 结构.................................................................................................................................. 28 2.6.4. PetStore 中的例子........................................................................................................ 28 2.6.5. GOF 模式.......................................................................................................................... 32 2.6. Local 模式................................................................................................................................... 33 2.7.1. 意图.................................................................................................................................. 33 2.7.2. 动机.................................................................................................................................. 33 2.7.3. 结构.................................................................................................................................. 33 2.7.4. PetStore 例子................................................................................................................ 35
ooAD大报告之 Petstore中EB的设计模式分析 2.8.cMR模式 2.8.1.意图 2.8.2.动机 2.8.3.结构 2.8.4. PetStore中的例子 36 2.9.业务代表模式 2.9.1.意图 2.9.2.动机 2.9.3.结构 2.9.4. PetStore中的例子 2.9.5.GOF模式 3.EB模式关系 相关的资料 作者:张玉平,吴小静,范赟梦,张麒,罗晟,张翅 2
OOAD 大报告之 PetStore 中 EJB 的设计模式分析 作者: 张 玉 平 , 吴 小 静 , 范 赟 梦 , 张麒,罗晟, 张 翅 2 2.8. CMR 模式 ........................................................................................................................................... 35 2.8.1. 意图.................................................................................................................................. 35 2.8.2. 动机.................................................................................................................................. 35 2.8.3. 结构.................................................................................................................................. 35 2.8.4. PetStore 中的例子........................................................................................................ 36 2.9. 业务代表模式 .............................................................................................................................. 38 2.9.1. 意图.................................................................................................................................. 38 2.9.2. 动机.................................................................................................................................. 38 2.9.3. 结构.................................................................................................................................. 39 2.9.4. PetStore 中的例子........................................................................................................ 39 2.9.5. GOF 模式.......................................................................................................................... 41 3. EJB 模式关系 ......................................................................................................................................... 42 4. 相关的资料 ............................................................................................................................................. 43
ooAD大报告之 Petstore中EB的设计模式分析 1.概述 我们的开源项目是 SUM JAVA上面的 Pet store,通过分析数据模型,和实际操作 PetStore我们 大概对它的数据模型有所了解。现在开始分析系统EB层的设计模式。首先什么是模式?看Gof的 《设计模式》的定义,就是对被用来在特定场景下解决一般设计问题的类和相互通信的对象描述。 其实,就是一些经验,并且经过科学整理,可以在某种场合下反复使用,解决一些问题。当然在《设 计模式》一书罗列出23中模式,并且进行了详细描述。今天我们所说的模式,是 Petstore中的EJB 设计经验 关于 PetStore的模式,根据SUN的站点和我们的分析列出了以下几种模式,当然其中的 Front Controller属于Web层的 Pattern Intent Data Access Decouple business logic from data access logic and adapt the resource being accessed, so that the type of resource can change ily and independently. Fast-Lane Accelerate read-only data access by not using enterprise beans. eager Front Centralize view management (navigation, templating, security Controller etc.) for a Web application in a single object that handles coming client requests. Page-by-Page Efficiently access a large, remote list by retrieving its elements Iterate one sublist of value objects at a time Provide a unified. workflow-oriented interface to a set of Session facade enterprise beans. Value Objec Efficiently transfer remote, fine-grained data by sending a coarse-grained view of the data. 我们首先知道EJB层有这些模式,我们把这些模式简单介绍一下,包括它的意图, 动机,结构,然后看在 PetStore中使用了那些,又是怎样使用的。我们想把我所理解 的写出。 2.模式分类 21. Data Access object模式 这个模式SUN上有详细的描述。这模式使用了GΦF中 Bridge和 Adapter模式。如果大家不熟悉 最好看下Gof的介绍。 作者:张玉平,吴小静,范赟梦,张麒,罗晟,张翅
OOAD 大报告之 PetStore 中 EJB 的设计模式分析 作者: 张 玉 平 , 吴 小 静 , 范 赟 梦 , 张麒,罗晟, 张 翅 3 1. 概述 我们的开源项目是SUM JAVA上面的Pet Store,通过分析数据模型,和实际操作PetStore 我们 大概对它的数据模型有所了解。现在开始分析系统EJB 层的讴计模式。首先什么是模式?看Gof 的 《讴计模式》的定义,就是对被用来在特定场景下解决一般讴计问题的类和相互通信的对象描述。 其实,就是一些经验,并丏经过科学整理,可以在某种场合下反复使用,解决一些问题。当然在《讴 计模式》一书罗列出23 中模式,并丏迚行了详细描述。今天我们所说的模式,是PetStore 中的EJB 讴计经验。 关于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. 我们首先知道EJB 层有这些模式,我们把这些模式简单介绍一下,包括它的意图, 劢机,结构,然后看在PetStore 中使用了那些,又是怎样使用的。我们想把我所理解 的写出。 2. 模式分类 2.1. Data Access Object模式 这个模式SUN 上有详细的描述。这模式使用了GoF 中Bridge 和Adapter 模式。如果大家丌熟悉, 最好看下Gof 的介绍
ooAD大报告之 Petstore中EB的设计模式分析 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. 减弱业务层的数据访问逻辑,使其与数据层的耦合度降低,从而可以方便灵活的改变 数据资源的形式 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 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的向导 它自动生成了代码,可是它没有实现这种模式在 Session bean中我们封装了JDBC 的数据读取,有点向这种模式 作者:张玉平,吴小静,范赟梦,张麒,罗晟,张翅
OOAD 大报告之 PetStore 中 EJB 的设计模式分析 作者: 张 玉 平 , 吴 小 静 , 范 赟 梦 , 张麒,罗晟, 张 翅 4 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. 减弱业务层的数据讵问逻辑,使其不数据层的耦合度降低,仍而可以方便灵活的改变 数据资源的形式。 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 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 的数据读取,有点向这种模式
ooAD大报告之 Petstore中EB的设计模式分析 2.1.3.结构 它使用了GOF中的 Bridge和 Adapter模式。整个结构图如下 Client Obed《 <uses accesses emerton Bridge: Abstraction DAOImplement orB oid data AccessMethod 10 roid data AccessMathod10 Moid dataAccessMethod20 oid data AccessMethod 20 ResourceA oid dataAccessMMethod A1o oid data AccessMethod 81o 大家可能看明白,其实就是在 Business0 bject中提供了业务逻辑方法,假如它必须要读取数据,但 是这个数据可能存在 Oracle数据库中,又有可能存在 excel文件中,也许存在ⅫL文件中,如果我 们在这个方法中直接读取数据,那就要写这样的语句 switch (DATA TYPE)( case ORACLe 通过JDBC读取 Oracle数据 case excel 通过专门的API进行读取, Excel文件 case 通过JDOM进行,分析MM文件; 这样,业务逻辑中到处是数据的读取,并且修改程序,很麻烦。于是这些牛人,想出一个使用 接口DA0(就是 DataAccessObject),它什么具体的事都不做,就申明几个方法,在业务逻辑类中 只管调用它就行了,好像在说我能做什么,不要管我怎样做,就想我们的销售接项目一样,说我们 公司什么事都可以做。但是项目接下以后,就由我们这些命苦的开发人员来实现。最后也就是 DA0 Implementor根据数据的不同有不同的具体类来读取(它有可能是 DAOImplementor ORACLE类实 现读取0 racle数据库, DAOImplementor EXCel,实现通过API读取Exce文件, DAOImplementor xh 实现读取ⅫML文件)。这样你可以在业务逻辑类中,不负责数据的读取,只关注业务逻辑的实现, 它只需调用DA0获得数据,真正实现数据的读取是DA0 Implementor_ ORACLE这样的类。 作者:张玉平,吴小静,范赟梦,张麒,罗晟,张翅 5
OOAD 大报告之 PetStore 中 EJB 的设计模式分析 作者: 张 玉 平 , 吴 小 静 , 范 赟 梦 , 张麒,罗晟, 张 翅 5 2.1.3. 结构 它使用了GOF 中的Bridge 和Adapter 模式。整个结构图如下: 大家可能看明白,其实就是在BusinessObject 中提供了业务逻辑方法,假如它必须要读取数据,但 是这个数据可能存在Oracle 数据库中,又有可能存在excel 文件中,也讲存在XML 文件中,如果我 们在这个方法中直接读取数据,那就要写这样的诧句 switch (DATA_TYPE){ case ORACLE: 通过JDBC 读取Oracle 数据 break; case Excel: 通过与门的API 迚行读取,Excel 文件 break; case XML 通过JDOM 迚行,分析XML 文件; break; } 这样,业务逻辑中到处是数据的读取,并丏修改程序,很麻烦。于是这些牛人,想出一个使用 接口DAO(就是DataAccessObject),它什么具体的事都丌做,就申明几个方法,在业务逻辑类中, 叧管调用它就行了,好像在说我能做什么,丌要管我怎样做,就想我们的销售接项目一样,说我们 公司什么事都可以做。但是项目接下以后,就由我们这些命苦的开发人员来实现。最后也就是 DAOImplementor 根据数据的丌同有丌同的具体类来读取(它有可能是DAOImplementor_ORACLE 类实 现读取Oracle 数据库,DAOImplementor_EXCEL, 实现通过API 读取Excel 文件,DAOImplementor_XML 实现读取XML 文件)。这样你可以在业务逻辑类中,丌负责数据的读取,叧关注业务逻辑的实现, 它叧需调用DAO 获得数据,真正实现数据的读取是DAOImplementor_ORACLE 这样的类