Mediator Pattern
태그 :
- 개념
- - 마치 카운셀러 처럼 각 멤버로 부터 올라온 보고를 토대로 대국적인 판단을 해서 각 멤버에게 지시를 내리는 패턴
I. 의도
객체들간의 상호작용을 캡슐화해서 하나의 객체 안에 정의한다. 각 객체가 관련성을 갖는 다른 객체에 대한 참조 관계를 직접 정의하기보다는 이를 독립된 다른 객체가 관리하게 한다
정의: 마치 카운셀러 처럼 각 멤버로 부터 올라온 보고를 토대로 대국적인 판단을 해서 각 멤버에게 지시를 내리는 패턴
: 서로 관련된 객체 사이의 복잡한 통신과 제어를 한 곳으로 집중시키고자 하는 경우에는 미디에이터 패턴을 사용한다.
II. 활용
- 여러 객체가 잘 정의된 형태이나 복잡한 상호 작용을 하는 경우, 객체들 간의 의존성을 잘 이해하기 어려울 때
- 객체의 재사용이 다른 객체와의 연결관계의 복잡함으로 인해 방해를 받을 때
- 여러 클래스에 분산된 행위들이 상속없이 수정되어야 할 때
III. 결과
- 서브클래스 수를 제한한다
- colleague 들 사이의 종속성을 줄인다
- 객체의 프로토콜을 단순화하는 장점이 있다
- 객체들 간의 협력 방법을 하나의 클래스로 추상화한다
- 통제의 집중화가 이루어진다 – mediator 클래스 자체의 유지보수가 어려워질 수 있다
관련 패턴: façade 패턴은 객체들로 구성된 서브시스템을 추상화해 좀 더 편한 인터페이스를 제공하려는 것으로, façade 객체는 서브시스템을 구성하는 객체로만 메시지가 전달되고, 서브시스템 구성객체가 façade 객체에 메시지를 전달하지는 못한다. Mediator객체는 양방향이다.
상호 관련된 객체들은 observer 패턴을 이용해서 mediator 객체들과 교류한다
Mediator: Colleague 객체와 교류하는 데 필요한 인터페이스를 정의한다.
ConcreteMediator: Colleague 객체와 조화를 이룸으로써 이루어지는 협력 행위를 구현하고 자신의 colleague 가 무엇인지를 알고 이를 관리한다.
Colleague: Mediator 객체가 누구인지를 안다. 다른 객체와 연결성을 필요하면 Mediator 를 통해 이루어지도록 한다.
소스코드
public class MediatorReal {
public static void main(String[] args) {
IChatroom m = new Chatroom();
Participant c1 = new BeatleParticipant("Beatle");
Participant c2 = new NonBeatleParticipant("NonBeatle");
m.Register(c1);
m.Register(c2);
c1.Send(c2.getName(), "How are you?");
c2.Send(c1.getName(), "Fine, thanks");
}
}
//"Mediator"
interface IChatroom {
void Register(Participant participant);
void Send(String from, String to, String message);
}
// "ConcreteMediator"
class Chatroom implements IChatroom {
// Fields
private Hashtable participants = new Hashtable();
// Methods
public void Register(Participant participant) {
if (participants.containsKey(participant.getName()) == false) {
participants.put(participant.getName(), participant);
}
participant.setChatroom(this);
}
public void Send(String from, String to, String message) {
Participant pto = (Participant) participants.get(to);
if (pto != null)
pto.Receive(from, message);
}
}
// "AbstractColleague"
class Participant {
private Chatroom chatroom;
private String name;
// Constructors
public Participant(String name) {
this.name = name;
}
// Methods
public void Send(String to, String message) {
chatroom.Send(name, to, message);
}
public void Receive(String from, String message) {
System.out.println(from + " to " + this.name + " : " + message);
}
public void setChatroom(Chatroom chatroom) {
this.chatroom = chatroom;
}
public String getName() {
return name;
}
}
//" ConcreteColleague1"
class BeatleParticipant extends Participant {
// Constructors
public BeatleParticipant(String name) {
super(name);
}
public void Receive(String from, String message) {
System.out.println("To a Beatle: ");
super.Receive(from, message);
}
}
//" ConcreteColleague2"
class NonBeatleParticipant extends Participant {
// Constructors
public NonBeatleParticipant(String name) {
super(name);
}
public void Receive(String from, String message) {
System.out.println("To a non-Beatle: ");
super.Receive(from, message);
}
}