原版设计模式之适配器模式

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.(适配者能让无法用其他方式协同工作的不兼容接口协同工作)

Also Known As (别名)

Wrapper (包装)

Motivation (案例)

在这里插入图片描述
工具包toolkit中存在TextView类,用来展示文本,但是此时要获取文本的形状。
How can existing and unrelated classes like TextView work in an application that expects classes with a different and incompatible interface? (像TextView这样现有的,不相关的类,如何在不兼容的应用程序的接口中运行
We could change the TextView class so that it conforms to the Shape interface, but that isn’t an option unless we have the toolkit’s source code.(我们可以修改TextView类,使它能符合Shape接口,但是这是行不通的,因为我们没有toolkit的源码
Even if we did, it wouldn’t make sense to change TextView;(即使修改了TextView类的代码,这样也是没有意义的
the toolkit shouldn’t have to adopt domain-specific interfaces just to make one application work.(toolkit工具包不应该为了使某个应用程序能运行就适配特殊的接口
Instead, we could define TextShape so that it adapts the TextView interface to Shape’s. (相反,我们可以定义TextShape类使TextView类的界面和Shape界面适配
We can do this in one of two ways: (1) by inheriting Shape’s interface and TextView’s implementation or (我们可以有两种方式实现上述方案,第一种是TextShape继承Shape的界面,和TextView的实现【实现Shape接口,继承TextView类】
(2) by composing a TextView instance within a TextShape and implementing TextShape in terms of TextView’s interface. (第二种是在TextShape类中组合TextView实例,并根据TextView的界面实现TextShape
These two approaches correspond to the class and object versions of the Adapter pattern. We call TextShape an adapter.(这两种方法对应适配器模式的类版本和对象版本,而TextShape则被称为适配器
参考代码

Applicability (适用点)

  • 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.(【仅限对象适配器】你需要用一些已存的子类,但对每个子类再次子类化是不切实际的,此时使用对象适配器来适配他们的父类

Structure (结构)

类适配器

使用多继承方式(C++可以多继承)
在这里插入图片描述

对象适配器

在这里插入图片描述
Adapter类组合Adaptee。

Participants (参与者)

  • Target (Shape) (目标
    • defines the domain-specific interface that Client uses. (定义客户端使用的特殊方法
  • Client (DrawingEditor) (客户端
    • collaborates with objects conforming to the Target interface. (与符合Target方法的对象协作【使用Target的实例对象】
  • Adaptee (TextView) (被适配者
    • defines an existing interface that needs adapting.(已经存在的接口,并需要被适配
  • Adapter (TextShape) (适配器
    • adapts the interface of Adaptee to the Target interface.(将Adaptee的方法和Target接口适配

Collaborations (约定)

Clients call operations on an Adapter instance. In turn, the adapter calls Adaptee operations that carry out the request.(客户端调用适配器对象的操作,然后适配器调用被适配者的操作来执行请求

Related Patterns (相关模式)

Bridge (171) has a structure similar to an object adapter, but Bridge has a different intent: It is meant to separate an interface from its implementation so that they can be varied easily and independently. An adapter is meant to change the interface of an existing object.(桥接模式在结构上和适配器相似,但桥接模式的目的不同。桥接模式是为了从具体实现中分离出接口,以便具体实现和接口可以轻松独立的变化。而适配器是为了更改已经存在的对象的方法

Decorator (196) enhances another object without changing its interface. A decorator is thus more transparent to the application than an adapter is. As a consequence,Decorator supports recursive composition, which isn’t possible with pure adapters.(装饰者在不改变接口下增强一个对象。因此,对于应用程序来说,装饰者比适配器更透明。另外,装饰者还支持递归组合,这对于纯粹的适配器来说是不可能支持的。

Proxy (233) defines a representative or surrogate for another object and does not change its interface. (代理为目标定义代理或者代理对象,而不改变其接口。

posted @ 2022-05-13 11:21  伟衙内  阅读(14)  评论(0编辑  收藏  举报