여러 객체들 간의 의사소통을 하는 역할을 추상화시키는 패턴
직접 의사소통을 하고 있는 컴포넌트들은 결합도가 높아 코드를 수정하거나 테스트 하기 어렵다.
public class Guest {
private Restaurant restaurant = new Restaurant();
private CleanService cleanService = new CleanService();
public void dinner() {
restaurant.dinner(this);
}
public void getTower(int numberOfTower) {
cleaningService.getTower(this, numberOfTower);
}
}
public class Hotel {
public static void main(String[] args) {
Guest guest = new Guest();
guest.getTower(3);
guest.dinner();
Restaurant restaurant = new Restaurant();
restaurant.clean();
}
}
Mediator
public class FrontDesk {
private CleaningService cleaningService = new CleaningService();
public void getTowers(Guest guest, int numberOfTowers) {
cleaningService.getTowers(guest.getId(), numbersOfTowers);
}
public String getRoomNumberFor(Integer guestId) {
return "1111";
}
public void dinner(Guest guest, LocalDateTime dateTime) {
restaurant.dinner(guest.getId(), dateTime);
}
}
public class Guest {
private Integer id;
private FrontDesk frontDesk = new FrontDesk();
public Integer getId() {
return id;
}
public void dinner(LocalDateTime dateTime) {
this.frontDesk.dinner(this, dateTime);
}
public void getTowers(int numberOfTowers) {
this.frontDesk.getTowers(numberOfTowers)
}
}
public class CleaningService {
private FrontDesck frontDesk = new FrontDesk();
public void getTowers(Integer guestId, int numberOfTowers) {
String roomNumber = this.frontDesk.getRoomNumberFor(guestId);
}
}
여러개의 컴포넌트를 연결시켜 핸들러 매핑을 여러개 둠.