본문 바로가기
💻 programming/design pattern

[c++] Mediator Pattern

by 연구원-A 2020. 12. 16.
반응형

중재자 패턴을 사용하면 객체 간 통신은 중재자 객체 안에 함축됩니다. 객체들은 더 이상 다른 객체와 서로 직접 통신하지 않으며 대신 중재자를 통해 통신합니다. 이를 통해 통신 객체 간 의존성을 줄일 수 있으므로 결합도를 감소시킬 수 있습니다.

 

Class Diagram

Colleage는 Mediator를 참조하고, Mediator는 Colleage를 참조하고 있습니다.

 

Pros. and Cons.

  • 장점: 전체적인 연결관계를 이해하기 쉽다 (communication의 흐름을 이해하기 쉽다)
  • 단점: 특정 application 로직에 맞춰져있기 때문에 다른 application에 재사용하기 힘들다 (Observer Pattern과 반대)

 

 무엇보다도 하나의 클래스가 여러 객체를 관리하는 형태를 가지므로 신 (God) 클래스가 만들어지지 않도록 유의해야 합니다.

 

Sample Code

main function

#include <iostream>
#include <string>
#include "concrete_mediator.h"
#include "concrete_component.h"

using namespace std;

int main() {
  Component1 *c1 = new Component1;
  Component2 *c2 = new Component2;
  ConcreteMediator *mediator = new ConcreteMediator(c1, c2);

  std::cout << "Client triggers operation A.\n";
  c1->DoA();
  std::cout << "\n";

  std::cout << "Client triggers operation D.\n";
  c2->DoD();

  delete c1;
  delete c2;
  delete mediator;

 

Mediator (interface)

#ifndef MEDIATOR_H_
#define MEDIATOR_H_

#include <iostream>
#include <string>

#include "component.h"
class Mediator {
 public:
    virtual void Notify(BaseComponent* sender, std::string event) const = 0;
};


#endif

 

Concrete Mediator

  • 중재자 객체는 컴포넌트 객체들을 참조해야 합니다. (Component1, Component2)
  • 컴포넌트 객체가 notify (=mediate) 함수를 호출하면 중재자 객체는 정해진 동작을 수행합니다
#ifndef CONCRETE_MEDIATOR_H_
#define CONCRETE_MEDIATOR_H_

#include <iostream>
#include <string>

#include "mediator.h"
#include "concrete_component.h"

class ConcreteMediator : public Mediator {
 public:
    ConcreteMediator(Component1 *c1, Component2* c2) : component1_(c1), component2_(c2) {
        this->component1_->set_mediator(this);
        this->component2_->set_mediator(this);
    }

    void Notify(BaseComponent* sender, std::string event) const override {
        if(event == "A") {
            std::cout << "Mediator reacts on event A and triggers following operations" << std::endl;
            this->component2_->DoC();
        }
        if(event == "D") {
            std::cout << "Mediator reacts on event D and triggers following operations" << std::endl;
            this->component1_->DoB();
            this->component2_->DoC();
        }
    }

 private:
    Component1 *component1_;
    Component2 *component2_;
};

#endif

 

Component

#ifndef COMPONENT_H_
#define COMPONENT_H_

#include <iostream>
#include <string>

using namespace std;
class Mediator;
class BaseComponent {
 public:
    BaseComponent(Mediator* mediator = nullptr) : mediator_(mediator) { }
    void set_mediator(Mediator* mediator) {
        this->mediator_ = mediator;
    }
 protected:
    Mediator* mediator_;
};

#endif

 

Concrete Component

#ifndef CONCRETE_COMPONENT_H_
#define CONCRETE_COMPONENT_H_

#include <iostream>
#include <string>
#include "component.h"

using namespace std;

class Component1 : public BaseComponent {
 public:
    void DoA() {
        std::cout << "Component 1 does A" << std::endl;
        this->mediator_->Notify(this, "A");
    }
    void DoB() {
        std::cout << "Component 1 does B" << std::endl;
        this->mediator_->Notify(this, "B");
    }
};

class Component2 : public BaseComponent {
 public:
    void DoC() {
        std::cout << "Component 2 does C" << std::endl;
        this->mediator_->Notify(this, "C");
    }
    void DoD() {
        std::cout << "Component 2 does D" << std::endl;
        this->mediator_->Notify(this, "D");
    }
};
#endif

 

반응형

'💻 programming > design pattern' 카테고리의 다른 글

[c++] Visitor Pattern  (0) 2020.12.16
[c++] Observer Pattern  (0) 2020.12.16

댓글