Upload
bonek-mania
View
9
Download
0
Embed Size (px)
Citation preview
27-1/62
GoF Design Patterns Navigation
Three Types of Patterns
• Creational Patterns
• Structural Patterns
• Behavioral Patterns
27-2/62
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer
instantiation to subclasses.
Creational Pattern: Factory Method
绝大多数设计模式利用了多态性表达能力
无法预计创建哪一具体类的对象实例
27-3/62
Object Behavioral Pattern: Strategy
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from
clients that use it.
即使在运行时也可方便地切换不同算法
不同的实现有不同的效果:时间、空间、观感、 ...
27-4/62
Object Structural Pattern: Decorator
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
通过复合实现功能委派通过继承保
持一致接口
27-5/62
Object Behavioral Pattern: Iterator
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
基础数据结构的接口
数据结构的具体实现
27-6/62
Class Behavioral Pattern: Template Method
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine
certain steps of an algorithm without changing the algorithm's structure.
是运用多态的典型例子
27-7/62
Class/Object Structural Pattern: Adapter
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of
incompatible interfaces.
客户程序不必看到真正工作的类
又称Wrapper,该名字也很形象
可能是第三方产品或遗留系统,故无法更改接口
We got the right object, but the wrong interface.
Adapter复合 Adaptee则称对象适配器Adapter继承 Adaptee则称类适配器
adaptee
27-8/62
Object Structural Pattern: Proxy
Provide a surrogate or placeholder for another object to control access to it.
该类未必与代理接口相同,此时代理还充当适配器的角色
可能通过网络协议实现
因 RealSubject实例化成本高,故代以创建一 Proxy
27-9/62
Object Behavioral Pattern: Chain of Responsibility
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving obje
cts and pass the request along the chain until an object handles it.
链中每一结点必须具有统一的接口
客户程序中的请求交由一组 Hand
ler处理
27-10/62
Object Structural Pattern: Facade
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to
use.
对外统一的简单接口
如有需要仍可访问子系统中复杂的内部类