【7】设计模式之桥模式(Bridge)

转载自:http://blog.csdn.net/rexuefengye/article/details/12834631

1、描述

 今天我要说说我自己,梦想中的我自己,我身价过亿,有两个大公司,一个是房地产公司,一个是服装制造业,这两个公司都很赚钱,天天帮我在累加财富,其实是什么公司我倒是不关心,我关心的是是不是在赚钱,赚 了多少,这才是我关心的,我是商人呀,唯利是图是我的本性,偷税漏税是我的方法,欺上瞒下、压榨员工血汗 我是的手段嘛,呵呵,同时我公司也 会发展,终于在有一天我觉得赚钱速度太慢,于是我上下疏通,左右打关系,终于开辟了一条赚钱的康庄大道:生产山寨产品,什么产品呢?就是市场上什么牌子的东西火爆我生产什么牌子的东西,甭管是打火机还是电脑,只要它火爆,我就生产,赚过了高峰期就换个产品,打一枪换一个牌子,不承担售后成本、也不担心销路问题,
我只有正品的十分之一的价格,你买不买?哈哈,赚钱呀! 
      那么,我的服装厂就开始变成山寨 iPod 生产车间,然后就看我的财富在积累积累,你想呀山寨的东西不需要特别的销售渠道(正品到哪里我就到哪里),不需要维修成本(大不了给你换个,你还想咋地,过了高峰期我就改头换面了你找谁维修去,投诉?投诉谁呢?),不承担广告成本(正品在打广告,我还需要吗?需要吗?),但是我也有犯愁的时候,我这是个山寨工厂,要及时的生产出市场上流行产品,转型要快,要灵活,今天从生产 iPod 转为生产 MP4,明天再转为生产上网本,这个都需要灵活的变化,不要限制的太死,那问题来了,每次我的厂房,我的工人,我的设备都在,不可能每次我换个山寨产品我的厂子就彻底不要了,这不行,成本忒高了点,那怎么办?请看类图:


CNewCorp 类和 IProduct 类建立一个关联关系,可以彻底解决我以后山寨公司生产产品的问题了。


注释:

      main(),客户

      IProduct,产品接口

      CHouse,房子

      CIPod,ipod

      CClothes,服装

      CNewCorp,桥梁类,MakeMoney()是桥梁方法

      CNewHouseCorp,只能生产房子,所以构造函数是CHouse*

      CShanZhaiCorp,什么赚钱就生产什么,所以构造函数是IProduct*

      说明:客户直接使用CNewHouseCorp和CShanZhaiCorp类,在main()函数里构造产品,然后传到这两个类里。这两个类的MakeMoney()函数,先调用基类的MakeMoney(),然后分别执行各自的逻辑。

      注意:CNewCorp起到了桥梁的作用。可以分别增加产品和公司。


Product-IProduct
//IProduct.h
#ifndef Bridge_IProduct_h
#define Bridge_IProduct_h

#include <iostream>
class IProduct
{
public:
    IProduct(void){}
    virtual ~IProduct(void){}
    virtual void BeProducted() = 0;
    virtual void BeSelled() = 0;
    
};
#endif
Product-House
//House.h
#ifndef __Bridge__House__
#define __Bridge__House__

#include <iostream>
#include "IProduct.h"

class CHouse : public IProduct
{
public:
    CHouse();
    ~CHouse();
    void BeProducted();
    void BeSelled();
};

#endif /* defined(__Bridge__House__) */

//House.cpp
#include "House.h"
using std::cout;
using std::endl;
CHouse::CHouse()
{
    
}
CHouse::~CHouse()
{
    
}

void CHouse::BeProducted()
{
    cout << "正在生产房子" << endl;
}
void CHouse::BeSelled()
{
    cout << "买房子啦" << endl;
}
Product-IPod
//IPod.h
#ifndef __Bridge__IPod__
#define __Bridge__IPod__

#include <iostream>

#include "IProduct.h"
class CIPod :
public IProduct
{
public:
    CIPod(void);
    ~CIPod(void);
    void BeProducted();
    void BeSelled();
};

#endif /* defined(__Bridge__IPod__) */

//IPod.cpp
#include "IPod.h"

using std::cout;
using std::endl;
CIPod::CIPod(void)
{
}
CIPod::~CIPod(void)
{
}
void CIPod::BeProducted()
{
    cout << "正在生产IPod" << endl;
}
void CIPod::BeSelled()
{
    cout << "卖ipod啦" << endl;
}
Product-Clothes
//Clothes.h
#ifndef __Bridge__Clothes__
#define __Bridge__Clothes__

#include <iostream>
#include "IProduct.h"

class CClothes :
public IProduct
{
public:
    CClothes(void);
    ~CClothes(void);
    void BeProducted();
    void BeSelled();
};
#endif /* defined(__Bridge__Clothes__) */

//Clothes.cpp
#include "Clothes.h"
#include "Clothes.h"
using std::cout;
using std::endl;
CClothes::CClothes(void)
{
}
CClothes::~CClothes(void)
{
}
void CClothes::BeProducted()
{
    cout << "正在生产衣服" << endl;
}
void CClothes::BeSelled()
{
    cout << "卖衣服啦" << endl;
}
Bridge-NewCorp
//NewCorp.h
#ifndef __Bridge__NewCorp__
#define __Bridge__NewCorp__

#include <iostream>
#include "IProduct.h"
class CNewCorp
{
public:
    CNewCorp(IProduct* pproduct);
    virtual ~CNewCorp();
    void MakeMoney();
private:
    IProduct* m_pProduct;
};

#endif /* defined(__Bridge__NewCorp__) */


//NewCorp.cpp
#include "NewCorp.h"
CNewCorp::CNewCorp(IProduct* pproduct)
{
    m_pProduct = pproduct;
}
CNewCorp::~CNewCorp()
{
    
}
void CNewCorp::MakeMoney()
{
    m_pProduct->BeProducted();
    m_pProduct->BeSelled();
}
Bridge-NewHouseCorp
//NewHouseCorp.h
#ifndef __Bridge__NewHouseCorp__
#define __Bridge__NewHouseCorp__

#include <iostream>
#include "NewCorp.h"
#include "House.h"

class CNewHouseCorp : public CNewCorp
{
public:
    CNewHouseCorp(CHouse* pHouse);
    ~CNewHouseCorp();
    void MakeMoney();
};

#endif /* defined(__Bridge__NewHouseCorp__) */

//NewHouseCorp.cpp
#include "NewHouseCorp.h"
using std::cout;
using std::endl;

CNewHouseCorp::CNewHouseCorp(CHouse* pHouse) : CNewCorp(pHouse)
{
    
}
CNewHouseCorp::~CNewHouseCorp()
{
    
}
void CNewHouseCorp::MakeMoney()
{
    CNewCorp::MakeMoney();
    cout << "房子卖完了,发财咯" << endl;
}
Bridge-ShanZhaiCorp
//ShanZhaiCorp.h
#ifndef __Bridge__ShanZhaiCorp__
#define __Bridge__ShanZhaiCorp__

#include <iostream>
#include "NewCorp.h"
#include "IProduct.h"

class CShanZhaiCorp : public CNewCorp
{
public:
    CShanZhaiCorp(IProduct* pproduct);
    ~CShanZhaiCorp();
    void MakeMoney();
};

#endif /* defined(__Bridge__ShanZhaiCorp__) */

//ShanZhaiCorp.cpp
#include "ShanZhaiCorp.h"
#include <iostream>
using std::cout;
using std::endl;

CShanZhaiCorp::CShanZhaiCorp(IProduct* pproduct) : CNewCorp(pproduct)
{
    
}
CShanZhaiCorp::~CShanZhaiCorp()
{
    
}
void CShanZhaiCorp::MakeMoney()
{
    CNewCorp::MakeMoney();
    cout << "IPod卖完了,发财啦" << endl;
}
Bridge-ClothesCorp
//ClothesCorp.h
#ifndef __Bridge__ClothesCorp__
#define __Bridge__ClothesCorp__

#include <iostream>
#include "NewCorp.h"
#include "IProduct.h"

class CClothesCorp : public CNewCorp
{
public:
    CClothesCorp(IProduct* pproduct);
    ~CClothesCorp();
    void MakeMoney();
};

#endif /* defined(__Bridge__ClothesCorp__) */

//ClothesCorp.cpp
#include "ClothesCorp.h"
#include <iostream>
using std::cout;
using std::endl;
CClothesCorp::CClothesCorp(IProduct* pproduct) : CNewCorp(pproduct)
{
    
}
CClothesCorp::~CClothesCorp()
{
    
}
void CClothesCorp::MakeMoney()
{
    CNewCorp::MakeMoney();
    cout << "衣服卖完了,发财啦额" << endl;
}

main.cpp
#include <iostream>
#include "NewCorp.h"
#include "NewHouseCorp.h"
#include "ShanZhaiCorp.h"
#include "ClothesCorp.h"

#include "Clothes.h"
#include "IPod.h"
#include "House.h"
using std::cout;
using std::endl;

void DoNewRun1()
{
    cout << "我是房地产大亨" << endl;
    CHouse house;
    CNewHouseCorp newHouseCorp(&house);
    newHouseCorp.MakeMoney();
    cout << endl;
    
    cout << "我是山寨达人" << endl;
    CIPod iPod;
    CShanZhaiCorp shanZhaiCorp(&iPod);
    shanZhaiCorp.MakeMoney();
    cout << endl;
    
    cout << "我是衣服大使" << endl;
    CClothes cClothes;
    CClothesCorp newClothesCorp(&cClothes);
    newClothesCorp.MakeMoney();
    cout << endl;
}


int main(int argc, const char * argv[]) {
    // insert code here...
    
    DoNewRun1();
    
    std::cout << "Hello, World!\n";
    return 0;
}

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值