一、简介
1、适配器模式将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
2、在软件设计的初期要尽量将接口统一,及早发现不统一的接口进行重构,在双方都不太容易修改的时候再使用适配器模式进行适配。
3、UML
4、所属类别:结构型
二、C++程序
1 // 适配器模式.cpp : 定义控制台应用程序的入口点。
2 //
3
4 #include "stdafx.h"
5 #include<iostream>
6 using namespace std;
7
8 class Target//当前要用的普通接口
9 {
10 public:
11 Target(){}
12 virtual ~Target(){}
13 virtual void show()
14 {
15 cout<<"普通请求"<<endl;
16 }
17 };
18 class Adaptee//需要适配的类(需要将这里面的接口转换成Target类那样的接口)
19 {
20 public:
21 Adaptee(){}
22 ~Adaptee(){}
23 void Adaptee_show()
24 {
25 cout<<"特殊请求"<<endl;
26 }
27 };
28 class Adapter:public Target//适配器类
29 {
30 private:
31 Adaptee *adaptee;
32 public:
33 Adapter()
34 {
35 adaptee=new Adaptee();
36 }
37 virtual ~Adapter(){}
38 virtual void show()
39 {
40 adaptee->Adaptee_show();
41 }
42 };
43 int _tmain(int argc, _TCHAR* argv[])
44 {
45 Adapter *adapter=new Adapter();
46 adapter->show();
47 return 0;
48 }