版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 务工劳工合同模板(3篇)
- 乡镇卫生院护理不良事件分析与防范
- 外汇购销合同模板(3篇)
- 统计文员合同模板(3篇)
- 2025年大关县辅警招聘考试备考题库附答案
- 2026年陕西事业编制招聘在哪里看备考题库附答案
- 2026年抖音考试题库附答案【综合卷】
- 2026中国建设银行宁夏回族自治区分行校园招聘180人(公共基础知识)综合能力测试题附答案
- 2025年江汉艺术职业学院单招(计算机)测试备考题库附答案
- 2026年民权县辅警招聘考试备考题库及答案1套
- 挖机迁坟工程合同(2篇)
- 人教部编版八年级语文上册现代文阅读教学课件:记叙文阅读(含小说、散文)考点七-理解段落作用-答题模板
- 气管切开套管意外脱管处理
- Python数据分析与应用-从数据获取到可视化(第2版)课件 第2章 科学计算库NumPy
- 糖尿病核心信息知识讲座
- 美容外科临床诊疗指南诊疗规范2023版
- 【语文】西安高新一小小学四年级上册期末试题
- 英语课堂小游戏PPT(18个游戏模板)
- 临床微生物检验-脑脊液标本检验(微生物检验技术课件)
- 快感体验能力量表(TEPS)
- 八年级期末考试多项细目表语文
评论
0/150
提交评论