2 Star 1 Fork 2

咸鱼(已废弃)/设计模式C语言

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
策略模式.txt 1.49 KB
一键复制 编辑 原始数据 按行查看 历史
Dick—_YangDi 提交于 2017-11-08 22:31 . fist all file
//策略模式 {将功能分类}
#include<iostream>
using namespace std;
class Takeoff{//飞机的起飞方式基类
public:
virtual void takeoff()=0;
virtual ~Takeoff(){}
};
class Reritied:public Takeoff{
public:
void takeoff(){
cout<<"Reritied"<<endl;
}
};
class LongDictance:public Takeoff{
public:
void takeoff(){
cout<<"LongDictance"<<endl;
}
};
class FlyBelanchir{//飞机的速度基类
public:
virtual void fly()=0;
virtual ~FlyBelanchir(){}
};
class SubSonic:public FlyBelanchir{
public:
void fly(){
cout<<"Subsonic"<<endl;
}
};
class Sup:public FlyBelanchir{
public:
void fly(){
cout<<"Sup"<<endl;
}
};
class Plan{//飞机
protected:
Takeoff * t;
FlyBelanchir * f;
public:
Plan(Takeoff * t,FlyBelanchir * f){
this->t=t;
this->f=f;
}
virtual void takeoff()=0;
virtual void fly()=0;
virtual ~Plan(){
delete t;
delete f;
}
};
class Helicoptor:public Plan{
public:
Helicoptor(Takeoff * t,FlyBelanchir * f):Plan(t,f){}
void takeoff(){
t->takeoff();
}
void fly(){
f->fly();
}
};
class Airplane:public Plan{
public:
Airplane(Takeoff * t,FlyBelanchir * f):Plan(t,f){}
void takeoff(){
t->takeoff();
}
void fly(){
f->fly();
}
};
class Fighter:public Plan{
public:
Fighter(Takeoff * t,FlyBelanchir * f):Plan(t,f){}
void takeoff(){
t->takeoff();
}
void fly(){
f->fly();
}
};
int main(void)
{
Plan * f=new Fighter(new Reritied(),new Sup());
f->takeoff();
f->fly();
delete f;
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Dick_YangDi/SheJiMoShi.git
git@gitee.com:Dick_YangDi/SheJiMoShi.git
Dick_YangDi
SheJiMoShi
设计模式C语言
master

搜索帮助