用代码和UML图化解设计模式之桥接模式的深入分析
投稿:jingxian
厂家生产不同的产品。。。。产品和厂家有这组合的关系。
上代码
// Bridge.cpp : 定义控制台应用程序的入口点。
/************************************************************************/
#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;
}
ok 今天就到这里。。。。继续学习