c_cpp C ++命令设计模式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C ++命令设计模式相关的知识,希望对你有一定的参考价值。

#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;
};

class CoffeeMachine
{
public:
  typedef std::vector<std::function<void()>> OrderQ;
  OrderQ orders;
  
  CoffeeMachine()
    : orders()
  {}
  
  void request(OrderQ::value_type order)
  {
    orders.push_back(order);
  }
  
  void start()
  {
    for(auto const& order : orders) { order(); }
    orders.clear();
  }
};

int main() 
{
  CoffeeMachine coffeeMachine;
      CaffeineBeverage coffee(
        [] { return Recipes::amountWaterMl(150); }, &Recipes::brewCoffee);
    CaffeineBeverage tea(
        [] { return Recipes::amountWaterMl(200); }, &Recipes::brewTea);
  
  coffeeMachine.request([&]{ coffee.prepare(); });
  coffeeMachine.request([&]{ tea.prepare(); });
  coffeeMachine.start();
  
  return 0;
}

以上是关于c_cpp C ++命令设计模式的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 命令行进度条

c_cpp MFC解析命令行

c_cpp 执行命令并获取输出

c_cpp 控制平面:推送命令和获取结果

c_cpp 在执行命令之前设置控制台模式

c_cpp main()函数与命令行args