320x100
구조
#pragma once
#include <iostream>
#include <algorithm>
class Colleague;
class Mediator
{
public:
virtual void request() {}
protected:
Colleague* colleague1;
Colleague* colleague2;
};
class Colleague
{
public:
virtual std::string request() {}
virtual std::string request(std::string str) {}
protected:
Mediator* mediator_;
};
class ConcreteColleague1 : public Colleague
{
public:
ConcreteColleague1(Mediator* mediator)
{
mediator_ = mediator;
}
std::string request() {
return "Apple";
}
};
class ConcreteColleague2 : public Colleague
{
public:
ConcreteColleague2(Mediator* mediator)
{
mediator_ = mediator;
}
std::string request(std::string str)
{
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
return str;
}
};
class ConcreteMediator : public Mediator
{
public:
ConcreteMediator()
{
colleague1 = new ConcreteColleague1(this);
colleague2 = new ConcreteColleague2(this);
}
void request() override
{
std::string str = colleague1->request();
std::string result = colleague2->request(str);
std::cout << result;
}
};
사용
int main()
{
Mediator* mediator = new ConcreteMediator;
mediator->request();
system("pause");
return 0;
}
여러가지의 객체들을 사용해야 할 때, 그 구조가 복잡해지니까
하나의 객체가(A) 모든 객체(a,b,c,d...)들을 멤버변수로 갖고, 그 멤버변수들 끼리 관계를 조절한다.
320x100