중재자 패턴을 사용해 ChatRoom을 중재자로 두고, 사용자들이 서로를 직접 참조하지 않고 메시지를 주고받게 구현하세요.
사용자는 ChatRoom을 통해서만 통신합니다. 다른 사용자에 대한 직접 참조는 없습니다.
export class User {
name: string;
messages: { from: string; message: string }[];
receive(from: string, message: string): void;
}
export class ChatRoom {
register(user: User): void;
send(from: string, message: string): void; // 발신자 본인은 받지 않음
}
const room = new ChatRoom();
const alice = new User('alice');
const bob = new User('bob');
room.register(alice);
room.register(bob);
room.send('alice', '안녕!');
// bob.messages = [{ from: 'alice', message: '안녕!' }]
// alice.messages = [] ← 본인은 받지 않음