版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 Ch9 Design Patterns(1)Programming to InterfacesOCPSoftware System Design and ArchitectureMain ContentsDesign patternsProgramming to InterfacesOCPWhat is a Design Pattern?A design patternabstracts a recurring design structurecomprises class and/or objectdependencies, structures, interactions, orconv
2、entions distills design experienceElements of Design PatternsDesign patterns have 4 essential elements:Pattern name: increases vocabulary of designersProblem: intent, context, when to apply Solution: UML-like structure, abstract codeConsequences: results and tradeoffsMain ContentsDesign patternsProg
3、ramming to InterfacesOCPProgramming to Interfaces, not Implementations“Connections that address or refer to a module as a whole by its name yield lower coupling than connections referring to the internal elements of another module”Single values VS a collection of valuesTake Care Friend methods and R
4、eferencesShare Data !Iterator PatternIntent Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.Supports multiple traversals of aggregate objects.Provides a uniform interface for traversing different aggregate structures (it support
5、s polymorphic iteration).Abstraction Mechanisms in CLU, Liskov, 1977createIterator( )Iterator PatternStructure/client codeAggregate myList = new ConcreteAggregate( );Iterator itr = myList.createIterator( );Iterator PatternConsequencesProgramming to interfaces and Information HidingIt supports variat
6、ions in the traversal of an aggregate. For example, code generation may traverse the parse tree inorder or preorder. Iterators make it easy to change the traversal. Just replace the iterator instance with a different one.Iterators simplify the Aggregate interface. Iterators traversal interface obvia
7、tes the need for a similar interface in Aggregate.More than one traversal can be pending on an aggregate.11ClientSubjectabstractRequest()RealSubjectRequest()ProxyRequest()refers to-Request()The Proxy Pattern: SolutionThe Proxy PatternThe ProblemProvide a surrogate or placeholder for another object t
8、o control access to itRemote Proxy: provide local representative for object in different address space, may re-code request.Virtual Proxy: creates expensive objects on demand, like in example, may also cache.Protection Proxy: control access to original object, useful when access rights differ.The Pr
9、oxy Pattern ConsequencesThe Proxy pattern introduces a level of indirection when accessing an object:A remote proxy can hide the fact that the object resides in a different address spaceA virtual proxy can perform optimisationsAllow additional housekeepingPrototype PatternClassification Class , Prot
10、otype ClassSpecify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototypePrototype Pattern UMLParticipants: Prototypeo declares an interface for cloning itself. ConcretePrototype o implements an operation for cloning itself. Cliento creates a n
11、ew object by asking a prototype to clone itself.Lets Try!思考题有一个数据列表DataList,其基本类型是3维向量ThreeD1)用Java语言实现该数据列表的数据结构假设有三个外部对象A,B,C,分别对其x、y、z维度感兴趣,希望访问DataList在相应维度的数据并进行处理2)请定义其对外的数据接口分别使用迭代器与ProxyMain ContentsDesign patternsProgramming to InterfacesOCPOCPFunction of InheritanceIncremental developmentD
12、ata Abstraction and Hierarchy, LiskovModify closed, increment easierOn the Notion of Inheritance,ANTERO TAIVALSAARIAfter 1990s, requirements changed become criticalOCP (depend DIP)Open/Closed Principle (OCP)Software entities should be open for extension, but closed for modificationB. Meyer, 1988 / q
13、uoted by R. Martin, 1996Be open for extensionmodules behavior can be extendedBe closed for modificationsource code for the module must not be changesThere is no 100% OCP!A Simple Example of OCPCopyReadKeyboardWritePrintervoid Copy(ReadKeyboard& r, WritePrinter& w) int c; while (c = r.read () != EOF)
14、 w.write (c);WriteDiskenum OutputDevice printer, disk; void Copy(ReadKeyboard& r, WritePrinter& wp, WriteDisk& wd, OutputDevice dev) int c; while(c = r.read()!= EOF) if(dev = printer) wp.write(c); else wd.write (c);A Simple Example of OCPDiskWriter:Write(c) WriteDisk(c);CopyKeyboardReaderPrinterWrit
15、erDiskWritervoid Copy(ReadKeyboard& r, WritePrinter& w) int c; while (c = r.read () != EOF) w.write (c);思考题Extension:Border( Plain, 3D, Fancy)ScrollBar( Horiz, Vert).VisualComponentdraw()resize()TextViewdraw()resize()SteamedVideoViewdraw()resize()24The Decorator Pattern: ProblemsDecorators expand th
16、e functionality of an instance of a class without changing the class code Add borders or scrollbars to a GUI componentAdding items such as scroll bars as needed provides more flexibility than requiring all windows to have scroll barsAdd headers and footers to an advertisementAdd stream functionality
17、 such as reading a line of input or compressing a file before sending it over the wireMore flexibility than static inheritanceDecorators work behind the scenes, they are transparent to the interfaceThe Decorator Pattern: The SolutionVisualComponentdraw()resize()TextViewdraw()resize()Borderdraw()resi
18、ze()Decoratordraw()resize()ScrollBardraw()resize()SteamedVideoViewdraw()resize()11Plaindraw()resize()3Ddraw()resize()Fancydraw()resize()Decorator contains a visual componentHorizdraw()resize()Vertdraw()resize()27The Decorator Pattern: ConsequencesMore flexible than static inheritanceAvoids feature laden classes high up in hierarchyLo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年石狮市琼林中心幼儿园合同教师招聘备考题库及一套完整答案详解
- 2026年自助烧烤场地租赁合同
- 2026年贵族生活方式分享课程合同
- 2025年中国科学院心理研究所认知与发展心理学研究室杜忆研究组招聘备考题库及参考答案详解
- 2025执业药师继续教育试题库(含答案)
- 2025年北京体育大学医院(社区卫生服务中心)合同制人员公开招聘备考题库及参考答案详解1套
- 2025年中国水利水电科学研究院水力学所科研助理招聘备考题库及完整答案详解1套
- 2025年兴业银行总行社会招聘备考题库参考答案详解
- 2025年河南洛阳63880部队社会招聘备考题库及完整答案详解一套
- 中国电建集团贵阳勘测设计研究院有限公司2026届秋季招聘40人备考题库完整参考答案详解
- 2025秋人教版(新教材)初中美术八年级上册知识点及期末测试卷及答案
- DB50∕T 867.76-2025 安全生产技术规范 第76部分:汽车制造企业
- 2026年保安员考试题库500道附完整答案(历年真题)
- 2025至2030中国司法鉴定行业发展研究与产业战略规划分析评估报告
- (2025年)危重病人的观察与护理试题及答案
- 膝关节韧带损伤康复课件
- 个人契约协议书范本
- 医药区域经理述职报告
- 养老事业与养老产业协同发展路径探析
- 建筑施工项目职业病危害防治措施方案
- 船员上船前安全培训课件
评论
0/150
提交评论