桥接模式就是 独立和实现相分离。
不同的厂家生产不同的产品。。。。产品和厂家有这组合的关系。
上代码
- // Bridge.cpp : 定义控制台应用程序的入口点。
- //
- /
- //************************************************************************/
- /* @filename Bridge.cpp
- @author wallwind
- @createtime 2012/10/24 00:00
- @function 桥接模式
- @email wochenglin@qq.com
- */
- /************************************************************************/
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- class Product
- {
- public:
- Product(){}
- virtual ~Product(){}
- virtual void make()=0;
- virtual void sell()=0;
- };
- class ProductA:public Product
- {
- public:
- ProductA(){}
- virtual ~ProductA(){}
- virtual void make()
- {
- cout<<"ProductA:make()"<<endl;
- }
- virtual void sell()
- {
- cout<<"ProductA:sell()"<<endl;
- }
- };
- class ProductB:public Product
- {
- public:
- ProductB(){}
- virtual ~ProductB(){}
- virtual void make()
- {
- cout<<"ProductB:make()"<<endl;
- }
- virtual void sell()
- {
- cout<<"ProductB:sell()"<<endl;
- }
- };
- class Corp
- {
- public:
- Corp(Product* pro)
- :m_product(pro)
- {}
- virtual ~Corp()
- {
- delete m_product;
- }
- virtual void process()
- {
- m_product->make();
- m_product->sell();
- }
- private:
- Product *m_product;
- };
- class CorpA:public Corp
- {
- public:
- CorpA(Product * pro) :Corp(pro){}
- virtual ~CorpA(){}
- virtual void process()
- {
- cout<<"CorpA():process()"<<endl;
- Corp::process();
- }
- };
- class CorpB:public Corp
- {
- public:
- CorpB(Product * pro) :Corp(pro){}
- virtual ~CorpB(){}
- virtual void process()
- {
- cout<<"CorpB:process()"<<endl;
- Corp::process();
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- Product* product;
- product = new ProductA;
- Corp * corp ;
- corp = new CorpA(product);
- corp ->process();
- cout<<"----------"<<endl;
- product= new ProductB;
- corp = new CorpB(product);
- corp->process();
- return 0;
- }