软件设计模式(二).ppt

上传人:marr****208 文档编号:156611191 上传时间:2020-12-19 格式:PPT 页数:77 大小:616.50KB
返回 下载 相关 举报
软件设计模式(二).ppt_第1页
第1页 / 共77页
软件设计模式(二).ppt_第2页
第2页 / 共77页
软件设计模式(二).ppt_第3页
第3页 / 共77页
软件设计模式(二).ppt_第4页
第4页 / 共77页
软件设计模式(二).ppt_第5页
第5页 / 共77页
点击查看更多>>
资源描述

《软件设计模式(二).ppt》由会员分享,可在线阅读,更多相关《软件设计模式(二).ppt(77页珍藏版)》请在金锄头文库上搜索。

1、软件设计模式(二),潘爱民 ,内容,复习 续:介绍一些重要的模式 Structural Patterns Behavioral Patterns,复习:pattern定义,定义: 特定环境中问题的成功解决方案中的静态、动态结构,以及结构元素相互之间的协作关系 Design patterns represent solutions to problems that arise when developing software within a particular context 关于pattern的研究状况 研究历史 现状 pattern与框架 pattern的分类 粒度,复习:如何描述一个模

2、式,关键要素 Design pattern name,Aliases or Also Known As Problem,Intent or Goal Forces,Constraints,Motivation Context, Applicability Solution Structure Participants Collaboration Implementation Evaluation,Resulting Context,Consequences Related Patterns Examples,Known uses,复习:creational patters,Factory Me

3、thod 本质:用一个virtual method完成创建过程 Abstract Factory 一个product族的factory method构成了一个factory接口 Prototype 通过product原型来构造product,Clone+prototype manager Builder 通过一个构造算法和builder接口把构造过程与客户隔离开 Singleton 单实例类型,如何构造这单个实例?如何访问这单个实例? Finder 把对象的获取过程与客户隔离开,creational patterns小结,了解每一种模式的实质 具体实现的时候可能会有变化情况,或者扩展,或者退化

4、 factory method是基础,abstract factory是它的扩展 factory method、abstract factory、prototype都涉及到类层次结构中对象的创建过程,有所取舍 prototype需要prototype manager factory method需要依附一个creator类 abstract factory需要一个平行的类层次 根据应用的其他需求,以及语言提供的便利来决定使用哪种模式,creational patterns小结(续),builder往往适合于特定的结构需要,它所针对的product比较复杂 singleton有比较强烈的物理意义

5、,可以用在许多细微的地方,不一定与类层次关联 finder模式需要有一定范围内的对象管理功能 这些patterns都很常见,有时需要结合两种或者多种模式完成系统中对象的构造过程,Structural Patterns,Adapter Bridge Composite * Decorator Facade Flyweight * Proxy,模式 7: Adapter (一),Aliases:Wrapper Intent Convert the interface of a class into another interface clients expect. Adapter lets cla

6、sses work together that couldnt otherwise because of incompatible interfaces. Motivation Sometimes a toolkit class thats designed for reuse isnt reusable only because its interface doesnt match the domain-specific interface an application requires.,Adapter模式(二),Applicability:Use the Adapter pattern

7、when you want to use an existing class, and its interface does not match the one you need. you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that dont necessarily have compatible interfaces. (object adapter only) you need to use several existi

8、ng subclasses, but its impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.,Adapter模式(三),Struct class adapter object adapter,Adapter模式(三),Participants Client、Target、Adaptee、Adapter Collaborations class adapter delegation object

9、 adapter container,Adapter模式(四),Evaluation 本质上是两种重用模型 class adapter: 无法adapt adaptee的子类,但是可以重载adaptee的行为 object adapter 可以adapt adaptee的所有子类 How much adapting does Adapter do? Pluggable adapters Using two-way adapters to provide transparency 针对class adapter,用多重继承来实现,Adapter模式(五),Implementation 使用C+继

10、承机制实现class adapter 使用内嵌对象技术实现object adapter Pluggable adapters,三种实现方案 使用抽象方法定义 使用代理对象 参数化技术 这三种方法的实质:如何在一个类中定义抽象操作,供客户插入? hook 技术,Adapter模式(六),Related Patterns Bridge Decorator Proxy Examples DrawCli:COleDrawObj C+ STL COM中的site,模式 8:Bridge(一),Aliases:Handle/Body Intent Decouple an abstraction from

11、its implementation so that the two can vary independently Motivation 要做到“抽象(接口)与实现分离”,最常用的办法是定义一个抽象类,然后在子类中提供实现。也就是说,用继承机制达到“抽象(接口)与实现分离” 但是这种方法不够灵活,继承机制把实现与抽象部分永久地绑定起来,要想独立地修改、扩展、重用抽象(接口)与实现都非常困难。,Bridge模式(二),Applicability:Use the Bridge pattern when 编译时刻无法确定抽象(接口)与实现之间的关系 抽象部分与实现部分都可以通过子类化而扩展 对一个实

12、现的修改不影响客户(无须重新编译) 在C+中,对客户完全隐瞒实现细节 因为扩展的原因,需要把一个类分成两部分,(以便灵活组合) 在多个对象之间共享数据,但客户不需要知道,Bridge模式(三),Struct Participants Client, Abstraction, RefinedAbstraction, Implementor, ConcreteImplementor Collaborations,Bridge模式(四),Evaluation 抽象部分与实现部分的分离,可以在运行时刻连接起来二进制兼容性 提高可扩充性:抽象与实现两部分可以单独扩充 对客户隐藏实现细节,Bridge模式

13、(五),Implementation Only one Implementor Creating the right Implementor object如何创建?根据客户环境,或者通过factory Sharing implementors 资源管理:引用计数技术 Using multiple inheritance,Bridge模式(六),Related Patterns Abstract Factory可以用来创建和配置Bridge模式 与Adapter模式的区别 Examples handle:文件handle、窗口handle,插:Handle/Body,class StringRe

14、p friend class String; StringRep(const char *s); StringRep(); int count; char *rep; ; class String public: String(); String(const String ,Counted Handle/Body,模式 9:Composite(一),Intent Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual obj

15、ects and compositions of objects uniformly. Motivation 一些部件对象经过组合构成的复合部件对象仍然具有单个部件对象的接口,这样的复合部件对象被称为“容器(container)” 复合部件与单个部件具有同样的接口,所有接口包含两部分:单个部件的功能、管理子部件的功能 递归组合,Composite模式(二),Applicability:Use the Composite pattern when you want to represent part-whole hierarchies of objects. you want clients t

16、o be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.,Composite模式(三),Struct Participants Client, Component, Leaf, Composite Collaborations,典型的composite对象结构,Composite模式(四),Evaluation defines class hierarchies consisting of primitive objects and composite objects。定义了包含leaf对象和composite对象的类层次接口。递归结构 makes the client simple。客户一致地处理复合对象和单个对象 makes it easier to add new kinds of components。易于增

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号