我所理解的设计模式(C++实现)——适配器模式(Adapter Pattern)

http://blog.csdn.net/lcl_data/article/details/8780140


解决的问题:

适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本接口不匹配而无法在一起工作的两个类能够在一起工作。比如说我的hp笔记本,美国产品,人家美国的电压是110V的,而我们中国的电压是220V,要在中国能使用,必须找个变压器转一下电压才可以。这个变压器就是个适配器。

适配器模式有类适配器和对象适配器两种模式,我们将分别讨论。


类适配器:

由图中可以看出,Adaptee类没有Request方法,而客户期待这个方法。为了使客户能够使用Adaptee类,提供一个中间环节,即类Adapter类,Adapter类实现了Target接口,并继承自AdapteeAdapter类的Request方法重新封装了AdapteeSpecificRequest方法,实现了适配的目的。

因为AdapterAdaptee是继承的关系,所以这决定了这个适配器模式是类的。

该适配器模式所涉及的角色包括:

目标(Target)角色:这是客户所期待的接口。因为C#不支持多继承,所以Target必须是接口,不可以是类。
源(Adaptee)角色:需要适配的类。
适配器(Adapter)角色:把源接口转换成目标接口。这一角色必须是类

    简单实现:

[cpp]  view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. // "ITarget"  
  5. class Target  
  6. {  
  7. public:  
  8.     // Methods  
  9.     virtual void Request(){};  
  10. };  
  11.   
  12. // "Adaptee"  
  13. class Adaptee  
  14. {  
  15. public:  
  16.     // Methods  
  17.     void SpecificRequest()  
  18.     {  
  19.         cout<<"Called SpecificRequest()"<<endl;  
  20.     }  
  21. };  
  22.   
  23. // "Adapter"  
  24. class Adapter : public Adaptee, public Target  
  25. {  
  26. public:  
  27.     // Implements ITarget interface  
  28.     void Request()  
  29.     {  
  30.         // Possibly do some data manipulation  
  31.         // and then call SpecificRequest    
  32.         this->SpecificRequest();  
  33.     }  
  34. };  
  35.   
  36.   
  37. int main()  
  38. {  
  39.     // Create adapter and place a request  
  40.     Target *t = new Adapter();  
  41.     t->Request();  
  42.   
  43.     return 0;  
  44. }  

对象适配器:

从图中可以看出:客户端需要调用Request方法,而Adaptee没有该方法,为了使客户端能够使用Adaptee类,需要提供一个包装(Wrapper)类Adapter。这个包装类包装了一个Adaptee的实例,从而将客户端与Adaptee衔接起来。由于AdapterAdaptee是委派关系,这决定了这个适配器模式是对象的。

该适配器模式所涉及的角色包括:

目标(Target)角色:这是客户所期待的接口。目标可以是具体的或抽象的类,也可以是接口。
源(Adaptee)角色:需要适配的类。
适配器(Adapter)角色:通过在内部包装(Wrap)一个Adaptee对象,把源接口转换成目标接口。

简单实现:

[cpp]  view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. // "ITarget"  
  5. class Target  
  6. {  
  7. public:  
  8.     // Methods  
  9.     virtual void Request(){};  
  10. };  
  11.   
  12. // "Adaptee"  
  13. class Adaptee  
  14. {  
  15. public:  
  16.     // Methods  
  17.     void SpecificRequest()  
  18.     {  
  19.         cout<<"Called SpecificRequest()"<<endl;  
  20.     }  
  21. };  
  22.   
  23. // "Adapter"  
  24. class Adapter : public Target  
  25. {  
  26. private:  
  27.     Adaptee *adaptee;  
  28.   
  29. public:  
  30.     Adapter()  
  31.     {  
  32.         adaptee = new Adaptee();  
  33.     }  
  34.   
  35.     // Implements ITarget interface  
  36.     void Request()  
  37.     {  
  38.         // Possibly do some data manipulation  
  39.         // and then call SpecificRequest    
  40.         adaptee->SpecificRequest();  
  41.     }  
  42. };  
  43.   
  44.   
  45. int main()  
  46. {  
  47.     // Create adapter and place a request  
  48.     Target *t = new Adapter();  
  49.     t->Request();  
  50.   
  51.     return 0;  
  52. }  

缺省适配器:

缺省适配器模式是一种特殊的适配器模式,但这个适配器是由一个抽象类实现的,并且在抽象类中要实现目标接口中所规定的所有方法,但很多方法的实现都是平庸的实现,也就是说,这些方法都是空方法。而具体的子类都要继承此抽象类。 

简单实现:

[cpp]  view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4.   
  5. class Target {   
  6. public:  
  7.     virtual void f1(){};   
  8.     virtual void f2(){};   
  9.     virtual void f3(){};     
  10. };  
  11.   
  12. class DefaultAdapter : public Target   
  13. {   
  14. public:  
  15.     void f1() {   
  16.     }   
  17.   
  18.     void f2() {   
  19.     }   
  20.   
  21.     void f3() {   
  22.     }   
  23. };  
  24.   
  25. class MyInteresting :public DefaultAdapter  
  26. {   
  27. public:  
  28.      void f3(){         
  29.         cout<<"呵呵,我就对f3()方法感兴趣,别的不管了!"<<endl;  
  30.     }   
  31. };  
  32.   
  33. int main()  
  34. {  
  35.     // Create adapter and place a request  
  36.     Target *t = new MyInteresting();  
  37.     t->f3();  
  38.   
  39.     return 0;  
  40. }  


实现要点:

1.Adapter模式主要应用于希望复用一些现存的类,但是接口又与复用环境要求不一致的情况,在遗留代码复用、类库迁移等方面非常有用

2Adapter模式有对象适配器和类适配器两种形式的实现结构,但是类适配器采用多继承的实现方式,带来了不良的高耦合,所以一般不推荐使用。对象适配器采用对象组合的方式,更符合松耦合精神。

3Adapter模式的实现可以非常的灵活,不必拘泥于GOF23中定义的两种结构。例如,完全可以将Adapter模式中的现存对象作为新的接口方法参数,来达到适配的目的。

4Adapter模式本身要求我们尽可能地使用面向接口的编程风格,这样才能在后期很方便的适配


使用场景:

在以下各种情况下使用适配器模式:

1.系统需要使用现有的类,而此类的接口不符合系统的需要。

2.想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作。这些源类不一定有很复杂的接口。

3.(对对象适配器而言)在设计里,需要改变多个已有子类的接口,如果使用类的适配器模式,就要针对每一个子类做一个适配器,而这不太实际。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
适配器模式Adapter Pattern)是一种结构型设计模式,它允许将一个类的接口转换成客户端所期望的另一个接口。它通常用于解决两个已有接口之间不兼容的问题。 在给出的代码示例中,我们可以看到适配器模式的应用。在Main.cpp文件中,创建了一个Target对象指针target,并将其初始化为Adapter对象。然后调用target的request()函数,实际上调用的是Adapter的request()函数。而Adapter的request()函数内部调用了Adaptee的specificRequest()函数,完成了适配的过程。 在Head.h文件中定义了三个类:Target、Adaptee和Adapter。Target类是适配器模式中的目标接口,定义了一个虚函数request()。Adaptee类是被适配的类,它有一个特殊的请求函数specificRequest()。Adapter类是适配器类,它继承了Target类,并在其request()函数中调用了Adaptee类的specificRequest()函数。 通过适配器模式,我们可以将不兼容的两个接口进行适配,使它们能够协同工作。这在软件开发中经常用于复用已有代码或集成多个系统。 参考: C++设计模式适配器模式Adapter Head.cpp Main.cpp<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++设计模式适配器模式(Adapter)](https://download.csdn.net/download/weixin_38666785/12761879)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C++设计模式-适配器模式](https://blog.csdn.net/qq78442761/article/details/95766831)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值