CommandPattern.cpp 802 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <iostream>
  2. using namespace std;
  3. class Reciever {
  4. public:
  5. void Action() {
  6. cout << "Reciever::Action..." << endl;
  7. }
  8. };
  9. class Command {
  10. public:
  11. virtual ~Command() { }
  12. virtual void Excute() = 0;
  13. };
  14. class ConcreteCommand :public Command {
  15. public:
  16. ConcreteCommand(Reciever *rev) {
  17. _rev = rev;
  18. }
  19. void Excute() {
  20. _rev->Action();
  21. }
  22. private:
  23. Reciever *_rev;
  24. };
  25. class Invoker {
  26. public:
  27. Invoker(Command* cmd) {
  28. _cmd = cmd;
  29. }
  30. void Invoke() {
  31. _cmd->Excute();
  32. }
  33. private:
  34. Command *_cmd;
  35. };
  36. int main() {
  37. Reciever *rev = new Reciever();
  38. Command* cmd = new ConcreteCommand(rev);
  39. Invoker *inv = new Invoker(cmd);
  40. inv->Invoke();
  41. delete rev;
  42. delete cmd;
  43. delete inv;
  44. return 0;
  45. }