Command is a behavioral design pattern that turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as a method arguments, delay or queue a request’s execution, and support undoable operations.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Operation_binary{
public:
virtual double exec(double d1, double d2)const = 0;
};
class Sum_binary : public Operation_binary {
public:
double exec(double d1, double d2)const override {
return d1 + d2;
}
};
class Subtract_binary : public Operation_binary {
public:
double exec(double d1, double d2)const override {
return d1 - d2;
}
};
class Multiply_binary : public Operation_binary {
public:
double exec(double d1, double d2)const override {
return d1 * d2;
}
};
class Divide_binary : public Operation_binary {
public:
double exec(double d1, double d2)const override {
return d1 / d2;
}
};
class Calculator {
public:
static double calculate(const Operation_binary& op, double d1, double d2) {
return op.exec(d1, d2);
}
};
int main() {
auto d = Calculator::calculate(Sum_binary(), 10, 20);
auto b = Calculator::calculate(Multiply_binary(), 10, 20);
cout << d << endl;
cout << b << endl;
}
30
200


Leave a Reply