c_cpp C ++策略设计部分(参考:http://isocpp.org/blog/2013/10/patterns)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C ++策略设计部分(参考:http://isocpp.org/blog/2013/10/patterns)相关的知识,希望对你有一定的参考价值。
// c++ -std=c++11 -Wall ./strategy_cpp11.cpp -o strategy_cpp11
#include <iostream>
#include <vector>
#include <functional>
class Recipes {
public:
static void brewCoffee() { std::cout << "dripping Coffee through filter\n"; }
static void brewTea() { std::cout << "steeping Tea\n"; }
static int amountWaterMl(int ml) { return ml; }
};
class CaffeineBeverage {
public:
CaffeineBeverage(std::function<int()> amountWaterMl, std::function<void()> brew)
: _amountWaterMl(amountWaterMl)
, _brew(brew)
{}
void prepare() {
boilWater(_amountWaterMl());
_brew();
pourInCup();
}
private:
void boilWater(int ml) { std::cout << "boiling " << ml << " water\n"; }
void pourInCup() { std::cout << "pour in cup\n"; }
std::function<int()> _amountWaterMl;
std::function<void()> _brew;
};
int main() {
CaffeineBeverage coffee(
[] { return Recipes::amountWaterMl(150); }, &Recipes::brewCoffee);
CaffeineBeverage tea(
[] { return Recipes::amountWaterMl(200); }, &Recipes::brewTea);
using Beverages = std::vector<CaffeineBeverage*>;
Beverages beverages;
beverages.push_back(&coffee);
beverages.push_back(&tea);
for(auto &beverage : beverages) {
beverage->prepare();
}
}
以上是关于c_cpp C ++策略设计部分(参考:http://isocpp.org/blog/2013/10/patterns)的主要内容,如果未能解决你的问题,请参考以下文章
什么是CA安全体系,CA认证体系,C A 分别代表什么
yywebimage和sdwebimage哪个好
设计模式策略模式
在 XS 中为 C 库注册多个 Perl 子引用
设计模式之策略模式
设计模式之PHP项目应用——策略模式设计自动驾驶系统