软件设计模式(二) 潘爱民 http://www.icst.pku.edu.cn/compcourse
软件设计模式 ( 二 ) 潘爱民 http://www.icst.pku.edu.cn/compcourse
内容 复习 续:介绍一些重要的模式 Structural patterns Behavioral patterns 第二次作业
内容 • 复习 • 续:介绍一些重要的模式 – Structural Patterns – Behavioral Patterns • 第二次作业
复习: pattern定义 定义 特定环境中问题的成功解决方案中的静态、动态结构, 以及结构元素相互之间的协作关系 Design patterns represent solutions to problems that arise when developing software within a particular context 关于 pattern的研究状况 研究历史 现状 pattern与框架 pattern分类 粒度
复习:pattern定义 • 定义: – 特定环境中问题的成功解决方案中的静态、动态结构, 以及结构元素相互之间的协作关系 – Design patterns represent solutions to problems that arise when developing software within a particular context • 关于pattern的研究状况 – 研究历史 – 现状 • pattern与框架 • pattern的分类 – 粒度
复习:如何描述一个模式 关键要素 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
复习:如何描述一个模式 • 关键要素 – 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 Method 本质:用一个 virtual method完成创建过程 Abstract Factor 个 product族的 actory method构成了一个 factory接口 Prototype 通过 product原型来构造 product, Clone+ prototype manager Builder 通过一个构造算法和 builder接口把构造过程与客户隔离开 Singleton 单实例类型,如何构造这单个实例?如何访问这单个实例? Finder 把对象的获取过程与客户隔离开
复习:creational patters • Factory Method – 本质:用一个virtual method完成创建过程 • Abstract Factory – 一个product族的factory method构成了一个factory接口 • Prototype – 通过product原型来构造product,Clone+prototype manager • Builder – 通过一个构造算法和builder接口把构造过程与客户隔离开 • Singleton – 单实例类型,如何构造这单个实例?如何访问这单个实例? • Finder – 把对象的获取过程与客户隔离开
Structural patterns Adapter Bridge Composite R Decorator Facade Flyweight k Proxy
Structural Patterns • Adapter • Bridge • Composite • * Decorator • Facade • Flyweight • * Proxy
模式7: Adapter(-) Aliases: Wrapper Intent Convert the interface of a class into another inter face clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces Motivation Sometimes a toolkit class that's designed for reuse isn't reusable only because its interface doesn't match the domain-specific inter face an application requires
模式 7: Adapter (一) • Aliases:Wrapper • Intent – Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. • Motivation – Sometimes a toolkit class that's designed for reuse isn't reusable only because its interface doesn't match the domain-specific interface an application requires
Adapter模式(二) Applicability: Use the Adapter pattern 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 don't necessarily have compatible interfaces (object adapter only) you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class
Adapter模式(二) • Applicability:Use the Adapter pattern 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 don't necessarily have compatible interfaces. – (object adapter only) you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class
Adapter模式( Struct class Client Target Adaptee Request( SpecificRequesto) adapter (impl Adapter Request(o- SpecificRequest() object Client Target Adaptee Request( Specific Request adapter adaptee Adapter Request()o卜- adaptee->SpecificRequesto
Adapter模式(三) • Struct class adapter object adapter
Adapter模式(三) ° Participant Client、 Target、 Adaptee、 Adapter Collaborations class adapter-delegation obJect adapter container
Adapter模式(三) • Participants – Client、Target、Adaptee、Adapter • Collaborations – class adapter —— delegation – object adapter —— container