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

  • 格式:pdf
  • 大小:77.32 KB
  • 文档页数:3

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

Adapter适配器模式

作⽤:将⼀个类的接⼝转换成客户希望的另外⼀个接⼝。Adapter模式使得原本由于接⼝不兼容⽽不能⼀起⼯作的那些类可以⼀起⼯作。

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

系统的数据和⾏为都正确,但接⼝不符时,我们应该考虑使⽤适配器,⽬的是使控制范围之外的⼀个原有对象与某个接⼝匹配。适配器模式

主要应⽤于希望复⽤⼀些现存的类,但是接⼝⼜与复⽤环境要求不⼀致的情况。

想使⽤⼀个已经存在的类,但如果它的接⼝,也就是它的⽅法和你的要求不相同时,就应该考虑⽤适配器模式。

⽐如购买的第三⽅开发组件,该组件接⼝与我们⾃⼰系统的接⼝不相同,或者由于某种原因⽆法直接调⽤该组件,可以考虑适配器。

UML图如下:

图1:类模式适配器

图2:对象模式适配器

代码如下:

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

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 << "----------------------------" <

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 << "----------------------------" <

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来获得真正的接⼝继承了。