教案软件开发20151_第1页
教案软件开发20151_第2页
教案软件开发20151_第3页
教案软件开发20151_第4页
教案软件开发20151_第5页
已阅读5页,还剩47页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Chap 16 GRASP: Designing Objects With Responsibilitiesn目标:n1.定义模式n2.学习运用5个GRASP模式2022-4-25东南大学“软件开发方法与技术”课程主要内容Chap 9 用例模型:绘制系统顺序图Chap 10 领域模型:可视化概念Chap 11 领域模型:添加关联Chap 12 领域模型:添加属性Chap 13 用例模型:用操作契约增加细节Chap 14 迭代中的从需求到设计Chap 15 交互图表示法Chap 16 GRASP:根据职责设计对象Chap 17 设计模型:GRASP模式与用例实现Chap 18 设计模型:决定可见

2、性Chap 19 设计模型:创建设计类图Chap 20 实现模型:将设计映射成代码2022-4-25东南大学“软件开发方法与技术”课程设计n在确定了需求和产生领域模型之后,为软件类添加方法,并且定义对象间的消息传递,从而实现需求.n主要目标:决定哪些方法位于哪些地方以及对象间如何交互的.2022-4-25东南大学“软件开发方法与技术”课程Responsibilities and MethodsnResponsibilities are assigned to classes of objects during object design. nE.g., “a Sale is responsib

3、le for creating SalesLineItems” (a doing“做做”型型), nor “a Sale is responsible for knowing its total” (a knowing“知道知道”型型). 2022-4-25东南大学“软件开发方法与技术”课程R&Mn“做”型职责包括: (1)自己执行一些行为,如创建一个对象或进行一次计算等;(2)在其它对象中初始化操作;(3)在其它对象中控制或协调各项活动.n“知道”型职责包括: (1)了解私有的封装数据;(2)了解相关联的对象;(3)了解能够派生或者计算的事物.2022-4-25东南大学“软件开发方法

4、与技术”课程R&MnA responsibility is not the same thing as a method, but methods are implemented to fulfill responsibilities.n职责是对象设计过程中被分配给对象类的. 例如“一个Sale类负责产生一个SalesLineItem类”(一个“做”型职责);又如,“一个Sale类知道sale对象的total”.2022-4-25东南大学“软件开发方法与技术”课程Responsibilities and Interaction Diagramn交互图体现了如何为对象分配职责交互图体现了

5、如何为对象分配职责. . 当交互图创建当交互图创建时时, , 实际上已经为对象分配了职责实际上已经为对象分配了职责, ,反应到交互图反应到交互图就是该对象发送相应的消息到不同类的对象就是该对象发送相应的消息到不同类的对象. .n本章的重点本章的重点: GRASP: GRASP模式的基本原则模式的基本原则, ,以及如何运用以及如何运用GRASPGRASP模式来指导职责分配问题模式来指导职责分配问题. .2022-4-25东南大学“软件开发方法与技术”课程2022-4-25东南大学“软件开发方法与技术”课程PatternsnOO developers build up a repertoire o

6、f全部技能 both general principles and idiomatic solutions that guide them in the creation of software. nA pattern is a named problem/solution pair. nSample pattern:nPattern Name: Information ExpertnProblem What is a basic principle by which to assign responsibilities to objects?nSolution: Assign a respo

7、nsibility to the class that has the information needed to fulfill it.2022-4-25东南大学“软件开发方法与技术”课程Patternsn简单地说,模式是一种已命名的问题/解决方案对,可以应用到新的语境中,并提供了如何在新场合下应用的建议和优缺点的讨论.n两点强调:n (1)模式应该是不断重复的n (2)模式拥有名称提炼和整合诸多概念便于理解和记忆;有利于交流,如下例.2022-4-25东南大学“软件开发方法与技术”课程PatternsnFred: “你认为我们应该把产生一个SaleLineItem对象的职责分配给谁?如果采

8、用工厂模式,我认为使用Register类比较合适.”nWilma:“使用创建者模式吧,我认为Sale类更合适.”nFred: “好的,你真棒!我怎么没想到!”2022-4-25东南大学“软件开发方法与技术”课程GRASPnGRASP is an acronym(只取首字母的缩写) that stands for General Responsibility Assignment Software Patterns.nFirst five GRASP patterns:nInformation Expert(信息专家)nCreator(创建者)nLow Coupling(低耦合)nHigh Co

9、hesion(高内聚)nController(控制器)2022-4-25东南大学“软件开发方法与技术”课程Information Expert Pattern信息专家模式信息专家模式nProblem: What is a general principle of assigning responsibilities to objects? nSolution: Assign a responsibility to the information expertthe class that has the information necessary to fulfill the responsib

10、ility.2022-4-25东南大学“软件开发方法与技术”课程IEnE.g. In the POS application, some class needs to know the grand total (总数之和总数之和)of a sale.nWho should be responsible for that?nPerhaps the Sale class is the information expert to know the total. nHowever, the Sale needs other classes to plish that.2022-4-25东南大学“软件开

11、发方法与技术”课程IE2022-4-25东南大学“软件开发方法与技术”课程IEnWhat information is needed to determine the grand total? n (1)It is necessary to know about all the SalesLineItem instances of a sale and the sum of their subtotals. n (2)A Sale instance contains these; therefore, Sale is an information expert for the work.202

12、2-4-25东南大学“软件开发方法与技术”课程nWhat information is needed to determine the line item subtotal? n SalesLineItem.quantity and ProductSpecification.price are needed. nThe SalesLineItem knows its quantity and its associated ProductSpecification; therefore, by Information Expert, SalesLineItem should determine

13、the subtotal; it is the information expert.IE2022-4-25东南大学“软件开发方法与技术”课程2022-4-25东南大学“软件开发方法与技术”课程A few more words about Expertn信息专家模式核心思想信息专家模式核心思想: :把职责分配给具有完成某把职责分配给具有完成某项职责所需信息的个体项职责所需信息的个体. .n信息专家模式在职责分配中使用得非常广泛信息专家模式在职责分配中使用得非常广泛, ,它是它是对象设计中经常使用的基本指导原则对象设计中经常使用的基本指导原则. .n职责的履行需要信息职责的履行需要信息, ,而信

14、息往往分布在不同的对而信息往往分布在不同的对象中象中. .这就意味着许多这就意味着许多“部分部分”的信息专家有时需的信息专家有时需要协作才能来完成一个任务要协作才能来完成一个任务( (如上例如上例).).2022-4-25东南大学“软件开发方法与技术”课程A few more words 例外情况:例外情况:The Expert is very frequently used,however, not always. E.g. who should be responsible for saving a sale in a database?This is a complicated resp

15、onsibility, and if we assign it to Sale, it will lead to problems related to cohesion and coupling. 2022-4-25东南大学“软件开发方法与技术”课程优点n因为对象使用自己的信息来实现服务,所以信息的封装性得以维持. 低耦合/健壮性/易维护性.n行为分散在不同的类中,这些类各自具有完成行为所需要的信息. 这些“轻量级”的类易于理解和维护.2022-4-25东南大学“软件开发方法与技术”课程Creator Pattern创建者模式创建者模式nProblem: We want to have a

16、general principle for the assignment of creation responsibilities(分配创分配创建对象职责建对象职责). nSolution: Assign class B the responsibility to create an instance of class A if one or more of the following is true:2022-4-25东南大学“软件开发方法与技术”课程Creator PatternnFor example: In NextGen POS, Who should be responsible

17、for the creation of a SalesLineItem? (哪个类应该负责产生SalesLineItem的实例?)n根据创建者模式,我们应该寻找哪个类聚合或者包含了SalesLineItem类的实例.因为一个Sale类包含(实际上是聚合)了许多SalesLineItem对象,所以创建者模式指明Sale类是创建SalesLineItem类实例的很好的候选者.2022-4-25东南大学“软件开发方法与技术”课程2022-4-25东南大学“软件开发方法与技术”课程Creator Pattern2022-4-25东南大学“软件开发方法与技术”课程nThis assignment req

18、uires that a method makeLineItem is defined in the Sale class.n注意(again):职责是在绘制交互图的时候考虑和决定的.类图的方法栏表示了职责分配的结果,职责具体由这些方法来实现.Creator Pattern2022-4-25东南大学“软件开发方法与技术”课程Creator Patternn讨论n 1.创建者模式指导怎样分配“与创建对象”相关的职责。创建者模式的基本目的是找到一个在任何情况下都与被创建对象相关联的创建者,选择这样的类作为创建者能支持低耦合。n 2.集合聚集了部分,容器包含了内容,记录器记录了被记录的数据,这些类之

19、间的关系在类图中非常普遍. Creator Pattern建议:封装的容器类和记录器类是创建“自己包含或者记录的元素”的很好候选者.2022-4-25东南大学“软件开发方法与技术”课程Creator Patternn优点优点: : 支持低耦合支持低耦合, ,这种设计意味着具有更低的这种设计意味着具有更低的维护依赖性和更高的重用机会维护依赖性和更高的重用机会. . 创建者模式之所创建者模式之所以可能不增加耦合性是由于被创建类对于创建类以可能不增加耦合性是由于被创建类对于创建类而言已经可见了而言已经可见了, ,正是因为已存在的关联使得它正是因为已存在的关联使得它成为创建者成为创建者. .2022-

20、4-25东南大学“软件开发方法与技术”课程nSometimes the creation might be complicated, e.g. using recycled instances for performance reasons. nIn this cases it is advisable to delegate the creation to some helper class (Factory pattern: chap 23).Complicated Creation2022-4-25东南大学“软件开发方法与技术”课程Low Coupling Pattern低耦合低耦合模式

21、模式nProblem: How to support low dependency, low change impact, and increased reuse? (怎样支持低的依赖性,怎样支持低的依赖性,减小变更带来的影响,提高重用性减小变更带来的影响,提高重用性)nSolution: Assign a responsibility so that coupling remains low.2022-4-25东南大学“软件开发方法与技术”课程nCoupling is a measure of how strongly one class relies on other classes. n

22、A class with high (or strong) coupling relies on many other classes. Such classes may be undesirable; some suffer from the following problems:nChanges in related classes force local changes.nHarder to understand in isolation.nHarder to reuse.Low Coupling Pattern2022-4-25东南大学“软件开发方法与技术”课程Example Bad

23、Couplingn举例:考虑在销售点终端系统中,假设要创建一举例:考虑在销售点终端系统中,假设要创建一个个Payment的实例并使它和的实例并使它和Sale之间的实例关之间的实例关联,哪个类应当承担职责呢?在真实世界中联,哪个类应当承担职责呢?在真实世界中Register记录了一个记录了一个Payment的实例,根据创的实例,根据创建者模式,建者模式, Register类应该是创建类应该是创建Payment的的候选类。候选类。2022-4-25东南大学“软件开发方法与技术”课程Bad Couplingn方案一:一个方案一:一个Register发送一个发送一个addPayment消息给消息给Sa

24、le实例,并将新生成的实例,并将新生成的Payment实例作为一个参数传实例作为一个参数传递给递给Sale。 Register需要知道需要知道Payment类的消息,两者类的消息,两者被耦合。被耦合。2022-4-25东南大学“软件开发方法与技术”课程Good Couplingn方案二:一个方案二:一个Sale实例最终要和一个实例最终要和一个Payment实例耦合起实例耦合起来。因此来。因此Payment由由Sale创建,没有增加耦合度,所以整创建,没有增加耦合度,所以整体耦合度低。体耦合度低。2022-4-25东南大学“软件开发方法与技术”课程Bad Coupling2022-4-25东南大

25、学“软件开发方法与技术”课程Good Coupling (why?)2022-4-25东南大学“软件开发方法与技术”课程Low Coupling Patternn讨论: (1)低耦合是一个设计目标,也是一个评估原则; (2)低耦合模式鼓励职责分配时不增加耦合性.从而避免了高耦合可能产生的不良后果;(3)低耦合模式的极端情况就是类之间没有耦合.n优点: (1)不受其它组件改变的影响;(2)便于单独理解;(3)重用方便.2022-4-25东南大学“软件开发方法与技术”课程Low Coupling Pattern2022-4-25东南大学“软件开发方法与技术”课程High Cohesion Patt

26、ern高内高内聚模式聚模式nProblem How to keep complexity manageable?nSolution Assign a responsibility so that cohesion remains high.2022-4-25东南大学“软件开发方法与技术”课程High Cohesion PatternnA class with highly related responsibilities, and which does not do a tremendous amount of work, has high cohesion. nA class with lo

27、w cohesion does many unrelated things, or does too much work. They are:nhard to comprehendnhard to reusenhard to maintainndelicate; constantly effected by change(系统脆弱,常常受到变化带来的影响)2022-4-25东南大学“软件开发方法与技术”课程ExamplenThe same example problem used in the Low Coupling pattern can be analyzed for High Cohe

28、sion.nIf we make the Register responsible for doing work related to more operations, it will e increasingly burdened with tasks and e incohesive.2022-4-25东南大学“软件开发方法与技术”课程ExamplenThe point is not that this single Payment creation task in itself makes the Register incohesive, but as part of a larger

29、picture, it may suggest a trend toward low cohesion.2022-4-25东南大学“软件开发方法与技术”课程Example2022-4-25东南大学“软件开发方法与技术”课程Example2022-4-25东南大学“软件开发方法与技术”课程Varying Degrees of Functional CohesionnVery low cohesion. A class is solely responsible for many things in very different functional areas.RDB-RPC-Interface

30、nLow cohesion. A class has sole responsibility for a complex task in one functional area.RDBInterface 2022-4-25东南大学“软件开发方法与技术”课程Varying Degrees of Functional CohesionnHigh cohesion. A class has moderate responsibilities in one functional area and collaborates with other classes to fulfill tasks.RDBI

31、nterfacenModerate cohesion. A class has lightweight and sole responsibilities in a few different areas that are logically related to the class concept, but not to each other.Company2022-4-25东南大学“软件开发方法与技术”课程High Cohesion Patternn优点: 设计的清晰性和易于理解性得到提高。(1)维护和扩展得到简化;(2)常常支持低耦合;(3)重用性提高。2022-4-25东南大学“软件开发方法与技术”课程Controller Pattern控制器模控制器模式式nProblem nAn input system event is an event gene

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论