版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 Ch11 Design Patterns(3)Indirection (Decoupling)Runtime RegistrationSoftware System Design and ArchitectureMain ContentsIndirection (Decoupling)Runtime RegistrationDe-Coupling MethodsAvoiding Repetition: Do it OnceDuplication always implies coupling, because changing one piece of duplicate code impl
2、ies changing the otherDIPDefining an interface in a module that a separate module intends to implement is a fundamental way to break dependencies and reduce coupling.IndirectionDIPAbstractions should not depend on details. Details should depend on abstractionsPolicy LayerMechanismLayerUtilityLayerde
3、pends ondepends onPolicy LayerMechanismLayerUtilityLayerdepends ondepends onMechan. ServiceInterfacePolicy ServiceInterfaceIs DIP is a kind of Indirection?The Facade Design Pattern: SolutionConsequencesit decouples a client from a subsystem of components.DelegationIs delegator enough?7ClientSubjecta
4、bstractRequest()RealSubjectRequest()ProxyRequest()refers to-Request()The Proxy Pattern: SolutionThe ProblemProvide a surrogate or placeholder for another object to control access to itAdapter Pattern Adapter SolutionAdapter Pattern Adapter ProblemThe Adapter Pattern converts the interface of a class
5、 into another interface the clients expect.Adapter lets classes work together that couldnt otherwise because of incompatible interfaces.可以应用Adapter的常见情景有哪些?Mediator PatternProblemA set of objects communicates in well-defined, but complex ways. Often with unstructured dependencies.Reusing objects is
6、difficult because it refers to and communicates with many other objects.A behavior thats distributed between several classes should be customizable without a lot of subclassing.13Mediator Pattern StructureParticipantsMediator Defines an interface for communicating with Colleague objects Typically me
7、diator is informed of some event or situationConcreteMediator Implements cooperative behavior by coordinating Colleague objects Knows and maintains its colleaguesColleague classes Each Colleague class knows its Mediator object Each colleague communicates with its mediator whenever it would have othe
8、rwise communicated with another colleague Offers services (requests) to mediator There may be requests that are common to all colleagues, as well as specific ones.ConsequencesDefine an object that encapsulates how a set of objects interact.Mediator promotes loose coupling by keeping objects from ref
9、erring to each other explicitlyLets you vary their interaction independentlyCentralizes control思考题用Mediator Pattern 模拟实现麻将游戏的框架四人,其中一个为庄家顺时针游戏顺序只模拟摸牌、出牌、吃牌、碰牌、槓牌和胡牌行为不要求详细的行为判断规则Main ContentsIndirection (Decoupling)Runtime RegistrationRuntime registration and Call-backsA callback is a client method
10、that can be registered to a server object so that the server can call it backExample: registration of client code to be called when server catch user eventsThey are a mechanism for extensibilityCall-back: message sequence chartClientUserServerClient installs callbackUser calls serverServer invokes c
11、allbackCallback queries serverCallback returnsServer returnsCallsResultsObserver PatternObserver and EventObserver PatternProblemDefine a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automaticallyObserver PatternSolutionObs
12、erver Pattern ConsequencesFlexibility, changeability, and reusabilityLow coupling with “event” styleMore complexMore difficult for comprehension and testObserver Pattern SummaryUse the Observer pattern in any of the following situations:When a change to one object requires changing others, and you d
13、ont know how many objects need to be changed. When an object should be able to notify other objects without making assumptions about who these objects are. In other words, you dont want these objects tightly coupled. 思考题用observer编写程序:Model: student(ID, name, birthday)View:View1: 显示 ID+nameView2:显示ID
14、+birthdayView3:修改student的三个列View12为observer,model为subject实现每次view3中实现修改后View12自动更新思考题与真正自实现的Event Style相比,Observer Pattern的不同在哪里?如果众多Observer的接口不相同怎么办?不都是update接口,甚至不是同一种类型Command Pattern: Problemsparameterize objects by actions they performspecify, queue, and execute requests at different timessupp
15、ort undo by storing context information support change log for recovery purposes27Command Pattern: The SolutionUNDO/ REDO: Commandpublic abstract class AbstractCommand public abstract boolean do(); public abstract boolean undo();public class SalesCommand extends AbstractCommand /private historybehav
16、ior; update() /delegate to others, update new data, establish historybehavior clear() /delegate to others, clear updated data according to historybehavior public boolean do() update(); return true; /undoable public boolean undo() clear(); return true; UNDO/ REDO: Command/this would possibly be a goo
17、d candidate for singleton class alsoClass CommandManager private int maxHistory = 100; private List undo; private List redo; public void synchronization() /for all concrete command if (command.do() addToUndo(command); public void undo( ) /for each command of undo List if (command.undo() undo.clear(command); addToRedo(command); Co
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 山东2025年鲁东大学招聘工作人员12人笔试历年参考题库附带答案详解
- 南通江苏南通如东县城市管理局招聘政府购买服务城管协管员12人笔试历年参考题库附带答案详解
- 伊春2025年黑龙江铁力市林业和草原局所属事业单位招聘10人笔试历年参考题库附带答案详解
- 生产安全教育培训费用课件
- 生产安全教育培训内容课件
- 企业会计准则制度
- 人工流产制度
- 卫生院重点药品监控制度
- 中学生卫生管理制度
- 乡镇卫生院应急总结制度
- 文物安全保护责任书范本
- 广东省惠州市某中学2025-2026学年七年级历史上学期期中考试题(含答案)
- 2025公文写作考试真题及答案
- 停电施工方案优化(3篇)
- DB64∕T 1279-2025 盐碱地综合改良技术规程
- 2025年度耳鼻喉科工作总结及2026年工作计划
- 2024年执业药师《药学专业知识(一)》试题及答案
- 高压氧进修课件
- 2025年第三类医疗器械经营企业质量管理自查报告
- 2025无人机物流配送网络建设与运营效率提升研究报告
- 产品工艺评审管理办法
评论
0/150
提交评论