1 Star 0 Fork 621

popey/DesignPattern

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
StrategyPattern.cpp 748 Bytes
一键复制 编辑 原始数据 按行查看 历史
#include <iostream>
using namespace std;
class Strategy {
public:
virtual void Interface() = 0;
virtual ~Strategy() { }
};
class ConcreteStrategyA :public Strategy {
public:
void Interface() {
cout << "ConcreteStrategyA::Interface..." << endl;
}
};
class ConcreteStrategyB :public Strategy {
public:
void Interface() {
cout << "ConcreteStrategyB::Interface..." << endl;
}
};
class Context {
public:
Context(Strategy *stg) {
_stg = stg;
}
void DoAction() {
_stg->Interface();
}
private:
Strategy *_stg;
};
int main() {
Strategy *ps = new ConcreteStrategyA();
Context *pc = new Context(ps);
pc->DoAction();
delete pc;
delete ps;
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/popey/DesignPattern.git
git@gitee.com:popey/DesignPattern.git
popey
DesignPattern
DesignPattern
master

搜索帮助