软件系统设计与体系结构:Ch9 Design Patterns(1)_第1页
软件系统设计与体系结构:Ch9 Design Patterns(1)_第2页
软件系统设计与体系结构:Ch9 Design Patterns(1)_第3页
软件系统设计与体系结构:Ch9 Design Patterns(1)_第4页
软件系统设计与体系结构:Ch9 Design Patterns(1)_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

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

文档简介

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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论