/*****************************************
Copyright (c) 2016 Jingshuang Hu
@filename:Target.h
@datetime:2016.09.15
@author:HJS
@e-mail:[email protected]
@blog:http://blog.csdn.net/hujingshuang
*****************************************/
#ifndef _TARGET_H
#define _TARGET_H
// 目標接口類
class Target {
public:
virtual void request(); // 目標接口
};
#endif // _TARGET_H
/*****************************************
Copyright (c) 2016 Jingshuang Hu
@filename:Target.cpp
@datetime:2016.09.15
@author:HJS
@e-mail:[email protected]
@blog:http://blog.csdn.net/hujingshuang
*****************************************/
#include "Target.h"
#include <iostream>
using namespace std;
void Target::request() {
cout << "目標接口" << endl;
}
/*****************************************
Copyright (c) 2016 Jingshuang Hu
@filename:Adaptee.h
@datetime:2016.09.15
@author:HJS
@e-mail:[email protected]
@blog:http://blog.csdn.net/hujingshuang
*****************************************/
#ifndef _ADAPTEE_H
#define _ADAPTEE_H
// 需要適配的類,其接口與目標接口不符
class Adaptee {
public:
virtual void specificRequest(); // 原有接口
};
// 需要適配的類A
class concreteAdapteeA : public Adaptee {
public:
virtual void specificRequest(); // 原有接口
};
// 需要適配的類B
class concreteAdapteeB : public Adaptee {
public:
virtual void specificRequest(); // 原有接口
};
#endif // _ADAPTEE_H
/*****************************************
Copyright (c) 2016 Jingshuang Hu
@filename:Adaptee.cpp
@datetime:2016.09.15
@author:HJS
@e-mail:[email protected]
@blog:http://blog.csdn.net/hujingshuang
*****************************************/
#include "Adaptee.h"
#include <iostream>
using namespace std;
void Adaptee::specificRequest() {
cout << "需要適配的接口" << endl;
}
void concreteAdapteeA::specificRequest() {
cout << "需要適配的接口A" << endl;
}
void concreteAdapteeB::specificRequest() {
cout << "需要適配的接口B" << endl;
}
/*****************************************
Copyright (c) 2016 Jingshuang Hu
@filename:Adapter.h
@datetime:2016.09.15
@author:HJS
@e-mail:[email protected]
@blog:http://blog.csdn.net/hujingshuang
*****************************************/
#ifndef _ADAPTER_H
#define _ADAPTER_H
#include "Target.h"
#include "Adaptee.h"
// 類適配器(通過這種多繼承的方式,將兩個類進行適配)
class classAdapter : public Target, private Adaptee {
public:
void request();
};
// 對象適配器
class objectAdapter : public Target {
private:
Adaptee* adaptee;
public:
objectAdapter(Adaptee* adaptee);
void request();
};
#endif // _ADAPTER_H
/*****************************************
Copyright (c) 2016 Jingshuang Hu
@filename:Adapter.cpp
@datetime:2016.09.15
@author:HJS
@e-mail:[email protected]
@blog:http://blog.csdn.net/hujingshuang
*****************************************/
#include "Adapter.h"
#include <iostream>
using namespace std;
// 類適配器
void classAdapter::request() {
cout << "類適配器" << endl;
this->specificRequest();
}
// 對象適配器
objectAdapter::objectAdapter(Adaptee* adaptee) {
this->adaptee = adaptee;
}
void objectAdapter::request() {
cout << "對象適配器" << endl;
adaptee->specificRequest();
}
/*****************************************
Copyright (c) 2016 Jingshuang Hu
@filename:main.cpp
@datetime:2016.09.15
@author:HJS
@e-mail:[email protected]
@blog:http://blog.csdn.net/hujingshuang
*****************************************/
#include <iostream>
#include "Target.h"
#include "Adapter.h"
#include "Adaptee.h"
using namespace std;
// 1 Target:目標接口,客戶所期待的的接口;
// 2 Adaptee:需要適配的類;
// 3 Adapter:適配器,包裝一個需要適配的類或對象,將其原接口轉換成目標接口。
int main() {
// 類適配器
Target* target1 = new classAdapter(); // 可看做與生俱來就適配好了
target1->request(); // 目標接口
// 對象適配器
Adaptee* adaptee1 = new concreteAdapteeA(); // 需要適配的對象A
Target* target2 = new objectAdapter(adaptee1); // 該對象經適配器,將原有接口轉化成目標接口
target2->request(); // 轉換後的目標接口
Adaptee* adaptee2 = new concreteAdapteeB(); // 同上
Target* target3 = new objectAdapter(adaptee2);
target3->request();
system("pause");
return 0;
}