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來得到真正的接口繼承了。

相關文章
相關標籤/搜索