hello head

适配器模式是一种创建型模式。如下:

 1 #include <iostream>
 2 using namespace std;
 3 class powerA{
 4 public:
 5     virtual void connect(){
 6         cout << "i am powerA working..." << endl;
 7     }   
 8 };
 9 
10 class powerB{
11 public:
12     void connect(){
13         cout << "i am powerB working..." << endl;
14     }   
15 };
16 
17 class Adaptor : public powerA{
18 public:
19     powerB pb; 
20     Adaptor(powerB &pob){
21         pb = pob;
22     }   
23     virtual void connect(){
24         pb.connect();
25     }   
26 };
27 
28 void testwork(powerA &A){
29     cout << "connect..." << endl;
30     A.connect();
31     cout << "finished." << endl;
32 }
33 
34 int main(){
35     powerA pa;
36     testwork(pa);
37 
38     powerB pb;
39     Adaptor adaptor(pb);
40     testwork(adaptor);
41 
42     return 0;
43 }

怎么有点像中介者模式呢?先提个问题,下篇看下两者之间的异同点。

posted on 2017-11-03 11:11  gardonkoo  阅读(82)  评论(0编辑  收藏  举报

hello foot