OOA & OOD: An introduction 面向对象的分析与设计简介 Institute of Computer Software Nanjing University 2019/1/17
摘要 引言 如何发现“类” 如何设计“类” 小结 Institute of Computer Software 2019/1/17 Nanjing University 2019/1/17
摘要 引言 如何发现“类” 如何设计“类” 小结 Institute of Computer Software 2019/1/17 Nanjing University 2019/1/17
面向对象软件工程 问题域 需求分析 问题域 OOA 总体设计 OOD 详细设计 OOP OOT 编程 计算机 测试 计算机 自然语言 分析与设计的鸿沟 自然语言 OOA 总体设计 OOD 详细设计 面向对象的编程语言 OOP OOT 编程语言 编程 计算机 测试 计算机 面向对象的软件工程方法 传统的软件工程方法 Institute of Computer Software Nanjing University 2019/1/17
面向对象软件工程 面向对象方法真正意义深远的目标是它适合于 解决分析与设计期间的复杂性并实现分析与设 计的复用。 面向对象的开发不仅仅是编程,必须在整个软 件生命周期采用一种全新的方法:在软件开发 过程所有阶段都运用面向对象的方法,将 OOA,OOD,OOP,OOT有机地集成在一起,这样有 利于系统的稳定性。 Institute of Computer Software Nanjing University 2019/1/17
面向对象软件工程 喷泉模型 以用户需求为动力,以对象为驱动 各阶段是相互迭代和无间隙的 使用相同的描述方法和模型,使得 软件生存期各阶段所使用的方法、 技术具有高度的连续性。 Institute of Computer Software Nanjing University 2019/1/17
面向对象方法 三种基本活动 Booch Coad/Yourdon OMT:对象模型, 功能模型,动态 模型 Jacobson UML 识别类和对象 描述对象和类之间的关系 通过描述每个类的功能定义对象的行为 Institute of Computer Software Nanjing University 2019/1/17
面向对象方法 Rational Unified Process (RUP)– Rational统一开发 过程 迭代式的增量开发 用例驱动 以软件体系结构为核心 Institute of Computer Software Nanjing University 2019/1/17
面向对象的分析与设计 OO方法强调开发过程的连续性 构造一系列不断精化的面向对象的模型 实际上目前大多倾向于采用迭代式开发 类成为分析、设计和实现的基本单元 核心问题: How to find the classes (不同层面的类)? How to design the classes? Institute of Computer Software Nanjing University 2019/1/17
推荐 Craig Larman: Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development Institute of Computer Software Nanjing University 2019/1/17
面向对象分析 OOA是软件开发过程中的问题定义阶段 领域分析(Domain Analysis):抽取和整理用户需 求并建立问题域精确模型的过程。以公共对象、类 和框架等形式在特定应用领域中标识、分析和规约 公共的可复用的软件成分的能力。抽象出目标系统 的本质属性,建立问题领域模型。 应用分析(Application Analysis):将领域分析建立 起来的问题领域模型,用某种基于计算机系统的语 言来表示。响应时间需求、用户界面需求和数据安 全等特殊的需求也都在这一层分解抽出。 Institute of Computer Software Nanjing University 2019/1/17
面向对象分析 领域分析 领域知识源 领域分析模型 领域分析 Institute of Computer Software 2019/1/17 技术文件 类的分类 已有应用 领域分析 复用标准 客户考察 功能模型 专家建议 领域语言 目前/未来的需求 Institute of Computer Software Nanjing University 2019/1/17
Institute of Computer Software Nanjing University 2019/1/17 ask the relevant questions (A2): what is the maximum acceptable temperature? What are the recognized categories of employees? How are bonds handled differently from stocks? By providing you with a framework, which you will have to fill using input from people competent in the application domain, an analysis method will help spot and remove obscurities and ambiguities which can be fatal to a development. Nothing is worse than discovering, at the last stage of implementation, that the marketing and engineering departments of the client company have irreconcilable views of what equipment maintenance means, that one of these views was taken by default, and that no one cared to check what the actual order giver had in mind. As to A3, a good analysis document will be the place to which everyone constantly goes back if delicate questions or conflicting interpretations arise during the development process. Institute of Computer Software Nanjing University 2019/1/17
面向对象分析 具体步骤 获取用户基本需求 标识类和对象 定义类的结构和层次 表示类(对象)间的关系 为对象行为建模 常用用例来收集和描述用户的需求 获取用户基本需求 标识类及类的属性和服务 标识类和对象 定义类的结构和层次 描述系统的静态结构 表示类(对象)间的关系 为对象行为建模 描述系统的动态行为 Institute of Computer Software Nanjing University 2019/1/17
需求分析 Waterfall式开发其需求分析一开始(其实是过早地)确定的features 在最终产品中真正采用情况的比例 Institute of Computer Software Nanjing University 2019/1/17
Requirement checklist FURPS+ Functional features, capabilities, security Usability human factors, help, documentation Reliability frequency of failure, recoverability, predictability Performance response times, throughput, accuracy, availability, resource usage. Supportability adaptability, maintainability, internationalization, configurability. “+” Implementation Interface Operations Packaging Legal Institute of Computer Software Nanjing University 2019/1/17
Classification Functional requirements: behavior, features, capabilities,security Usability requirements: human factors, help, documentation “The font on the display should be readable from 5 feet” Institute of Computer Software Nanjing University 2019/1/17
Classification Reliability requirements: frequency of failure, recoverability Performance requirements: response times, throughput, accuracy, availability, resource usage, etc. Institute of Computer Software Nanjing University 2019/1/17
Classification Supportability requirements: adaptability, internationalization, maintainability “The system should allow frequent and easy changes in the network configuration” The + in the FURPS+: Implementation requirements: “Must use Linux and Java” Institute of Computer Software Nanjing University 2019/1/17
面向对象分析 OOA模型 交互图 主题图 详细说明 基本模型(类图) 对象层 特征层 关系层 Institute of Computer Software Nanjing University 2019/1/17
面向对象设计 OOD是面向对象方法在软件设计阶段应用与扩 展的结果,通常分为两个阶段 OOD的准则 高层设计:建立应用的体系结构 低层设计:集中于类的详细设计 OOD的准则 抽象,信息隐藏,模块化,弱耦合,强内聚,可重 用 Institute of Computer Software Nanjing University 2019/1/17
面向对象设计 OOD具体内容 按实现条件对OOA模型进行调整,并补充几个新的 组成部分: 设计问题域组元 设计人机交互组元 设计任务管理组元 设计数据管理组元 Institute of Computer Software Nanjing University 2019/1/17
摘要 引言 如何发现“类” 如何设计“类” 小结 Institute of Computer Software 2019/1/17 Nanjing University 2019/1/17
回顾:三个世界 Three worlds Visual modeling Institute of Computer Software Nanjing University 2019/1/17
Reality 现实世界 抽象 问题世界 软件世界 Institute of Computer Software 2019/1/17 Nanjing University 2019/1/17
问题:How to find the classes? Use case 基于需求文档 Use-case model: writing requirements in context Institute of Computer Software Nanjing University 2019/1/17
用例(Use Cases) 用例是一个叙述性文档,用来描述参与者使用 系统完成某个过程时的事件发生顺序。 用例乃是对过程的描述。 过程描述事件、动作和事务处理自始至终的发 生顺序。 Use cases are text documents, not diagrams, and use-case modeling is primarily an act of writing text, not drawing diagrams. Institute of Computer Software Nanjing University 2019/1/17
Use Case例子: Process Sale: A customer arrives at a checkout with items to purchase. The cashier uses the POS system to record each purchased item. The system presents a running total and line-item details. The customer enters payment information, which the system validates and records. The system updates inventory. The customer receives a receipt from the system and then leaves with the items. Institute of Computer Software Nanjing University 2019/1/17
Actor, Scenario, Use Case 简单地说, an actor is something with behavior, such as a person (identified by role), computer system, or organization; for example, a cashier. A scenario is a specific sequence of actions and interactions between actors and the system; it is also called a use case instance. It is one particular story of using a system, or one path through the use case; for example, the scenario of successfully purchasing items with cash, or the scenario of failing to purchase items because of a credit payment denial. a use case is a collection of related success and failure scenarios that describe an actor using a system to support a goal. For example, here is a casual format use case with alternate scenarios: Institute of Computer Software Nanjing University 2019/1/17
Process Sale Institute of Computer Software 2019/1/17 Nanjing University 2019/1/17
Institute of Computer Software Nanjing University 2019/1/17
Actor有Goal Ivar Jacobson: A set of use-case instances, where each instance is a sequence of actions a system performs that yields an observable result of value to a particular actor. 软件工程师容易犯的错误:自认为所做的是客户/ 用户需要的。 不能代替客户/用户假想其价值所在。 Institute of Computer Software Nanjing University 2019/1/17
创建USE CASE的一些原则 用例可以是一个场景,包括动作和交互。 用例可以是一组场景,描述不同场景下的行为。这种书写格式 可以在任何时候描述有变体的行为,例如黑盒需求,业务流程, 系统设计说明。 用例里不要有系统设计,用例里不要有界面设计,用例里不要 有特性列表,用例里不要有测试。 用例应该描述行为需求。 用例的主场景不要超过九步。可以在适当的层次上得到子目标 和移除设计说明。 用例的最大价值不在于主场景,而是在于备选行为。主场景可 能只占用例长度的四分之一到十分之一。 Institute of Computer Software Nanjing University 2019/1/17
用例的识别 界定系统边界 基于参与者 基于事件 先识别参与者 对每个参与者,找出其所发起和参与的过程 识别系统必须响应的外部事件 把事件与参与者和用例联系起来 Institute of Computer Software Nanjing University 2019/1/17
边界,Actors,Goals Institute of Computer Software 2019/1/17 Nanjing University 2019/1/17
识别Actors及其Goals 除了明显的Actors之外,还要问问诸如以下这些问题: Who starts and stops the system? Who does system administration? Who does user and security management? Is "time" an actor because the system does something in response to a time event? Is there a monitoring process that restarts the system if it fails? Who evaluates system activity or performance? How are software updates handled? Push or pull update? Who evaluates logs? Are they remotely retrieved? In addition to human primary actors, are there any external software or robotic systems that call upon services of the system? Who gets notified when there are errors or failures?
Use cases favor a functional approach! “Also, name use cases starting with a verb.” NOT very useful in finding classes. But, useful in understanding requirements, validating the resulted model and implementation. 此外还要对照FURPS+检查其他需求 Institute of Computer Software Nanjing University 2019/1/17
Find the classes “The nouns and the verbs” 基本原则:ADT 启发:类与方法; 但是 “The elevator will close its door before it moves to another floor.” 有的名词不是类; 有的类不表现为名词; 基本原则:ADT Institute of Computer Software Nanjing University 2019/1/17
Find the classes 没有机械的办法。 基于经验的启发性、指导性的原则: 哪些东西是可能的类; 哪些形式的类不理想,可考虑从模型中去除; Institute of Computer Software Nanjing University 2019/1/17
The ideal class 明确的抽象 类名是名词或形容词,刻画该抽象 代表一系列运行时刻的对象。 有修改操作(或作用式操作) 形容词 常用于 表达接口 代表一系列运行时刻的对象。 (允许特意的singleton) 有修改操作(或作用式操作) 形式或非形式地刻画性质:不变式 前后置断言 There is a clearly associated abstraction, which can be described as a data abstraction (or as an abstract machine). • The class name is a noun or adjective, adequately characterizing the abstraction. • The class represents a set of possible run-time objects, its instances. (Some classes are meant to have only one instance during an execution; that is acceptable too.) • Several queries are available to find out properties of an instance. • Several commands are available to change the state of an instance. (In some cases, there are no commands but instead functions producing other objects of the same type, as with the operations on integers; that is acceptable too.) • Abstract properties can be stated, informally or (preferably) formally, describing: how the results of the various queries relate to each other (this will yield the invariant); under what conditions features are applicable (preconditions); how command execution affects query results (postconditions). Institute of Computer Software Nanjing University 2019/1/17
三种 类 分析类 设计类 实现类 直接对应于问题域 为得到优雅的、可扩展的结构而引入 满足软件系统实现算法所需。如LinkedList; Array等 Institute of Computer Software Nanjing University 2019/1/17
分析类 Analysis classes represent an early conceptual model for ‘things in the system which have responsibilities and behavior’. Analysis classes are used to capture a ‘first-draft’, rough-cut of the object model of the system. Analysis classes handle primarily functional requirements. They model objects from the problem domain. Institute of Computer Software Nanjing University 2019/1/17
分析类 There are three aspects of the system that are likely to change: the boundary between the system and its actors (boundary) the information the system uses (entity) the control logic of the system (control) .边界对象 边界对象代表系统的内部工作和它所处环境之间的交互。它包括有一个用户通过图形界面的交互,与其它角色的交互(例如代表其它系统的角色),和设备的交互等。边界对象将系统的其它部分和外部的相关事物隔离和保护起来。简单地说,每一个角色-用例交互对映射到一个边界对象。 . 实体对象 实体对象代表系统的重要信息。在一个很长的时间内,它们都是持久和存在的。它们的主要目的是表达和管理系统中的信息。在模型中,系统中的关键概念以实体对象来表现。 . 控制对象 控制对象是用来模型化系统中的行为的。控制对象并不需要实现这个行为,它可能是与其它对象协作以实现用例的行为。它的想法是为了将行为和模型下层的信息隔离开来,这样在处理以后的改变时就比较容易。 behaviour information boundary Institute of Computer Software Nanjing University 2019/1/17
Heuristics for finding classes 寻找分析类 Operational Model of some aspect of the external world. Simulation But external classes can be quite abstract 不一定是客 观实体,也可能是抽象概念 Institute of Computer Software Nanjing University 2019/1/17
Heuristics for finding classes 寻找实现类 实现难 多复用 数据结构与算法知识 延迟实现类 如“Queue” 类层次 组织 多种不同实现 Institute of Computer Software Nanjing University 2019/1/17
Heuristics for finding classes 寻找设计类 创造性工作 设计模式的用武之地 其他途径 旧系统 Adaptation through inheritance Institute of Computer Software Nanjing University 2019/1/17
Institute of Computer Software Nanjing University 2019/1/17
Institute of Computer Software Nanjing University 2019/1/17
Institute of Computer Software Nanjing University 2019/1/17
Institute of Computer Software Nanjing University 2019/1/17
Domain Model 领域模型 A domain model is a representation of real-world conceptual classes not a representation of software components. not a set of diagrams describing software classes, not software objects with responsibilities. A domain model is a visual representation of conceptual classes or real-world objects in a domain of interest [MO95, Fowler96] They have also been called conceptual models, domain object models, and analysis object models The UP defines a Domain Model as one of the artifacts that may be created in the Business Modeling discipline. Institute of Computer Software Nanjing University 2019/1/17
Domain Model Using UML notation, a domain model is illustrated with a set of class diagrams in which no operations are defined. It may show: domain objects or conceptual classes associations between conceptual classes attributes of conceptual classes >> Informally, a conceptual class is an idea, thing, or object. >> More formally, a conceptual class may be considered in terms of its symbol, intension, and extension[MO95]. Institute of Computer Software Nanjing University 2019/1/17
Domain Model Institute of Computer Software 2019/1/17 Nanjing University 2019/1/17
Domain Model Software problems can be complex; Decomposition (divide-and-conquer) is a common strategy to deal with this complexity by division of the problem space into comprehensible units. In structured analysis, the dimension of decomposition is by processes or functions. However, in object-oriented analysis, the dimension of decomposition is fundamentally by things or entities in the domain. A central distinction between object-oriented and structured analysis is: division by conceptual classes (objects) rather than division by functions. A primary analysis task is to identify different concepts in the problem domain and document the results in a domain model Institute of Computer Software Nanjing University 2019/1/17
System Sequence Diagrams 识别系统事件 定义系统操作 给出这些操作的Contracts Institute of Computer Software Nanjing University 2019/1/17
Contract CO2: enterItem Operation: enterItem(itemID: ItemID, quantity: integer) Cross References: Use Cases: Process Sale Preconditions: There is a sale underway. Postconditions: - A SalesLineItem instance sli was created (instance creation). - sli was associated with the current Sale (association formed). - sli.quantity became quantity (attribute modification). - sli was associated with a ProductDescription, based on itemID match (association formed). Institute of Computer Software Nanjing University 2019/1/17
接下来 建立系统类模型 类图 在类之间分配职责 Institute of Computer Software 2019/1/17 Nanjing University 2019/1/17
摘要 引言 如何发现“类” 如何设计“类” 小结 Institute of Computer Software 2019/1/17 Nanjing University 2019/1/17
Class Design OO设计是一个迭代的过程! 类设计:增加细节和执行精细决策 把分析类直接带入设计阶段 如果在设计阶段合并了分析模型,那么在演化的时 候,保持分析和设计模型的一致性会更容易 类设计:增加细节和执行精细决策 Institute of Computer Software Nanjing University 2019/1/17
Class Design 填补从高层需求到低层服务之间的空白 用操作实现用例 阐述每个操作的算法 向下递归到支持更高层操作的设计操作 选择算法;数据结构;设计内部类和操作 向下递归到支持更高层操作的设计操作 功能分层;机制分层 把模型重构成更简洁的设计 Institute of Computer Software Nanjing University 2019/1/17
Class Design 优化数据的访问路径 具体化必须操作的行为 调整类的结构以增加继承 组织类和关联 效率问题 Command, Strategy模式 调整类的结构以增加继承 重新调整类和操作;提取公共行为;使用委托来共 享行为 组织类和关联 信息隐藏;内聚性;微调包 Institute of Computer Software Nanjing University 2019/1/17
Class Design 创建初始设计类 定义操作 定义方法 定义状态 定义属性 定义依赖 定义关联 Institute of Computer Software Nanjing University 2019/1/17
How Many Classes Are Needed? 大部分,简单的类意味着每一个类 封装较少的整个系统的智能 更易被复用 更易实现 少部分,复杂的类意味着每个类 封装较多的整个系统的智能 不太会被复用 更难实现 A class should have a single well focused purpose. A class should do one thing and do it well! Institute of Computer Software Nanjing University 2019/1/17
小结 面向对象分析和设计 如何发现类 如何设计类 特点,过程,任务 用例 领域模型 Institute of Computer Software Nanjing University 2019/1/17 65
UML - Books Institute of Computer Software 2019/1/17 Nanjing University 2019/1/17
? UML - Tools Institute of Computer Software 2019/1/17 Nanjing University 2019/1/17
作业(本次作业不用提交) 复习UML,学习使用一种UML工具 Institute of Computer Software Nanjing University 2019/1/17