Java设计模式(9) —— 适配器

Adapter

 

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.
You want to use an existing class, and its interface does not match the one you need.

How to
Object Adapter
Class Adapter

Target
defines the domain-specific interface that Client uses.
Client
collaborates with objects conforming to the Target interface.
Adaptee
defines an existing interface that needs adapting.
Adapter
adapts the interface of Adaptee to the Target interface.

Known cases
Use third-party lib, but it doesn't satisfy the interface clients expect.

UML

对象适配器

类适配器

 

代码:

对象适配器

类适配器

 

区别:

(1)在Java中不允许多重继承,所以有些情况下必须得用“对象适配器”;比如:一个Adapter要实现一个抽象类的接口,而Adaptee也是一个类,这只能用 “对象适配器”。

(2) 对象适配器比 类适配器更灵活,可以为 对象适配器配置不同子类的Adaptee

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值