본문 바로가기

디자인 패턴

디자인 패턴 - 퍼사드 패턴(Facade Pattern)

1. 정의

  • 서브시스템에 있는 일련의 인터페이스를 통합 인터페이스로 묶는다.

💡 고수준의 인터페이스를 정의하므로 서브시스템을 편리하게 사용할 수 있다.

2. 예시

  • 영화를 보기위한 일련의 과정(팝콘, 조명, 스크린, 음량, …)을 하나의 과정으로 통합한다.

3. 그림

4. 클래스 다이어그램

5. 코드

public class Client {
    public static void main(String[] args){

        TheaterFade theaterFade = new TheaterFade(
                new Popcorn(),
                new Light(),
                new Screen(),
                new Amplifier()
        );

        theaterFade.watchMovie();
    }
}
public class TheaterFade {
    Popcorn popcorn;
    Light light;
    Screen screen;
    Amplifier amp;

    public TheaterFade(Popcorn popcorn, Light light, Screen screen, Amplifier amp) {
        this.popcorn = popcorn;
        this.light = light;
        this.screen = screen;
        this.amp = amp;
    }

    public void watchMovie(){
        System.out.println("영화 셋팅 시작");
        popcorn.fried();
        light.lightControl();
        screen.screenControl();
        amp.ampControl();
        System.out.println("영화 셋팅 끝\\n영화 시작");
    }
}
public class Popcorn {

    public void fried(){
        System.out.println("팝콘 튀기기");
    }
}
public class Light {

    public void lightControl(){
        System.out.println("조명 조절");
    }
}
public class Screen {

    public void screenControl(){
        System.out.println("스크린 조절");
    }
}
public class Amplifier {

    public void ampControl(){
        System.out.println("음량 조절");
    }
}

https://github.com/kang-seongbeom/design_pattern

💡 Github에서 디자인 패턴 코드를 볼 수 있습니다.

6. 설명

  • 영화를 보기위한 일련의 과정을 TheaterFacade로 통합한다.
  • Client는 TheaterFacade의 wathMovie()만 호출하면 모든 일련의 과정이 한 번에 동작한다.
  • 이와같이 여러 서브시스템을 통합하여 하나로 묶어 단순화한다.
  • 통합된 서브시스템은 pulic인 이상 독립적으로도 접근할 수 있다.