[C++] 纯文本查看 复制代码 #include <iostream>
using namespace std;
namespace bridage_pattern {
// 炫彩
class abstract_xuancai
{
public:
virtual void dealwith_details() = 0;
virtual ~abstract_xuancai() {}
};
// 黑色 飞龙在天
class dark_feilong : public abstract_xuancai
{
public:
void dealwith_details() {
printf("飞龙在天 炫彩\n");
}
};
// 白色 白衣青龙
class white_qinglong : public abstract_xuancai
{
public:
void dealwith_details() {
printf("白色青龙 炫彩\n");
}
};
// 不败拳王
class bubai_quanwang : public abstract_xuancai
{
public:
void dealwith_details() {
printf("不败拳王 炫彩\n");
}
};
// 英雄联盟皮肤
class abstract_skin
{
public:
virtual void switch_skin(abstract_xuancai *ab_xuancai) = 0;
virtual ~abstract_skin() {}
protected:
abstract_xuancai *m_ab_xuancai; // 包含炫彩对象 让他们有座桥
};
// 龙的传人
class longdechuanren : public abstract_skin
{
public:
void switch_skin(abstract_xuancai *ab_xuancai) override {
this->m_ab_xuancai = ab_xuancai;
this->m_ab_xuancai->dealwith_details();
printf("更换龙的传人\n");
}
};
// 至高之拳
class wuyitao : public abstract_skin
{
public:
void switch_skin(abstract_xuancai *ab_xuancai) override {
this->m_ab_xuancai = ab_xuancai;
this->m_ab_xuancai->dealwith_details();
printf("更换至高之拳\n");
}
};
// 地下拳王
class quanwang : public abstract_skin
{
public:
void switch_skin(abstract_xuancai *ab_xuancai) override {
this->m_ab_xuancai = ab_xuancai;
this->m_ab_xuancai->dealwith_details();
printf("更换地下拳王\n");
}
};
}
// 可以看出将两个有某种关联的对象 隔离起来 让它们各自发展 降低两者的耦合性 再让一个对象包含另一个对象让它们之间又可以联系起来,就好像为它们搭了一座桥~
int main(void)
{
using namespace bridage_pattern;
abstract_skin *skin_longdechuanren = new longdechuanren;
abstract_skin *skin_quanwang = new quanwang;
abstract_xuancai *xuancai_qinglong = new white_qinglong;
abstract_xuancai *xuancai_quanwang = new bubai_quanwang;
skin_longdechuanren->switch_skin(xuancai_qinglong);
skin_quanwang->switch_skin(xuancai_quanwang);
delete xuancai_quanwang;
delete xuancai_qinglong;
delete skin_quanwang;
delete skin_longdechuanren;
return 0;
}
// 总结
// 优点:各自发展,降低耦合,扩展容易
// 缺点:调用相对繁杂 |