设计模式之九:Adapter(适配器)—类对象结构型模式

22:01:24 星期一 22:01:37 

Adapter,继续GOF。博客园 设计模式之九:Adapter(适配器)—类对象结构型模式

1、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.

将一个类的接口转换成客户希望的另外一个接口。 Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

2、Also Known As

Wrapper 包装器

3、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.

Consider for example a drawing editor that lets users draw and arrange graphical elements (lines, polygons, text, etc.) into pictures and diagrams. The drawing editor's key abstraction is the graphical object, which has an editable shape and can draw itself. The interface for graphical objects is defined by an abstract class called Shape. The editor defines a subclass of Shape for each kind of graphical object: a LineShape class for lines, a PolygonShape class for polygons, and so forth.

有时,为复用而设计的工具箱类不能够被复用的原因仅仅是因为它的接口与专业应用领域所需要的接口不匹配。

例如,有一个绘图编辑器,这个编辑器允许用户绘制和排列基本图元(线、多边型和正文等)生成图片和图表。这个绘图编辑器的关键抽象是图形对象。图形对象有一个可编辑的形状,并可以绘制自身。图形对象的接口由一个称为 S h a p e的抽象类定义。绘图编辑器为每一种图形对象定义了一个 S h a p e的子类: L i n e S h a p e类对应于直线, P o l y g o n S h a p e类对应于多边型,等等。


This diagram illustrates the object adapter case. It shows how BoundingBox requests, declared in class Shape, are converted to GetExtent requests defined in TextView. Since TextShape adapts TextView to the Shape interface, the drawing editor can reuse the otherwise incompatible TextView class.

上面的类图说明了对象适配器实例。它说明了在 S h a p e类中声明的 B o u n d i n g B o x请求如何被转换成在 Te x t Vi e w类中定义的 G e t E x t e n t请求。由于 Te x t S h a p e将Te x t Vi e w的接口与 S h a p e的接口进行了匹配,因此绘图编辑器就可以复用原先并不兼容的 Te x t Vi e w类。

4、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.

在以下情况使用适配模式:

 你想使用一个已经存在的类,而它的接口不符合你的需求。

 你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类(即那些接口可能不一定兼容的类)协同工作。

 (仅适用于对象 A d a p t e r)你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口。对象适配器可以适配它的 父类接口。

5、Structure

● class adapter


● object adapter


6、代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#if 1
class  Adaptee
{
public :
     void  SpecialRequest() {}
};
 
class  Target
{
public :
     virtual  void  Request() = 0;
};
 
class  Adapter :  public  Target,  private  Adaptee
{
public :
     virtual  void  Request() {SpecialRequest();}
};
 
 
class  Adapter_Object :  public  Target
{
public :
     virtual  void  Request() {_adaptee.SpecialRequest();}
 
private :
     Adaptee _adaptee;
};
 
//客户端代码:
void  Adapter_Demo()
{
     Target *p =  new  Adapter();
     p->Request();         //实际上调用的是Adaptee::SpecialRequest()
 
     Target *pObject =  new  Adapter_Object();
     pObject->Request();  //实际上调用的是Adaptee::SpecialRequest()
};
#endif


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值