首页 > 其他 > 详细

桥接模式

时间:2015-05-27 22:46:13      阅读:188      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!