桥梁模式的模型:
火车 产品A
商人 赚钱 桥梁 产品B 产品属性
卡车 产品C
Train Product_A
Businessman MakeMoney Bridge Product_B IProduct
Truck Product_C
说明:使用桥梁模式来实现一下内容:
一个商人做三种产品的生意,他有以下集中方式来运作:
用火车来运输A产品赚钱,需要经过桥梁Bridge
用卡车来运输A产品或者B产品或者C产品来赚钱,也需要经过桥梁Bridge
分析:A产品、B产品、C产品都具有产品属性IProduct
桥梁Bridge可以通过产品属性为IProduct的产品
火车与桥梁配合用产品A来赚钱(假设火车只允许运输A产品)
卡车与桥梁配合用任意一种产品来赚钱(假定卡车可以运输任何产品)
int main()
{
//第一种方式Train,只做Product_A生意:即参数只可以是Product_A
Product_A mProduct_A; //定义产品A
Train mTrain(&mProduct_A); //用产品A来初始化(设定)运输工具和桥梁
mTrain.MakeMoney(); //开动赚钱流程
cout << endl;
//第二种方式Truck,可以做Product_A、Product_B、Product_C三种产品的任意一种,即参数可以是任何一种产品
Product_A mProduct_A1; //定义产品A
Truck mTruck(&mProduct_A1); //用产品A来初始化(设定)运输工具和桥梁
mTruck.MakeMoney(); //开动赚钱流程
cout << endl;
Product_B mProduct_B; //定义产品B
Truck mTruck1(&mProduct_B); //用产品B来初始化(设定)运输工具和桥梁
mTruck1.MakeMoney(); //开动赚钱流程
cout << endl;
Product_C mProduct_C; //定义产品C
Truck mTruck2(&mProduct_C); //用产品C来初始化(设定)运输工具和桥梁
mTruck2.MakeMoney(); //开动赚钱流程
return 0;
}
桥梁的定义:(让其可以操作具有IProduct属性的任何产品)
class Bridge
{
public:
Bridge(IProduct *pproduct);
virtual ~Bridge(void);
void MakeMoney();
private:
IProduct *m_pProduct;
};
火车 产品A
商人 赚钱 桥梁 产品B 产品属性
卡车 产品C
Train Product_A
Businessman MakeMoney Bridge Product_B IProduct
Truck Product_C
说明:使用桥梁模式来实现一下内容:
一个商人做三种产品的生意,他有以下集中方式来运作:
用火车来运输A产品赚钱,需要经过桥梁Bridge
用卡车来运输A产品或者B产品或者C产品来赚钱,也需要经过桥梁Bridge
分析:A产品、B产品、C产品都具有产品属性IProduct
桥梁Bridge可以通过产品属性为IProduct的产品
火车与桥梁配合用产品A来赚钱(假设火车只允许运输A产品)
卡车与桥梁配合用任意一种产品来赚钱(假定卡车可以运输任何产品)
int main()
{
//第一种方式Train,只做Product_A生意:即参数只可以是Product_A
Product_A mProduct_A; //定义产品A
Train mTrain(&mProduct_A); //用产品A来初始化(设定)运输工具和桥梁
mTrain.MakeMoney(); //开动赚钱流程
cout << endl;
//第二种方式Truck,可以做Product_A、Product_B、Product_C三种产品的任意一种,即参数可以是任何一种产品
Product_A mProduct_A1; //定义产品A
Truck mTruck(&mProduct_A1); //用产品A来初始化(设定)运输工具和桥梁
mTruck.MakeMoney(); //开动赚钱流程
cout << endl;
Product_B mProduct_B; //定义产品B
Truck mTruck1(&mProduct_B); //用产品B来初始化(设定)运输工具和桥梁
mTruck1.MakeMoney(); //开动赚钱流程
cout << endl;
Product_C mProduct_C; //定义产品C
Truck mTruck2(&mProduct_C); //用产品C来初始化(设定)运输工具和桥梁
mTruck2.MakeMoney(); //开动赚钱流程
return 0;
}
桥梁的定义:(让其可以操作具有IProduct属性的任何产品)
class Bridge
{
public:
Bridge(IProduct *pproduct);
virtual ~Bridge(void);
void MakeMoney();
private:
IProduct *m_pProduct;
};