今天我要说说我自己,梦想中的我自己,我身价过亿,有两个大公司,一个是房地产公司,一个是服装制造业,这两个公司都很赚钱,天天帮我在累加财富,其实是什么公司我倒是不关心,我关心的是是不是在赚钱,赚 了多少,这才是我关心的,我是商人呀,唯利是图是我的本性,偷税漏税是我的方法,欺上瞒下、压榨员工血汗 我是的手段嘛,呵呵,同时我公司也 会发展,终于在有一天我觉得赚钱速度太慢,于是我上下疏通,左右打关系,终于开辟了一条赚钱的康庄大道:生产山寨产品,什么产品呢?就是市场上什么牌子的东西火爆我生产什么牌子的东西,甭管是打火机还是电脑,只要它火爆,我就生产,赚过了高峰期就换个产品,打一枪换一个牌子,不承担售后成本、也不担心销路问题,
我只有正品的十分之一的价格,你买不买?哈哈,赚钱呀!
那么,我的服装厂就开始变成山寨 iPod 生产车间,然后就看我的财富在积累积累,你想呀山寨的东西不需要特别的销售渠道(正品到哪里我就到哪里),不需要维修成本(大不了给你换个,你还想咋地,过了高峰期我就改头换面了你找谁维修去,投诉?投诉谁呢?),不承担广告成本(正品在打广告,我还需要吗?需要吗?),但是我也有犯愁的时候,我这是个山寨工厂,要及时的生产出市场上流行产品,转型要快,要灵活,今天从生产 iPod 转为生产 MP4,明天再转为生产上网本,这个都需要灵活的变化,不要限制的太死,那问题来了,每次我的厂房,我的工人,我的设备都在,不可能每次我换个山寨产品我的厂子就彻底不要了,这不行,成本忒高了点,那怎么办?请看类图:
CNewCorp 类和 IProduct 类建立一个关联关系,可以彻底解决我以后山寨公司生产产品的问题了。
注释:
main(),客户
IProduct,产品接口
CHouse,房子
CIPod,ipod
CClothes,服装
CNewCorp,桥梁类,MakeMoney()是桥梁方法
CNewHouseCorp,只能生产房子,所以构造函数是CHouse*
CShanZhaiCorp,什么赚钱就生产什么,所以构造函数是IProduct*
说明:客户直接使用CNewHouseCorp和CShanZhaiCorp类,在main()函数里构造产品,然后传到这两个类里。这两个类的MakeMoney()函数,先调用基类的MakeMoney(),然后分别执行各自的逻辑。
注意:CNewCorp起到了桥梁的作用。可以分别增加产品和公司。
//IProduct.h
#ifndef Bridge_IProduct_h
#define Bridge_IProduct_h
#include
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
#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;
}
//IPod.h
#ifndef __Bridge__IPod__
#define __Bridge__IPod__
#include
#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;
}
//Clothes.h
#ifndef __Bridge__Clothes__
#define __Bridge__Clothes__
#include
#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;
}
//NewCorp.h
#ifndef __Bridge__NewCorp__
#define __Bridge__NewCorp__
#include
#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
#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;
}
//ShanZhaiCorp.h
#ifndef __Bridge__ShanZhaiCorp__
#define __Bridge__ShanZhaiCorp__
#include
#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
using std::cout;
using std::endl;
CShanZhaiCorp::CShanZhaiCorp(IProduct* pproduct) : CNewCorp(pproduct)
{
}
CShanZhaiCorp::~CShanZhaiCorp()
{
}
void CShanZhaiCorp::MakeMoney()
{
CNewCorp::MakeMoney();
cout << "IPod卖完了,发财啦" << endl;
}
//ClothesCorp.h
#ifndef __Bridge__ClothesCorp__
#define __Bridge__ClothesCorp__
#include
#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
using std::cout;
using std::endl;
CClothesCorp::CClothesCorp(IProduct* pproduct) : CNewCorp(pproduct)
{
}
CClothesCorp::~CClothesCorp()
{
}
void CClothesCorp::MakeMoney()
{
CNewCorp::MakeMoney();
cout << "衣服卖完了,发财啦额" << endl;
}
#include
#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;
}