来源:爱编程的大丙
定义:将抽象部分和它的实现部分实现部分分离,使它们可以独立变化,这种处理模式就是桥接模式。
cpp
#include<iostream>
#include<map>
#include<string>
using namespace std;
//人员信息
struct Person {
//使用 string biezhu=string()
// 可以让 Person 类的构造函数更灵活,调用者可以选择是否提供 biezhu 参数。
Person(string name, string job, string ablity, string reward, string biezhu = string())
{
this->name = name;
this->job = job;
this->ability = ability;
this->reward = reward;
this->beiZhu = biezhu;
}
~Person() {
cout << name << "被析构..." << endl;
}
string name; // 名字
string job; // 职责
string ability; // 能力
string reward; // 赏金
string beiZhu; // 备注
};
//抽象船队
class AbstractTeam {
public:
AbstractTeam(string name) :m_teamName(name) {}
string getTeamName(){
return m_teamName;
}
void addMember(Person* p) {
m_infoMap.insert(make_pair(p->name, p));
}
void show() {
cout << m_teamName << endl;
for (const auto& item : m_infoMap) {
cout << "【Name: " << item.second->name
<< ", Job: " << item.second->job
<< ", Ability: " << item.second->ability
<< ", MoneyReward: " << item.second->reward
<< ", BeiZhu: " << item.second->beiZhu
<< "】" << endl;
}
}
virtual void executeTask() = 0;
virtual ~AbstractTeam() {
for (const auto& item : m_infoMap) {
delete item.second;
}
}
protected:
string m_teamName;
map<string, Person*>m_infoMap;
};
class CaoMaoTeam :public AbstractTeam {
public:
using AbstractTeam::AbstractTeam;
void executeTask() override {
cout << "在海上冒险,找到 ONE PIECE 成为海贼王!" << endl;
}
};
class SmokerTeam :public AbstractTeam {
public:
using AbstractTeam::AbstractTeam;
void executeTask() override {
cout << "为了正义, 先将草帽一伙一网打尽!!!" << endl;
}
};
class AbstractShip {
public:
AbstractShip(AbstractTeam* team) :m_team(team) {}//桥梁,聚合关系
void showTeam() {
m_team->show();
m_team->executeTask();
}
virtual string getName() = 0;
virtual void feature() = 0;
virtual ~AbstractShip() {}
protected:
AbstractTeam* m_team = nullptr;
};
class MerryShip :public AbstractShip {
public:
using AbstractShip::AbstractShip;
string getName() override {
return string("前进·梅利号");
}
void feature()override {
cout<<getName()<<" -- 船首为羊头, 在司法岛化身船精灵舍己救下了草帽一伙!" << endl;
}
};
class HaiJunShip :public AbstractShip {
public:
using AbstractShip::AbstractShip;
string getName() override {
return string("无敌号");
}
void feature()override {
cout << getName() << " -- 船底由海楼石建造, 可以穿过无风带的巨大炮舰!" << endl;
}
};
int main() {
//草帽海贼团
CaoMaoTeam* caomao = new CaoMaoTeam("草帽海贼团");
Person* luffy=new Person("路飞", "船长", "橡胶果实能力者", "30亿贝里", "爱吃肉");
Person* zoro = new Person("索隆", "剑士", "三刀流", "11亿1100万贝里", "路痴");
Person* sanji = new Person("山治", "厨师", "隐形黑", "10亿3200万贝里", "好色");
Person* nami = new Person("娜美", "航海士", "天候棒+宙斯", "3亿6600万贝里", "喜欢钱");
caomao->addMember(luffy);
caomao->addMember(luffy);
caomao->addMember(zoro);
caomao->addMember(sanji);
caomao->addMember(nami);
MerryShip* sunny = new MerryShip(caomao);
sunny->feature();
sunny->showTeam();
// 斯摩格的船队
SmokerTeam* team = new SmokerTeam("斯摩格的海军部队");
Person* smoker = new Person("斯摩格", "中将", "冒烟果实能力者", "", "爱吃烟熏鸡肉");
Person* dasiqi = new Person("达斯琪", "大佐", "一刀流", "", "近视");
team->addMember(smoker);
team->addMember(dasiqi);
HaiJunShip* ship = new HaiJunShip(team);
ship->feature();
ship->showTeam();
delete caomao;
delete sunny;
delete team;
delete ship;
return 0;
}