知识卡片
适配器模式的定义与三个角色
适配器把源能力转换成客户期望的接口,使原本不匹配的角色协作。Target是使用契约,Adaptee提供源能力,Adapter完成映射,可采用继承或对象委托。
它可减少改动稳定两方的范围,但不保证永远只新增一类或零改动。字段含义、异常和生命周期仍须匹配,无法转换的语义不能靠包装解决。图按类适配源码request→doSomething,未运行。
结构图:
classDiagram
class Target {
<<interface>>
+request() void
}
class Adaptee {
+doSomething() void
}
class Adapter {
+request() void
}
Adaptee <|-- Adapter
Target <|.. Adapter
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 19.2 适配器模式的定义 · 查看原书依据
Convert the interface of a class into another interface clients expect.Adapter lets classes work together that couldn't otherwise
参考来源
- 位置:《设计模式之禅(第2版)》19.2 适配器模式的定义(来源项目book-sources,源文件:extracted/设计模式之禅(第2版)/_epub-src/text/part0086.html)
- 结论依据:原文定义、示例及取舍支撑本卡;已知过强表述在正文限定。
- 原始内容:Convert the interface of a class into another interface clients expect.Adapter lets classes work together that couldn't otherwise