채팅 구현하기
Colleague.class
public abstract class Colleague {
private Mediator mediator;
protected String name;
private String message;
public Colleague(String name){
this.name = name;
}
public void setMediator(Mediator mediator) {
this.mediator = mediator;
}
public String getName() {
return this.name;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage(){
return this.message;
}
public void send() {
System.out.println(this.name + " send()");
System.out.println();
mediator.mediate(this);
}
public abstract void receive(Colleague colleague);
}
ConcreteColleague1.class
// User
public class ConcreteColleague1 extends Colleague {
public ConcreteColleague1(String name) {
super(name);
}
@Override
public void receive(Colleague colleague) {
System.out.println(this.name + " recevied " + colleague.getName() + "'s Message : " + colleague.getMessage());
}
}
ConcreteColleague2.class
// System
public class ConcreteColleague2 extends Colleague {
public ConcreteColleague2(String name) {
super(name);
}
@Override
public void receive(Colleague colleague) {
System.out.println("System can't receive messages");
}
}
ConcreteColleague3.class
// Admin
public class ConcreteColleague3 extends Colleague {
public ConcreteColleague3(String name) {
super(name);
}
@Override
public void receive(Colleague colleague) {
System.out.println("Admin can't receive messages");
}
}
Mediator.class
public interface Mediator {
void addColleague(Colleague colleague);
void mediate(Colleague colleague);
}
ConcreteMediator.class
public class ConcreteMediator implements Mediator {
private List<Colleague> colleagueList;
public ConcreteMediator() {
this.colleagueList = new ArrayList<Colleague>();
}
@Override
public void addColleague(Colleague colleague) {
this.colleagueList.add(colleague);
}
@Override
public void mediate(Colleague colleague) {
for(Colleague receiverColleague : colleagueList) {
System.out.println("\tMediating " + colleague.getName() + " to " + receiverColleague.getName());
receiverColleague.receive(colleague);
}
}
}
Main.class
public static void main(String args[]) {
Mediator mediator1 = new ConcreteMediator();
Colleague colleague1 = new ConcreteColleague1("User1");
Colleague colleague2 = new ConcreteColleague1("User2");
Colleague colleague3 = new ConcreteColleague2("System");
Colleague colleague4 = new ConcreteColleague3("Admin");
colleague1.setMediator(mediator1);
colleague2.setMediator(mediator1);
colleague3.setMediator(mediator1);
colleague4.setMediator(mediator1);
mediator1.addColleague(colleague1);
mediator1.addColleague(colleague2);
mediator1.addColleague(colleague3);
mediator1.addColleague(colleague4);
colleague1.setMessage("I'm User1");
colleague1.send();
}
실행결과
>>>User1 send()
>>> Mediating User1 to User1
>>>User1 recevied User1's Message : I'm User1
>>> Mediating User1 to User2
>>>User2 recevied User1's Message : I'm User1
>>> Mediating User1 to System
>>>System can't receive messages
>>> Mediating User1 to Admin
>>>Admin can't receive messages
참조: