码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式_适配器

时间:2015-02-19 01:23:20      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:设计模式

//类适配器
#include<memory>
#include<iostream>
using namespace std;
class Target {
  public:
    virtual void request(){
        cout << "Target::Request"<<endl;
    }
};
class Adaptee{
public:
   void SpecificRequest(){
        cout << "adaptee::SpecificRequest()"<<endl;
    }
};
class Adapter : public Target , Adaptee{
public:
    void request(){
        Adaptee::SpecificRequest ();
    }
};
int main(int argc, char *argv[])
{
    auto_ptr<Target> tarobj ( new Adapter() );//适配器就是引入中间类进行后台链接
    //Target *tarobj = new Adapter();
    tarobj->request ();
    return 0;
}
//对象适配器(比类适配器常用)
#include<iostream>
#include<memory>
using namespace std;
class Target
{
public:
    Target() {}
    virtual ~Target(){
        cout <<"delete target"<<endl;
    }
    virtual void Request(){
        cout << "Target request" << endl;
    }
};
class Adaptee
{
public:
    Adaptee() {}
    void SpecialRequest(){
        cout << "Adaptee::request" <<endl;
    }
};
class Adapter : public Target
{
public:
    Adapter()  : m_Adaptee(new Adaptee()){}
    ~Adapter(){
        if( m_Adaptee != NULL){
            delete m_Adaptee;
            cout << "delete Adaptee"<<endl;
            m_Adaptee = NULL;
        }
    }
    void Request (){
        m_Adaptee->SpecialRequest ();
    }
private:
    Adaptee *m_Adaptee;

};
int main(int argc, char *argv[])
{
    auto_ptr<Target> tarobj (new Adapter());
    tarobj->Request ();
    return 0;
}


设计模式_适配器

标签:设计模式

原文地址:http://blog.csdn.net/emb_hqj/article/details/43878957

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!