关闭

Design pattern learning II: Adapter pattern

843人阅读 评论(0) 收藏 举报

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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:28049次
    • 积分:423
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:1篇
    • 译文:1篇
    • 评论:8条
    最新评论