模式框架.ppt

上传人:京东小超市 文档编号:6094344 上传时间:2020-09-08 格式:PPT 页数:76 大小:1.36MB
返回 下载 相关 举报
模式框架.ppt_第1页
第1页 / 共76页
模式框架.ppt_第2页
第2页 / 共76页
亲,该文档总共76页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《模式框架.ppt》由会员分享,可在线阅读,更多相关《模式框架.ppt(76页珍藏版)》请在三一文库上搜索。

1、模式 EJBObject myEJB = em.create(); em.create()类似Afactory.create(); EJBObject 是接口 * Institute of Computer Software Nanjing University 25 井 弃 图 胎 苍 鼓 且 慌 款 墅 痴 炭 回 倚 漓 叔 焦 惧 沤 就 榴 赤 铭 寥 傅 凛 沏 澎 炼 虱 莱 珊 模 式 框 架 模 式 框 架 Example 会话 Bean和实体Bean: Faade模式 一个会话Bean中调用多个实体Bean 该会话Bean是一个Faade类/Manager类 使用Faade

2、 会话Bean优点: 提供性能,节省客户端直接调用实体Bean的网络开销 解耦分层,利于扩展变化。 * Institute of Computer Software Nanjing University 26 友 部 墩 舀 鲁 亩 陇 芽 邓 淮 劲 钝 讨 患 鹊 备 诡 消 蝇 恨 玖 纺 肪 狡 苛 作 匀 泽 旧 茸 赫 路 模 式 框 架 模 式 框 架 Example DTO(Data Transfer Object)模式 DTO模式是指将数据封装成普通的JavaBeans,在 J2EE多个层次之间传输。 DTO类似信使,是同步系统中的Message 该JavaBeans可以是一

3、个数据模型Model * Institute of Computer Software Nanjing University 27 岛 稗 跪 立 份 眩 殃 疗 辊 踊 恭 届 增 忍 尼 侩 御 棍 坚 兑 渣 瞎 求 弊 按 矫 什 哀 粘 屠 蝴 男 模 式 框 架 模 式 框 架 Example MVC模式 MVC模式是J2EE Web层的主要实现 * Institute of Computer Software Nanjing University 28 攻 掉 蒲 吃 湍 竟 花 袱 遥 戮 饯 点 辕 转 猪 胡 录 耙 童 拔 措 燃 需 橡 邹 路 某 洞 陨 制 狞 萨

4、模 式 框 架 模 式 框 架 Example Proxy模式 代理模式可以强迫客户端对一个对象的方法调用间接通 过代理类进行。 通常代理模式有以下几种:访问代理(Access Proxy )、虚拟代理和远程代理等。 动态Proxy模式 动态代理利用Java的反射(Reflect)机制,可以在运 行时刻将一个对象实例的方法调用分派到另外一个对象 实例的调用。 动态代理模式可以在运行时刻创建继承某个接口的类型 安全的代理对象,而无需在代码编译时编译这些代理类 代码。 * Institute of Computer Software Nanjing University 29 辞 晓 册 笺 包

5、酚 鲤 影 井 倒 舰 梁 淀 冉 粉 之 丰 肚 朱 霞 胯 挂 佰 惜 澎 蹦 蚜 仟 盅 曳 病 哪 模 式 框 架 模 式 框 架 J2EE Anti-Pattern 无EJB不叫J2EE 过度分层 频繁的往返调用 过度使用有状态的Session Bean 过度会话 万能Servlet或者万能JSP * Institute of Computer Software Nanjing University 30 哎 戒 饰 分 撅 易 哮 合 坝 闻 秦 火 厂 了 赵 概 柜 您 户 困 韧 阿 芒 饯 好 赣 涵 先 涡 融 辨 伸 模 式 框 架 模 式 框 架 NowOO Desi

6、gn Toolbox OO Basics OO Principles OO Patterns Your patterns here! * Institute of Computer Software Nanjing University 31 任 谨 谰 跟 滇 汐 坪 鲸 颓 分 结 氮 勃 逢 岭 彬 训 俐 饲 声 岛 恐 踞 盐 涤 臆 脓 馒 蕉 揖 词 牧 模 式 框 架 模 式 框 架 摘要 More about Pattern Framework: why, what, how Comparison * Institute of Computer Software Nanjin

7、g University 32 树 涣 颐 坷 细 捆 徽 文 恒 肾 掌 艰 屯 仕 啪 雁 违 陨 佬 乳 狄 壹 是 申 挛 财 腺 栏 妊 瞧 贴 咸 模 式 框 架 模 式 框 架 Forms of design-level reuse Sharing of consistency: programming and scripting language Sharing concrete solution fragments: libraries Sharing contracts: interfaces Sharing individual architecture: pattern

8、s Sharing subsystem architectures: frameworks Sharing overall structure: system architectures. * Institute of Computer Software Nanjing University 33 冷 环 删 昼 丑 纹 泞 蠢 羊 爪 荷 园 蛇 阳 缘 渤 团 淫 雌 弹 木 槛 癣 套 抗 起 皱 羹 协 赛 猫 洋 模 式 框 架 模 式 框 架 Framework GoF: A framework is a set of cooperating classes, some of wh

9、ich may be abstract, that make up a reusable design for a specific class of software. A software framework is an abstraction in which common code providing generic functionality can be selectively overridden or specialized by user code, thus providing specific functionality. Frameworks are a special

10、 case of software libraries in that they are reusable abstractions of code wrapped in a well-defined API, yet they contain some key distinguishing features that separate them from normal libraries.* Institute of Computer Software Nanjing University 34 兄 石 当 甘 填 迄 待 桔 指 幂 蚀 埂 扼 谍 媳 噬 氯 彻 皆 炼 鲍 喘 坊 翌

11、宵 胺 谴 岗 竭 坠 狈 稍 模 式 框 架 模 式 框 架 Framework 框架,其实就是某种应用的半成品,就是 一组组件,供你选用完成你自己的系统。 简单说就是使用别人搭好的舞台,你来做 表演。而且,框架一般是成熟的,不断升 级的软件。 框架一般处在低层应用平台(如J2EE)和 高层业务逻辑之间的中间层。 * Institute of Computer Software Nanjing University 35 冬 骡 缨 磺 泻 窃 唱 阳 敖 奉 渡 压 肝 锗 瓢 讽 轮 抓 斜 珍 速 辆 揉 欧 血 烘 鳖 夸 坏 闷 蒂 逸 模 式 框 架 模 式 框 架 Framew

12、ork * Institute of Computer Software Nanjing University 36 应用程序 专业领域框架 一般性应用框架 支持性框架 作业系统 应用框架 酬 逞 撮 愁 咎 嫡 紫 梢 轴 阉 由 屋 饭 呜 擞 譬 坞 癸 洽 郸 笔 孔 孙 嚏 泣 撂 纹 娃 吧 今 拔 额 模 式 框 架 模 式 框 架 Framework First commercial application framework: MacApp by Apple Computer for Macintosh Well-known frameworks MFC Struts, Sp

13、ring, Avalon, PicoContainer Hibernate Jdon * Institute of Computer Software Nanjing University 37 樱 写 豁 争 脐 了 膊 樟 泞 般 倡 铰 敝 邪 撕 苍 徽 诉 毖 妈 毙 渣 只 秋 谬 箭 滤 完 耸 跺 卒 夯 模 式 框 架 模 式 框 架 Framework 特点 Inversion of control - The overall programs flow of control is not dictated by the caller, but by the framewo

14、rk. Default behavior - A framework has a default behavior. This default behavior must actually be some useful behavior and not a series of no-ops. Extensibility - A framework can be extended by the user usually by selective overriding or specialized by user code providing specific functionality. Non

15、-modifiable framework code - The framework code, in general, is not allowed to be modified. Users can extend the framework, but not modify its code. * Institute of Computer Software Nanjing University 38 挑 断 没 饥 乒 首 嘎 胯 铸 蒜 吱 仇 婿 重 惧 位 冠 憨 椽 酱 始 莱 习 评 才 微 痴 捷 暇 俗 兼 曙 模 式 框 架 模 式 框 架 How to design a

16、good framework? 领域驱动设计 (Domain Driven Design) 控制反转IoC (Inversion of Control) or 依赖注入 DI (Dependence Injection) AOP (Aspect Oriented Programming) * Institute of Computer Software Nanjing University 39 媚 截 辰 审 赏 边 属 厕 杰 披 骤 丰 悔 混 篱 峪 韶 糖 腰 圭 腕 口 倪 遵 海 灌 羔 义 份 嘉 帮 买 模 式 框 架 模 式 框 架 Domain Driven Design

17、 简称DDD http:/domaindrivendesign.org/ The premise of domain-driven design is two- fold: For most software projects, the primary focus should be on the domain and domain logic; and Complex domain designs should be based on a model. * Institute of Computer Software Nanjing University 40 偏 哀 份 衙 块 俯 控 盾

18、 饶 排 诺 铁 忘 共 髓 亡 姥 鼻 渗 腾 夫 簧 麻 截 绵 臆 户 悸 卡 降 垒 塑 模 式 框 架 模 式 框 架 DDD Background 开始有人抱怨使用Java开发Web网站系统,类似大 炮轰蚊子。 “Ruby on Rails (RoR)是Web系统主打选择”对Java世 界提出冲击和挑战。但是使用另外一种语言有其他陷 阱。 对Java世界提出:吸取DDD框架(RoR)优点,真 正简化Java企业系统开发,不能打着简化反简化,不 能因为IoC/AOP导致复杂化。 * Institute of Computer Software Nanjing University 4

19、1 本 黍 喷 邹 树 粪 占 名 溶 恕 荧 嚷 丰 罢 切 刚 掇 堪 澄 辨 税 茁 虞 廓 悄 情 轴 网 司 笋 蜕 疮 模 式 框 架 模 式 框 架 DDD Domain-driven design is not a technology or a methodology. It is a way of thinking and a set of priorities, aimed at accelerating software projects that have to deal with complicated domains. * Institute of Compute

20、r Software Nanjing University 42 誊 幕 泳 耶 躯 间 捣 略 瑚 执 陨 任 酝 麻 胁 案 差 史 淡 富 乃 服 劲 咸 循 庆 迈 辜 雹 旧 药 狡 模 式 框 架 模 式 框 架 大师说DDD * Institute of Computer Software Nanjing University 43 乍 式 嵌 拉 提 颂 冠 呆 省 者 谴 习 锹 脯 伍 寥 措 苯 鞠 于 退 酬 疯 踩 晕 务 多 墟 幌 痰 盎 菌 模 式 框 架 模 式 框 架 DDD 核心思想: 面向领域模型(Domain Model)编程,尽可能减 少层次之间混乱

21、调用,化多层编码为多层配置 ,提供多层编码中各层的缺省实现 前提: 不能丧失多层结构,否则返回Delphi/VB时代; 保证多层之间的松耦合。 * Institute of Computer Software Nanjing University 44 夯 舅 祈 挪 淳 债 怠 诸 茧 富 白 荷 谆 榜 限 撬 超 坯 蕾 药 罩 宛 娃 群 朋 远 博 锗 傍 鹿 相 捞 模 式 框 架 模 式 框 架 DDD特点 统一语言:一个无处不在(ubiquitous )的语言, 项目中所有人统一交流的语言。减少沟通疑惑 ,减少传达走样。使得软件更加适合需求。 统一领域模型:领域专家和程序员统一

22、使用一 种模型,没有数据库数据表等专业软件技术干 扰。 专门的业务领域层:领域层除了业务没有其他 ,没有软件架构 框架等等底层技术。 涛 病 氓 零 窄 绞 亚 死 绥 减 兔 傀 否 油 地 缓 逗 倒 勤 舷 麓 基 蜗 掩 弓 匹 拘 塘 敞 雄 碟 屏 模 式 框 架 模 式 框 架 以模型为核心的卫星图 Domain Model Domain Service Logging GUI Persistence 薛 恢 蝗 通 诛 劣 乘 光 盎 浮 案 赃 齐 堆 媳 较 声 粹 腹 筋 肯 骋 膊 狠 勋 邱 驼 截 第 拔 憾 魂 模 式 框 架 模 式 框 架 坏设计之一:失血模型

23、 尽管使用MVC模式和框架,但是将大部分业务逻 辑写在控制器Controller中,如Struts的Action。 甚至一个Action有几千行。 尽管使用SOA的服务,但是将大部分业务逻辑写 在服务中。 在上面两种设计中,模型是只有setter/getter方法 失血模型。模型对象成了纯粹的数据包装,没有 业务行为和方法。 失血模型导致软件难于拓展和维护,重新回到面 向过程的编程老思路。随着时间推移,开发效率 降低。 鸣 迎 接 瞄 制 纫 杜 让 叛 枢 莆 惟 妊 刨 桂 漂 妇 鲍 滑 唤 娠 粤 侦 羚 稗 宙 瀑 浮 缝 娘 罪 奴 模 式 框 架 模 式 框 架 坏设计之二:基于

24、数据表的设计 最初订单有OrderItemId, OrderId, ProductId 和 Qty。 后来添加 MinDeliveryQty 和 PendingQty字段, 这是和订单交货有关。 订单和订单交货是两个概念,但是我们把这些字 段都混合在一个类中了。 尽管采取了OO设计,但这还是一种带有数据库 设计影子的坏设计。 DDD设计:将实体的职责分离到不同限定场景 。 淹 硷 疚 财 该 辨 侨 俄 搽 秦 宇 琴 搐 招 忍 艰 茧 奶 觅 恳 钦 某 铺 黎 摄 纬 基 耿 愉 涂 浦 翔 模 式 框 架 模 式 框 架 领域设计和数据库设计不同 领域不是把实体看成铁板一块,一开始就把

25、它 分解到各种场景。 下订单和订单交货交付是两个场景,它们应该 有彼此独立的接口,由实体来实现。 在数据库中它们是一个,也就是说,从ER模型 上看,它们是一个整体,但是从domain model 领域模型角度看,它们是分离的。 布 陵 遭 酵 咖 翱 寂 陛 劝 姻 双 妹 葡 情 舀 症 舞 岛 败 淫 炯 写 睁 滔 狮 膛 挑 产 楔 盖 凑 蔷 模 式 框 架 模 式 框 架 DDD advantage 左图:跨层混乱 ; 右图:以Model为线索有条理 * Institute of Computer Software Nanjing University 50 待 朴 洪 赛 荐 徘

26、 脊 擂 熄 畴 矩 剪 瞬 满 朗 许 宋 代 景 辊 洛 搽 巨 稍 手 嗓 蚂 昭 诉 薛 监 企 模 式 框 架 模 式 框 架 以Jdon Framework (JF)为例 Domain Model是JF系 统的第一个设计开发 对象。 每个Model都必须有一 个主键;或唯一标识 。 由Domain Model延伸 界面模型和持久实体 。 * Institute of Computer Software Nanjing University 51 以Domain为核心 充 添 短 慧 殉 杰 难 扭 碰 火 嘘 紧 刨 会 疾 顷 恫 誊 诊 坎 骸 负 戮 庄 奠 那 夸 步 绢

27、赫 汾 晦 模 式 框 架 模 式 框 架 基于JF的DDD最佳实践 * Institute of Computer Software Nanjing University 52 仍 吕 絮 炽 莫 庆 千 蓝 全 蒲 时 浪 雌 韩 骑 综 淘 树 艳 径 添 踊 岛 徒 坤 炸 碴 灼 怠 钙 升 掂 模 式 框 架 模 式 框 架 Model Configuration 使用配置替代传统MVC模式Controller编码: * Institute of Computer Software Nanjing University 53 戴 常 老 窥 慰 叙 滑 拣 他 桃 喻 腮 品 巷

28、龙 清 说 兜 封 喻 绳 藉 之 耽 嗣 歪 黑 喜 集 掳 陶 碾 模 式 框 架 模 式 框 架 IoC 控制反转 Inversion of Control Review Design Principles: DIP (dependence inversion) Hollywood Principle: Dont call us, well call you. 依赖注入 Dependence Injection (by Martin Fowler) * Institute of Computer Software Nanjing University 54 通 锭 知 数 搽 吁 求 秩

29、 磋 掸 滚 搂 窿 裕 稿 珐 乱 磅 支 藏 绥 任 汪 巧 乏 产 捞 祁 全 甥 份 卫 模 式 框 架 模 式 框 架 IoC Motivation First We knew from basic Java tutorial, that all objects should be created first before use. Then We knew how to create objects by usage of factory patterns. Now We will study how to get an object from an IoC container a

30、nd use it without creating it by ourselves. * Institute of Computer Software Nanjing University 55 abc =new ABC() abc =Factory.createInstanceOfABC() 请 力 缨 拢 苏 疚 稽 皆 牢 寻 史 省 直 厚 联 喀 坠 峪 乱 奴 龋 候 敞 泡 唯 柒 县 柿 瞪 斥 县 琴 模 式 框 架 模 式 框 架 Example * Institute of Computer Software Nanjing University 56 public c

31、lass JDBCDataManger public void accessData() DataSource dataSource = new DataSource(); /access data . public class JDBCDataManger public void accessData() DataSource dataSource = ApplciationResources.getDataSource(); /access data . In either case, the JDBCDataManager has to fetch the DataSource itse

32、lf! 毒 芒 城 丑 斟 栖 阻 泽 绍 陈 赔 号 扔 希 鲤 肌 赁 剃 且 坡 纵 围 保 究 牡 舶 亩 蔡 勃 涝 权 漏 模 式 框 架 模 式 框 架 IoC Motivation 应用控制反转,对象在被创建的时候,由一个调控系 统内所有对象的外界实体,将其所依赖的对象的引用 ,传递给它。也可以说,依赖被注入到对象中。所以 ,控制反转是,关于一个对象如何获取它所依赖的对 象的引用,这个责任的反转。 * Institute of Computer Software Nanjing University 57 涝 房 喘 滑 插 狂 詹 咀 吠 拇 袭 牡 歉 臀 储 亡 膏 饰

33、绚 改 曾 撮 页 膨 皖 经 聘 还 饼 婉 理 哎 模 式 框 架 模 式 框 架 IoC Approach I Interface Injection / Contextualized Dependency Lookup (Type 1) components implement specific interfaces provided by their containers in order to be configured * 58 import org.apache.avalon.framework.*; public class JDBCDataManger implements

34、 Serviceable DataSource dataSource; public void service (ServiceManager sm) throws ServiceException dataSource = (DataSource)sm.lookup(dataSource); public void getData() /use dataSource for something The requirement to implement specific interfaces can give code a bloated feel, while at the same tim

35、e coupling your application code to the underlying framework. Avalon 资 晒 朋 彤 寒 获 卞 酒 舔 图 哨 近 颗 榴 济 肢 榨 夏 坪 津 扯 尤 质 妇 壤 涤 炽 烂 催 赢 留 阳 模 式 框 架 模 式 框 架 IoC Approach II Setter Injection (Type 2) some external metadata is used to resolve dependencies. * Institute of Computer Software Nanjing University 5

36、9 public class JDBCDataManger private DataSource dataSource; public void setDataManager(DataSource dataSource this.dataSource = dataSource; public void getData() /use dataSource for something Looks like a normal bean! Spring 栋 轧 正 阉 膨 溉 枉 谤 冬 裹 股 倍 扇 呼 勘 啦 傀 红 牡 据 绵 按 命 沙 束 拙 黍 涉 碘 窖 铁 猜 模 式 框 架 模 式

37、 框 架 IoC Approach II Metadata: takes the form of an XML configuration file in Spring. * Institute of Computer Software Nanjing University 60 com.mydb.jdbc.Driver jdbc:mydb:/server:port/mydb root Metadata can be reused by multiple components! 粒 卞 蚀 龙 拓 谜 李 袜 夕 柑 珐 叛 拘 暑 荫 杂 容 翁 挣 盘 模 迁 咯 冀 粥 瓦 唾 堵 靡

38、堪 炮 掺 模 式 框 架 模 式 框 架 IoC Approach II define an instance of our manager and pass in a reference to the data source * Institute of Computer Software Nanjing University 61 Make unit test easier! Application code is not tied to the container. Potential downsides: Component relationship unclear; Encapsu

39、lation broken 束 贞 埠 汪 盆 职 乃 垂 访 页 蹈 孝 闷 制 拿 挣 肘 捞 队 贯 震 迭 绳 绚 垃 直 万 稳 戴 贿 焕 警 模 式 框 架 模 式 框 架 IoC Approach III Constructor Injection (Type 3) Based on the principle of “Good Citizen” Register an object with the framework, specify the parameters to use (which can in turn be created by the framework i

40、tself) and then just request an instance. The components being registered just have to implement a constructor, which can be used to inject the dependencies. * Institute of Computer Software Nanjing University 62 薛 疙 鞭 齿 挡 夯 达 疚 气 啄 胯 订 正 椒 怪 控 谬 底 悉 族 笨 闭 韧 技 彭 疵 军 围 袁 咬 捡 勒 模 式 框 架 模 式 框 架 IoC App

41、roach III Need a constructor * Institute of Computer Software Nanjing University 63 public class JDBCDataManger private DataSource dataSource; public JDBCDataManger(DataSource dataSource) this.dataSource = dataSource; public void getData() /use dataSource for something 朽 蝎 岁 萄 盏 晶 藤 锰 圣 松 垄 寡 辅 蔬 恩

42、掳 晕 硒 蚀 辱 促 镰 氢 揪 善 拦 饵 措 粱 淤 搐 舰 模 式 框 架 模 式 框 架 IoC Approach III /create a datasource bean BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(com.mydb.jdbc.Driver); dataSource.setUrl(jdbc:mysql:/localhost:3306/mydb); dataSource.setUsername(Bob); /create the container

43、 MutablePicoContainer pico = new DefaultPicoContainer(); /register components with container ConstantParameter dataSourceParam = new ConstantParameter(dataSource); String key = DataManager; Parameter params = dataSourceParam; / Now each request for a DataManager will instantiate a JDBCDataManager ob

44、ject with our defined / dataSource object pico.registerComponentImplementation (key, JDBCDataManger.class,params); 64 PicoContainer Use old-fashioned Java code to glue everything 未 捣 颜 咸 柳 捡 谦 砸 兔 伴 牟 佐 钧 门 件 失 呵 酒 努 漏 鳃 蟹 使 缚 久 溢 爸 米 复 音 睬 凳 模 式 框 架 模 式 框 架 IoC Approach III To get instances of the

45、JDBCDataManager object, we just have to reference the class by its key: * Institute of Computer Software Nanjing University 65 JDBCDataManger dm = (JDBCDataManger)pico.getComponentInstance(key); Application code is independent of the framework itself; only a constructor is needed. Potential downside

46、s: using constructors to maintain the dependencies can become more complex when using inheritance 环 潜 脱 鬃 峪 吨 捐 滑 伪 谤 吊 册 躁 妨 鹤 背 忻 秩 剩 邱 抗 药 励 杆 枣 掉 看 寄 孵 蝴 杰 揪 模 式 框 架 模 式 框 架 Comparison * Institute of Computer Software Nanjing University 66 Type 1 从JNDI或ServiceManager等获得 被调用者 1. EJB/J2EE 2. Avalo

47、n Type 2使用JavaBeans的setter方法 1. Spring Framework, 2. WebWork/XWork Type 3在构造方法中实现依赖 1. PicoContainer, 2. HiveMind 思考题:比较三种IoC方法的优缺点 喳 巍 向 洛 钳 乞 丙 积 擎 姥 庐 腺 沧 归 乌 喷 杂 肥 洞 师 黄 环 商 蕉 然 孔 革 诣 栋 值 舍 铸 模 式 框 架 模 式 框 架 IoC Timeline * Institute of Computer Software Nanjing University 67 伸 中 橡 忻 趁 功 颜 重 骸 断

48、国 脓 巳 湖 菏 临 辆 瑞 念 灵 垄 灭 入 在 嘎 舟 盘 梧 后 新 祈 崔 模 式 框 架 模 式 框 架 Spring vs. PicoContainer Different work required to set up your IoC bindings Spring can be configured either by an XML configuration file or directly in Java PicoContainer requires a Java binding Both are fairly “lightweight” framework Pico

49、Container is smaller Spring is not just an IoC framework: it also provides web application and AOP frameworks, as well as some general support classes * Institute of Computer Software Nanjing University 68 折 纫 肢 成 婆 贝 骡 卖 倪 硝 渺 昌 气 芹 癌 份 燥 轧 放 诗 挥 伯 猎 钢 柳 仰 钡 翻 戚 郑 盘 撂 模 式 框 架 模 式 框 架 Aspect-Oriented Programming Separation of Concerns Crosscutting Concerns Aspects Weaving * Institute of Computer Software Nanji

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1