资源预览内容
第1页 / 共38页
第2页 / 共38页
第3页 / 共38页
第4页 / 共38页
第5页 / 共38页
第6页 / 共38页
第7页 / 共38页
第8页 / 共38页
第9页 / 共38页
第10页 / 共38页
亲,该文档总共38页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
第十一章 面向对象设计 (Object-Oriented Design)1. OOD准则:优秀软件设计的 一个重要特点是容易维护回顾:SD准则包括 Modularization Abstraction Information hiding Module independence对于 OOD有类似的准则:1、Module = Object1. OOD准则 Procedure abstraction: 在SD中已讨论 Data abstraction: Class即是一种抽象数据类型。外界无须 知道实现方法就可按照类协议(class description protocol)* 使用class中定义的数据。 Parameter abstraction: 将数据类型作为参数处理。* Class description protocol : The complete definition of all properties, features, and methods that are descriptive of any object that is an instance of a class. Timothy Budd An Introduction to Object-Oriented Programming Addison-Wesley Publishing Company, Inc. 19912、Abstraction:抽出事物的本质特性, 暂不考虑 其细节,使设计从具体实现方法中超脱。1. OOD准则例:C+ 中的“模板”(template)template class array_n private :T items n ; / 定义了T类型的向量元素共n个 ;main ( ) array_n w ; /w是有1000个元素的复向量1. OOD准则3、Information hiding = Encapsulation of object4、Coupling: 交互耦合(interactive coupling):通过传递message发生 要求 降低参数个数和参数复杂性减少objects发送 接收message的个数 as loose as possible 继承耦合(inheritance coupling):要求 Parent class IS_A child class as high as possible1. OOD准则 一般-特殊内聚(general-particular cohesion):High g-p cohesion High inheritance coupling5、Cohesion: 服务内聚(service cohesion):一个服务只完成一个功能 。 类内聚(class cohesion):一个类只有一个用途,否则分 解之。6、Reusability(详见3)2. 启发式规则1、设计结果清晰易懂,应做到: 用词一致 按习惯用法 命名。不同classes中相似的 methods最好取同一名字。 使用已有的protocol。 尽量减少message模式的数 目。 避免模糊定义。2、一般-特殊结构的深度应适当(约100个classes,则 设计72层)2. 启发式规则 3、设计简单的class(定义不超过一页纸或两屏)。 应注意: 避免过多attributes; 能用简单的语句描述一个class的任务; objects之间合作关系要简单;避免过多methods( 7个)。问题:设计出大量的classes,使结构复杂度增加。 解决:划分主题,提高可理解性。 4、使用简单的protocol,减少message 中传递的 parameters5、使用简单的method(CASE 可考虑用 inheritance替代)。6、把设计变动减至最小。1、概念: 知识重用(例如软件工程知识的重用) 方法和标准重用(例如OO方法和国家规定 的软件开发规范的重用) 软件成分的重用3. 软件重用 (Software Reuse)知识 工程 源码剪贴 无法溯源,无配置管理 Include 修改后所有包含了此段代码的程 序都须重新编译。 Inheritance 无须改动原有代码 想象一下,stdio.h 被改动之后 重用软件成分有三个级别: 代码重用:3. 软件重用 设计重用 当移植系统时 分析重用 当需求未变,而系统结构改变 时(例如将HDIS改为OO实现)2、重用效果的衡量: 额外代价: 创建可重用成分的专门投资 多花2 4倍时间测试以保证质量 构件库的建立与维护需要投资以上投资将分摊到重用这些构件的新系统成 本中。重用次数越多,分摊成本越少。3. 软件重用记:Lt = Total length of code ( # of lines)Ln = Length of new codeLr = Length of reused codeEt 、En、 Er are the corresponding efforts ( # of m-d) 重用率(Reusability)与生产率(Productivity)ProductivityReusability =开发代码的生产率重用新代码的生产率3. 软件重用重用技术:指利用可重用的构件开发软件的技 术,及开发可重用软件的技术。 软件组合技术: 底层部件库法(Bottom-up compositional reuse) : 从可重用的代码部件库(reuse repository)中选用 部件,组合成软件。A:是,前提条件为CnCr ,即重用比新开发效率高。Q:是否R 越高P就越高?Lucent Technologies initiated a companywide program to reuse software components (McClure 1997). As a consequence, the Workstation Software Development Department formed a Reuse Council to devise a strategy for selecting candidate components for its reuse repository. The Council was comprised of seven people, representing all groups in the department. The Council created an inventory of components and formed a matrix with the features of all past and planned projects. Then, each feature was rated in terms of whether it had been implemented and was still needed, had been implemented but was no longer needed, or had not been implemented but was still needed. Those features that were needed and were common to more than one project were targeted for reuse. In fact, some were redesigned to make them more reusable.The Council met every week for 2 hours to make component selections, inspect design documentation for those components already in the repository, and to monitor the levels of reuse in the departments projects.3. 软件重用例: 上层组合法:完整程序的组合3. 软件重用 软件生成技术:按照形式化的软件功能描述和一定的生成机 理,由生成器系统(generator system)自动生成目标程序 。重用的是generator 的代码规则 OO重用技术:Class component的重用(详见下文) 类构件(Class component):可重用的软构件应具备的特点:独立、可塑、接口清晰(文档详尽)3. 软件重用重用方式: 实例重用(instance reuse black-box reuse):创建class的不同instances,通过messages完成不同的任务。是最基本的重用方式。用几个简单的objects创建出更复杂的class,是实例重用的另一种形式 继承重用(inheritance reuse): 是一种安全地裁剪已有的class component的方式 。 多态重用(polymorphism reuse):Parent class与child class有相同的对外接口,使消息连接的复杂度降低。3. 软件重用注意:有些操作可能会妨碍class component的重用,如 与表示方法有关的操作 与数据结构、大小有关的操作 与外部设备有关的操作 实现算法在将来可能会改进改变的核心操作解决方法:将这些操作分离出来,作为适配接口 (adaptive interface),使class中其它操 作通过调用AI而实现。在不同应用环 境下,用户只须重新定义AI操作就可 以重用class。3. 软件重用Adaptive Interface还可进一步细分为 转换接口(transition interface):重用时必须重定义与表示方法、数据结构、硬件等有关的操作(例如C+中class 里的 pure virtual function) 扩充接口(expansion interface):一个操作可由多种算法实现,若无新算法则继承老算法。IPO问题域 Application Domain人机交互 Human- Computer Interface (HCI)任务管理 Task Management数据管理 Data ManagementMethodAttributeStructureClass- 接受message 的object 必有对应的 method; Method 改变 status(即 attributes),并完成 action。5. 设计类中的服务Event Status 1 do: Action 1 Status 2 do: Action 2 则算法应有DO_CASE型控制 从function model出发:DFD的一般结构是 IPO注意:Action (即算法) 与status 有关。例 如:不同status接 受同一个 event 时 ,其action不同 Input Flow Class ProcessI O ClassProcess5. 设计类中的服务 若 Process=从In
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号