# IMediator.java
public interface IMediator {
public abstract void play();
public abstract void eat();
public abstract void talk();
}
# CommonUnit.java
public abstract class CommonUnit {
IMediator mediator;
public void setMediator(IMediator mediator) {
this.mediator = mediator;
}
public abstract void run();
}
# Unit01.java
public class Unit01 extends CommonUnit {
@Override
public void run() {
mediator.eat();
mediator.play();
}
}
# Unit02.java
public class Unit02 extends CommonUnit {
@Override
public void run() {
mediator.talk();
mediator.eat();
}
}
# ConcreteMediator.java
public class ConcreteMediator implements IMediator {
@Override
public void play() {
System.out.println("playing...");
}
@Override
public void eat() {
System.out.println("eating...");
}
@Override
public void talk() {
System.out.println("talking...");
}
}
# Main.java
public class Main {
public static void main(String[] args) {
Unit01 unit01 = new Unit01();
ConcreteMediator concreteMediator = new ConcreteMediator();
unit01.setMediator(concreteMediator);
unit01.run();
System.out.println("====================");
Unit02 unit02 = new Unit02();
unit02.setMediator(concreteMediator);
unit02.run();
}
}
# 결과
eating...
playing...
====================
talking...
eating...
댓글 영역