C++設計模式之適配器模式

來源:互聯網
上載者:User

標籤:設計   space   動作   控制台   argc   適配器   調用   方法   特點   

對象適配器有以下特點:

  1. 有的時候,你會發現,不是很容易去構造一個Adaptee類型的對象;
  2. 當Adaptee中添加新的抽象方法時,Adapter類不需要做任何調整,也能正確的進行動作;
  3. 可以使用多肽的方式在Adapter類中調用Adaptee類子類的方法。

代碼實現:

 1 // Adapter.cpp : 定義控制台應用程式的進入點。 2 // 3  4 #include "stdafx.h" 5 #include <iostream> 6 #include<string> 7 using namespace std; 8  9 10 class Target11 {12 public:13     Target(){}14     virtual ~Target(){}15     virtual void Request()16     {17         cout << "Target::Request" << endl;18     }19 };20 class Adaptee21 {22 public:23     void SpecificRequest()24     {25         cout << "Adaptee::SpecificRequest" << endl;26     }27 };28 29 class Adapter : public Target30 {31 public:32     Adapter() : m_Adaptee(new Adaptee) {}33     ~Adapter()34     {35         if (m_Adaptee != NULL)36         {37             delete m_Adaptee;38             m_Adaptee = NULL;39         }40     }41     void Request()42     {43         m_Adaptee->SpecificRequest();44     }45 46 private:47     Adaptee *m_Adaptee;48 };49 int _tmain(int argc, _TCHAR* argv[])50 {51     cout<<"適配器模式:"<<endl;52     Target *targetObj = new Adapter();53     targetObj->Request();54     delete targetObj;55     targetObj = NULL;56     system("pause");57     return 0;58 }

 

C++設計模式之適配器模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.