适配器模式(Adapter Pattern)
定义:Convert the interface of a class into another interface clients
expect.Adapter lets classes work together that couldn’t otherwise
because of incompatible
interfaces.(将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。)
组成:
- 目标(Target)角色:定义 Client 使用的接口。
- 被适配(Adaptee)角色:这个角色有一个已存在并使用了的接口,而这个接口是需要我们
适配的。 - 适配器(Adapter)角色:这个适配器模式的核心。它将被适配角色已有的接口转换为目标
角色希望的接口。
使用场景:
你有动机修改一个已经投产中的接口时,适配器模式可能是最适合你的模式。比如系统扩展了,