C++设计模式-Adapter适配器模式

Adapter适配器模式
做用:将一个类的接口转换成客户但愿的另一个接口。Adapter模式使得本来因为接口不兼容而不能一块儿工做的那些类能够一块儿工做。ios

分为类适配器模式和对象适配器模式。this

系统的数据和行为都正确,但接口不符时,咱们应该考虑使用适配器,目的是使控制范围以外的一个原有对象与某个接口匹配。适配器模式主要应用于但愿复用一些现存的类,可是接口又与复用环境要求不一致的状况。spa

想使用一个已经存在的类,但若是它的接口,也就是它的方法和你的要求不相同时,就应该考虑用适配器模式。code

好比购买的第三方开发组件,该组件接口与咱们本身系统的接口不相同,或者因为某种缘由没法直接调用该组件,能够考虑适配器。对象

UML图以下:blog

图1:类模式适配器继承

图2:对象模式适配器接口

代码以下:ci

Adapter.h开发

 1 #ifndef _ADAPTER_H_
 2 #define _ADAPTER_H_
 3 
 4 //目标接口类,客户须要的接口
 5 class Target
 6 {
 7 public:
 8     Target();
 9     virtual ~Target();
10     virtual void Request();//定义标准接口
11 };
12 
13 //须要适配的类
14 class Adaptee
15 {
16 public:
17     Adaptee();
18     ~Adaptee();
19     void SpecificRequest();
20 };
21 
22 //类模式,适配器类,经过public继承得到接口继承的效果,经过private继承得到实现继承的效果
23 class Adapter:public Target,private Adaptee
24 {
25 public:
26     Adapter();
27     ~Adapter();
28     virtual void Request();//实现Target定义的Request接口
29 };
30 
31 //对象模式,适配器类,继承Target类,采用组合的方式实现Adaptee的复用
32 class Adapter1:public Target
33 {
34 public:
35     Adapter1(Adaptee* adaptee);
36     Adapter1();
37     ~Adapter1();
38     virtual void Request();//实现Target定义的Request接口
39 private:
40     Adaptee* _adaptee;
41 };
42 #endif

Adapter.cpp

 1 #include "Adapter.h"
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 Target::Target()
 7 {}
 8 
 9 Target::~Target()
10 {}
11 
12 void Target::Request()
13 {
14     cout << "Target::Request()" << endl;
15 }
16 
17 Adaptee::Adaptee()
18 {
19 }
20 
21 Adaptee::~Adaptee()
22 {
23 }
24 
25 void Adaptee::SpecificRequest()
26 {
27     cout << "Adaptee::SpecificRequest()" << endl;
28 }
29 
30 //类模式的Adapter
31 Adapter::Adapter()
32 {
33 }
34 
35 Adapter::~Adapter()
36 {
37 }
38 
39 void Adapter::Request()
40 {
41     cout << "Adapter::Request()" << endl;
42     this->SpecificRequest();
43     cout << "----------------------------" <<endl;
44 }
45 
46 //对象模式的Adapter
47 Adapter1::Adapter1():_adaptee(new Adaptee)
48 {
49 }
50 
51 Adapter1::Adapter1(Adaptee* _adaptee)
52 {
53     this->_adaptee = _adaptee;
54 }
55 
56 Adapter1::~Adapter1()
57 {
58 }
59 
60 void Adapter1::Request()
61 {
62     cout << "Adapter1::Request()" << endl;
63     this->_adaptee->SpecificRequest();
64     cout << "----------------------------" <<endl;
65 }

main.cpp

 1 #include "Adapter.h"
 2 
 3 int main()
 4 {
 5     //类模式Adapter
 6     Target* pTarget = new Adapter();
 7     pTarget->Request();
 8 
 9     //对象模式Adapter1
10     Adaptee* ade = new Adaptee();
11     Target* pTarget1= new Adapter1(ade);
12     pTarget1->Request();
13 
14     //对象模式Adapter2
15     Target* pTarget2 = new Adapter1();
16     pTarget2->Request();
17 
18     return 0;
19 }

在Adapter模式的两种模式中,有一个很重要的概念就是接口继承和实现继承的区别和联系。接口继承和实现继承是面向对象领域的两个重要的概念,接口继承指的是经过继承,子类得到了父类的接口,而实现继承指的是经过继承子类得到了父类的实现(并不统共接口)。在C++中的public继承既是接口继承又是实现继承,由于子类在继承了父类后既能够对外提供父类中的接口操做,又能够得到父类的接口实现。固然咱们能够经过必定的方式和技术模拟单独的接口继承和实现继承,例如咱们能够经过private继承得到实现继承的效果(private继承后,父类中的接口都变为private,固然只能是实现继承了。),经过纯抽象基类模拟接口继承的效果,可是在C++中pure virtual function也能够提供默认实现,所以这是不纯正的接口继承,可是在Java中咱们能够interface来得到真正的接口继承了。

相关文章
相关标签/搜索