前段时间有玩过妄想山海。所以对妄想山海中的动物的类比较好奇。所以就是尝试着自己写一下。主要涉及到的模式为建造者模式,算是一个比较大的实例吧。
建造者模式参考自建造者模式
一、组件类
妄想山海中的动物都是有多个组件的,如:头,尾,角什么的。而且可以进行修改动物的组件,且根据不同部件可以完成各自的功能。比如老虎安上翅膀,就能飞啥的。
先看看类图:
组件接口:
//组件接口
class IComponent{
public:
virtual void SetName(std::string name)=0;
virtual std::string GetName()=0;
virtual void Operation()=0;
virtual ~IComponent(){
}
};
具体组件类:
//翅膀
class Wing:public IComponent{
private:
std::string name;
public:
Wing(){
}
Wing(std::string name){
this->name=name;
}
void SetName(std::string name)override{
this->name=name;
}
std::string GetName()override{
return name;
}
void Operation(){
if(name=="无")
{
std::cout<<"没有翅膀!"<<std::endl;
}
else{
std::cout<<"使用"<<name<<"进行了飞行!"<<std::endl;
}
}
};
//角
class Horn:public IComponent{
private:
std::string name;
public:
Horn(){
}
Horn(std::string name){
this->name=name;
}
void SetName(std::string name)override{
this->name=name;
}
std::string GetName()override{
return name;
}
void Operation(){
if(name=="无")
{
std::cout<<"没有角!"<<std::endl;
}
else{
std::cout<<"使用"<<name<<"进行了冲刺!"<<std::endl;
}
}
};
//腿
class Leg:public IComponent{
private:
std::string name;
public:
Leg(){
}
Leg(std::string name){
this->name=name;
}
void SetName(std::string name)override{
this->name=name;
}
std::string GetName()override{
return name;
}
void Operation(){
if(name=="无")
{
std::cout<<"没有腿!"<<std::endl;
}
else{
std::cout<<"使用"<<name<<"进行了跑步!"<<std::endl;
}
}
};
//身体
class Body:public IComponent{
private:
std::string name;
public:
Body(){
}
Body(std::string name){
this->name=name;
}
void SetName(std::string name)override{
this->name=name;
}
std::string GetName()override{
return name;
}
void Operation(){
if(name.find("鱼")!=std::string::npos){
std::cout<<"使用"<<name<<"进行了游泳!"<<std::endl;
}
else{
std::cout<<"使用"<<name<<"进行了撞击石头!"<<std::endl;
}
}
};
//尾巴
class Tail:public IComponent{
private:
std::string name;
public:
Tail(){
}
Tail(std::string name){
this->name=name;
}
void SetName(std::string name)override{
this->name=name;
}
std::string GetName()override{
return name;
}
void Operation(){
if(name=="无")
{
std::cout<<"没有尾巴!"<<std::endl;
}
else{
std::cout<<"使用"<<name<<"进行了晃尾巴!"<<std::endl;
}
}
};
//头
class Head:public IComponent{
private:
std::string name;
public:
Head(){
}
Head(std::string name){
this->name=name;
}
void SetName(std::string name)override{
this->name=name;
}
std::string GetName()override{
return name;
}
void Operation(){
if(name=="无")
{
std::cout<<"没有头!"<<std::endl;
}
else{
std::cout<<"使用"<<name<<"进行了摇头!"<<std::endl;
}
}
};
二、动物接口
动物的类,主要就是可以设置这些部件啥的,相当于建造者模式中的产品类。
先看看类图:
动物类接口:
//动物接口
class IAnimal{
public:
virtual void SetName(std::string name)=0;
virtual void SetBody(IComponent*)=0;
virtual void SetHorn(IComponent*)=0;
virtual void SetTail(IComponent*)=0;
virtual void SetHead(IComponent*)=0;
virtual void SetWing(IComponent*)=0;
virtual void SetLeg(IComponent*)=0;
virtual IComponent* GetBody()=0;
virtual IComponent* GetHorn()=0;
virtual IComponent* GetTail()=0;
virtual IComponent* GetHead()=0;
virtual IComponent* GetWing()=0;
virtual IComponent* GetLeg()=0;
virtual void Operation(IComponent*)=0;
virtual void Show()=0;
virtual ~IAnimal(){
}
};
具体动物类:
//动物类
class Animal:public IAnimal{
protected:
IComponent* body;
IComponent* head;
IComponent* horn;
IComponent* wing;
IComponent* tail;
IComponent* leg;
std::string name;
public:
Animal(){
}
Animal(std::string name){
this->name=name;}
~Animal() override{
delete body;
delete head;
delete horn;
delete wing;
delete tail;
delete leg;
}
void SetName(std::string name) override{
this->name=name;
}
void SetBody(IComponent* body) override{
this->body=body;
}
void SetHead(IComponent* head) override{
this->head=head;
}
void SetHorn(IComponent* horn) override{
this->horn=horn;
}
void SetWing(IComponent* wing) override{
this->wing=wing;
}
void SetLeg(IComponent* leg) override{
this->leg=leg;
}
void SetTail(IComponent* tail) override{
this->tail=tail;
}
IComponent* GetBody()override{
return this->body;
}
IComponent* GetTail()override{
return this->tail;
}
IComponent* GetWing()override{
return this->wing;
}
IComponent* GetHead()override{
return this->head;
}
IComponent* GetHorn()override{
return this->horn;
}
IComponent* GetLeg()override{
return this->leg;
}
void Show() override{
std::cout<<this->name<<"的各个部件如下:"<<std::endl;
std::cout<<"头:"<<head->GetName()<<std::endl;
std::cout<<"角:"<<horn->GetName()<<std::endl;
std::cout<<"躯干:"<<body->GetName()<<std::endl;
std::cout<<"翅膀:"<<wing->GetName()<<std::endl;
std::cout<<"腿:"<<leg->GetName()<<std::endl;
std::cout<<"尾巴:"<<tail->GetName()<<std::endl;
std::cout<<std::endl;
}
void Operation(IComponent* icomp)override{
std::cout<<name;
icomp->Operation();
std::cout<<std::endl;
}
};
三、建造者类
建造者类,就是一个抽象建造者,两个具体建造者。
先看类图
建造者接口:
//建造者接口
class IBuilder{
public:
void Build(){
BuildBody();
BuildHead();
BuildHorn();
BuildLeg();
BuildTail();
BuildWing();
};
virtual IAnimal* GetResult()=0;
virtual ~IBuilder(){
}
private:
virtual void BuildBody()=0;
virtual void BuildHead()=0;
virtual void BuildTail()=0;
virtual void BuildWing()=0;
virtual void BuildLeg()=0;
virtual void BuildHorn()=0;
};
具体建造者:
//具体建造者1:山羊
class Goat:public IBuilder{
protected:
IAnimal* myAnimal;
public:
Goat(IAnimal* ani){
myAnimal=ani;
}
IAnimal* GetResult() override{
return myAnimal;
};
~Goat() override{
delete myAnimal;
}
private:
void BuildBody()override{
IComponent* comp=new Body("山羊身体");
myAnimal->SetBody(comp);
}
void BuildHead()override{
IComponent* comp=new Head("山羊头");
myAnimal->SetHead(comp);
}
void BuildWing()override{
IComponent* comp=new Wing("无");
myAnimal->SetWing(comp);
}
void BuildLeg()override{
IComponent* comp=new Leg("山羊腿");
myAnimal->SetLeg(comp);
}
void BuildHorn()override{
IComponent* comp=new Horn("山羊角");
myAnimal->SetHorn(comp);
}
void BuildTail()override{
IComponent* comp=new Tail("山羊尾");
myAnimal->SetTail(comp);
}
};
//具体建造者2,老虎
class Tiger:public IBuilder{
protected:
IAnimal* myAnimal;
public:
Tiger(IAnimal* ani){
myAnimal=ani;
}
IAnimal* GetResult() override{
return myAnimal;
};
~Tiger() override{
delete myAnimal;
}
private:
void BuildBody()override{
IComponent* comp=new Body("老虎身体");
myAnimal->SetBody(comp);
}
void BuildHead()override{
IComponent* comp=new Head("老虎头");
myAnimal->SetHead(comp);
}
void BuildWing()override{
IComponent* comp=new Wing("无");
myAnimal->SetWing(comp);
}
void BuildLeg()override{
IComponent* comp=new Leg("老虎腿");
myAnimal->SetLeg(comp);
}
void BuildHorn()override{
IComponent* comp=new Horn("无");
myAnimal->SetHorn(comp);
}
void BuildTail()override{
IComponent* comp=new Tail("老虎尾");
myAnimal->SetTail(comp);
}
};
四、指挥者
指挥者主要就是调用建造者中的方法完成复杂对象的创建。
先看类图
指挥者类:
//指挥者
class Director{
public:
Director(IBuilder* builder){
this->builder=builder;
}
IAnimal* Construct(){
builder->Build();
return builder->GetResult();
}
virtual ~Director(){
delete builder;
}
private:
IBuilder* builder;
};
五、主函数
测试两个具体建造者,并更改Animal对象中组件,并使用组件执行动作。
//实例
int main(void)
{
//建造一个山羊
IAnimal* ani=new Animal("山羊");
IBuilder* builder=new Goat(ani);
Director* dirtector=new Director(builder);
ani=dirtector->Construct();
ani->Show();//展示组件
ani->Operation(ani->GetHorn());//使用角
//由于每个类的析构函数中都有delete,并且构造时都是使用的指针,因此只需要delete指挥者即可。
delete dirtector;
//建造一个老虎
IAnimal* ani_1=new Animal("老虎");
builder=new Tiger(ani_1);
dirtector=new Director(builder);
ani_1=dirtector->Construct();
ani_1->Show();
ani_1->Operation(ani_1->GetWing());//使用翅膀,无法飞行
IComponent* comp =new Wing("鹏翅");
ani_1->SetWing(comp);//安装鹏翅
ani_1->Show();
ani_1->Operation(ani_1->GetWing());//开始飞行
delete dirtector;
return 0;
}