Design pattern learning II: Adapter pattern

GoF says:
Convert the interface of a class into another interface that the clients expect. Adapter lets classes work together that could not otherwise because of incompatible interfaces.

Basicly, this is saying that wo need a way to create a new interface for an object that dose the right stuff but has the wrong interface.

Intent: Match the existing object beyond your control to a particular interface;
Solution: The Adapter provides a wrapper with the desired interface;
Consequence: The Adapter pattern allows for preexisting objects to fit into new class structures without being limited by their interfaces;
Implementation: Contain the existing class in another class. Have the containing class match the required interface and call the methods of the contained class.

Two types of Adapter patterns:
Object Adapter pattern - The Adapter pattern relies on one object containning another
Class Adapter pattern - Multiple inheritance

The difference between Facade pattern and Adapter pattern:
A facade pattern simplifies an interface while adapter pattern convert the interface into a preexisting interface.

 

//-----------------------------------------------华丽的分割线-------------------------------------------------

 

GOF说:Adapter把类的接口转换成用户想要的接口。Adapter让有着不一致的接口的类共同工作。

如果一个对象做了正确的事但是有着错误的接口,那么我们需要创建一个新的接口。

目的:让非你控制的已存在的对象匹配特殊的接口
解决方案:Adapter提供了有着你想要的接口的包装
结果:Adapter模式允许已存在的对象适应新的类结构而不用受限于它们的原有接口
实现方法:在有着符合接口的另一个类中包含已存在的类,并调用它的方法

两种实现方式:
基于对象:让一个对象包含另一个对象
基于类:使用多继承类

Facade和Adapter的不同点:
Facade简化接口,而Adapter改变已存在的接口。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值