Mediator.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <stdio.h>
  2. /*
  3. * C++ Design Patterns: MEDIATOR
  4. * http://www.plantuml.com/plantuml/uml/TL31RXCn4BtxAqRg8Q1e6t6DLgjIoe53G4AzLxFs8pNoseizNX417zJlw8_XEDqIPOLFC-_DUsyyDqauIXwSsgnez_7kq_lxBrzfjL77fnsdH3iOon8Y_L9KtXMnCM_UljJ69ecspu82_Pab7uBN4O8BAJKqGPV-GGVdmFkCT2BMisbj_kSwY_xPZT5EB7WONe8kSMjEo1KbUBDB-vfHWGWDEw5YoyqNMUjBX_N2uT4Q48PH3ZHWw16JKgyNQ9kp_RTa_RSG1vpnlvjcSJUrE6JJBLTKhzN9PJcWV10G9_hyhYxVzbshWmjn6yE1tQtBwEQ8TbEPhjtqtJnyVnYnFG9qYqUUR8ZiAdVt_AIZ5SgUGlyTGZ-o9NZDOyeEv938Ua5aBJRuL3LcLQRXKpcHG-cgy9fsa88IJ-o52b5dTSYZupt8FJ_j4MdaMB9GJAUW-UX9-3awgwtWMi4RElvKtPGg3-ul
  5. *
  6. */
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10. class Mediator;
  11. class Colleague {
  12. public:
  13. Colleague(Mediator* const m, unsigned int i)
  14. : private_mediator(m), private_id(i) {}
  15. virtual ~Colleague() = default;
  16. unsigned int getId() {
  17. return private_id;
  18. }
  19. virtual void sendMsg(std::string) = 0;
  20. virtual void receiveMsg(std::string) = 0;
  21. protected:
  22. Mediator* private_mediator;
  23. unsigned int private_id;
  24. };
  25. class ConcreteColleague : public Colleague {
  26. public:
  27. ConcreteColleague(Mediator* const m, unsigned int i)
  28. : Colleague(m, i) {}
  29. ~ConcreteColleague() = default;
  30. void sendMsg(std::string msg) override;
  31. void receiveMsg(std::string msg) override {
  32. std::cout << "Message: " << msg << " is received by Colleague(id=" << this->getId() << ")" << std::endl;
  33. }
  34. };
  35. class Mediator {
  36. public:
  37. virtual ~Mediator() = default;
  38. virtual void add(Colleague* const c) = 0;
  39. virtual void distribute(Colleague* const sender, const std::string& msg) = 0;
  40. };
  41. class ConcreteMediator : public Mediator {
  42. public:
  43. ~ConcreteMediator() {
  44. for (unsigned int i = 0; i < colleagues_.size(); ++i) {
  45. delete colleagues_[i];
  46. }
  47. vector_colleagues.clear();
  48. }
  49. void add(Colleague* const c) {
  50. vector_colleagues.push_back(c);
  51. }
  52. void distribute(Colleague* const sender, const std::string& msg) {
  53. for (auto colleague : colleagues_) {
  54. if (colleague->getId() != sender->getId()) {
  55. colleague->receiveMsg(msg);
  56. }
  57. }
  58. }
  59. private:
  60. std::vector<Colleague*> vector_colleagues;
  61. };
  62. void ConcreteColleague::sendMsg(std::string msg) {
  63. std::cout << "Message: " << msg << " is sent by Colleague(id=" << this->getId() << ")" << std::endl;
  64. private_mediator->distribute(this, msg);
  65. }
  66. int main() {
  67. Mediator *mediator = new ConcreteMediator();
  68. Colleague *colleague1 = new ConcreteColleague(mediator, 1);
  69. Colleague *colleague2 = new ConcreteColleague(mediator, 2);
  70. Colleague *colleague3 = new ConcreteColleague(mediator, 3);
  71. mediator->add(colleague1);
  72. mediator->add(colleague2);
  73. mediator->add(colleague3);
  74. colleague1->sendMsg("Hello");
  75. colleague2->sendMsg("World");
  76. colleague3->sendMsg("Mediator Pattern");
  77. }