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

桥接模式

时间:2015-05-27 22:47:54      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

1】什么是桥接模式?

【2】桥接模式的代码示例

示例代码:

 

#include <iostream>
#include <string>
using namespace std;

class HandsetSoft
{
public:
    virtual void run() = 0;
};

class HandsetGame : public HandsetSoft
{
public:
    void run()
    {
        cout << "运行手机游戏" << endl;
    }
};

class HandsetAddressList : public HandsetSoft
{
public:
    void run()
    {
        cout << "运行手机通讯录" << endl;
    }
};

class HandsetBrand
{
protected:
    HandsetSoft *soft;
public:
    void setHandsetSoft(HandsetSoft *soft)
    {
        this->soft = soft;
    }
    virtual void run() = 0;
};

class HandsetBrandN : public HandsetBrand
{
public:
    void run()
    {
        soft->run();
    }
};

class HandsetBrandM : public HandsetBrand
{
public:
    void run()
    {
        soft->run();
    }
};

int main()
{
    HandsetBrand *hb;
    hb = new HandsetBrandM();
    
    hb->setHandsetSoft(new HandsetGame());
    hb->run();
    hb->setHandsetSoft(new HandsetAddressList());
    hb->run();

    return 0;
}

 

桥接模式

标签:

原文地址:http://www.cnblogs.com/leijiangtao/p/4534558.html

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