小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C++設(shè)計模式-Adapter

 幸福的樂土 2012-09-15

定義

  適配器將一個類的接口轉(zhuǎn)換成客戶希望的另外一個接口,該模式使得原來由于接口不兼容而不能一起工作的那些類可以一起工作。

結(jié)構(gòu)

  類適配器包含兩種結(jié)構(gòu):

  1.使用多重繼承對一個接口與另一個接口進(jìn)行匹配:如下圖所示。


  2.依賴于對象組合,如下圖所示。


理解

  在這么幾種情況下可以使用類適配器模式:

  1.你想使用一個已經(jīng)存在的類,而它的接口不符合你的需求。

  2.你想創(chuàng)建一個可以復(fù)用的類,該類可以與其他不相關(guān)的類(那些接口可能不一定兼容的類)或不可預(yù)見的類協(xié)同工作。

  3.你想使用一些已經(jīng)存在的子類,但是不可能對每一個都進(jìn)行子類化以匹配它們的接口。對象適配器可以適配它的父類接口。(僅使用于對象適配器)

應(yīng)用

  1.ET++Draw通過使用一個TextShape適配器類的方式復(fù)用了ET++中的一些類,并將它們用于正文編輯。

  2.InterView2.6中也使用了諸如類適配器和對象適配器的概念。

代碼

  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. // Target  
  5. class stack  
  6. {  
  7. public:  
  8.     int pop(){}  
  9.     void posh(int ){}  
  10.     int get_size(){}  
  11.     bool is_empty(){}  
  12. private:  
  13.     //  
  14. };  
  15.   
  16. // Adaptee  
  17. class queue  
  18. {  
  19. public:  
  20.     int q_push(){}  
  21.     void q_pop(){}  
  22.     int front(){}  
  23.     int back(){}  
  24.     int get_size(){}  
  25.     bool is_empty(){}  
  26. private:  
  27.     //  
  28. };  
  29.   
  30. // Adapter  
  31. class q_stack : public stack, private queue  
  32. {  
  33. public:  
  34.     int pop()  
  35.     {  
  36.         // Actually, if we want queue acts as stack, we could use two queues to simulate a stack.  
  37.         queue::q_pop();  
  38.     }  
  39.     void push()  
  40.     {  
  41.         queue::q_push();  
  42.     }  
  43.     int get_size()  
  44.     {  
  45.         return queue::get_size();  
  46.     }  
  47.     bool is_empty()  
  48.     {  
  49.         return queue::is_empty();  
  50.     }  
  51. private:  
  52.     //  
  53. };  

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多