2022-07-11-중재자패턴-2부-패턴적용하기
본문 바로가기
CS Study/디자인패턴

2022-07-11-중재자패턴-2부-패턴적용하기

by KyeongMin 2022. 7. 18.
728x90
반응형

01.중재자패턴 적용하기

01.1 FrontDesk

import java.time.LocalDateTime;

public class FrontDesk {

    private CleaningService cleaningService = new CleaningService();

    public void getTowers(Guest guest, int numberOfTowers) {
        cleaningService.getTowers(guest.getId(), numberOfTowers);
    }

    public String getRoomNumberFor(Integer guestId) {
        return "1111";
    }

    public void dinner(Guest guest, LocalDateTime dateTime) {
        restaurant.dinner(guest.getId(), dateTime);
    }
}
  • Mediater이기 때문에 college를 다 알고 있어야함
  • 이거 넘길때 그냥 게스트 전체를 넘기면 안되고 , 필요한 정보만 넘겨야함

01.2 CleaningService

  • before
  • public class CleaningService {
        public void clean(Gym gym) {
            System.out.println("clean " + gym);
        }
    
        public void getTower(Guest guest, int numberOfTower) {
            System.out.println(numberOfTower + " towers to " + guest);
        }
    
        public void clean(Restaurant restaurant) {
            System.out.println("clean " + restaurant);
        }
    }
  • after
  • public class CleaningService {
    
        private FrontDesk frontDesk = new FrontDesk();
    
        public void getTowers(Integer guestId, int numberOfTowers) {
            String roomNumber = this.frontDesk.getRoomNumberFor(guestId);
            System.out.println("provide " + numberOfTowers + " to " + roomNumber);
        }
    }
  • 룸넘버가 필요하면 FrontDesk에서 가져올 수 있게 선언하면됨

01.3 Guest

  • before
  • public class Guest {
    
        private Restaurant restaurant = new Restaurant();
    
        private CleaningService cleaningService = new CleaningService();
    
        public void dinner() {
            restaurant.dinner(this);
        }
    
        public void getTower(int numberOfTower) {
            cleaningService.getTower(this, numberOfTower);
        }
    
    }
  • after
  • import java.time.LocalDateTime;
    
    public class Guest {
    
        private Integer id;
    
        private FrontDesk frontDesk = new FrontDesk();
    
        public void getTowers(int numberOfTowers) {
            this.frontDesk.getTowers(this, numberOfTowers);
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    }

02.저녁을 추가하는 상황

02.1 Restaurant

import java.time.LocalDateTime;

public class Restaurant {
    public void dinner(Integer id, LocalDateTime dateTime) {

    }
}

02.2 FrontDesk

  • before
  • import java.time.LocalDateTime;
    
    public class FrontDesk {
    
        private CleaningService cleaningService = new CleaningService();
    
        public void getTowers(Guest guest, int numberOfTowers) {
            cleaningService.getTowers(guest.getId(), numberOfTowers);
        }
    
        public String getRoomNumberFor(Integer guestId) {
            return "1111";
        }
    }
  • after
  • import java.time.LocalDateTime;
    
    public class FrontDesk {
    
        private CleaningService cleaningService = new CleaningService();
    
        private Restaurant restaurant = new Restaurant();
    
        public void getTowers(Guest guest, int numberOfTowers) {
            cleaningService.getTowers(guest.getId(), numberOfTowers);
        }
    
        public String getRoomNumberFor(Integer guestId) {
            return "1111";
        }
    
        public void dinner(Guest guest, LocalDateTime dateTime) {
            restaurant.dinner(guest.getId(), dateTime);
        }
    }

02.3 Guest

  • before
  • import java.time.LocalDateTime;
    
    public class Guest {
    
        private Integer id;
    
        private FrontDesk frontDesk = new FrontDesk();
    
        public void getTowers(int numberOfTowers) {
            this.frontDesk.getTowers(this, numberOfTowers);
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    }
  • after
  • import java.time.LocalDateTime;
    
    public class Guest {
    
        private Integer id;
    
        private FrontDesk frontDesk = new FrontDesk();
    
        public void getTowers(int numberOfTowers) {
            this.frontDesk.getTowers(this, numberOfTowers);
        }
    
        private void dinner(LocalDateTime dateTime) {
            this.frontDesk.dinner(this, dateTime);
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    }
728x90
반응형

댓글