퍼사드 패턴(Facade Pattern)
디자인패턴 공부하기
  • JavaScript

퍼사드 패턴(Facade Pattern)

퍼사드 패턴이란 복잡하고 다양한 서브시스템을 인터페이스로 감싸 간단하게 만드는 것이다.

간단한 예제

집에서 영화를 본다고 가정해보자.

집에서 영화를 한 편 보기위해서는 여러가지 해야하는 일들이 있다.

1. 스크린을 내린다.
2. 빔을 켠다
3. 보고싶은 영화를 재생한다.
4. 팝콘을 튀긴다.
5. 맥주를 가져온다.

이와같은 일들을 해야하고, 이들의 작업을 프로그램으로 구성하면 대략 이런 모습이 될것이다.

screan.down();
projector.on();
movie.play();
popcorn.pop();
beer.pop();

비교적 간단했다고 생각했다.

자, 이제 영화를 다 보았다.
역순으로 다시 진행해줘야한다.

또 한줄한줄 적어나갈것인가..

이럴때 간편하게 사용할 수 있는게 퍼사드 패턴이다.

class PlayMovie {
    constructor (screen, projector, movie, popcorn, beer) {
        this.screen = screen;
        this.projector = projector;
        this.movie = movie;
        this.popcorn = popcorn;
        this.beer = beer;
    }

    play() {
        screen.down();
        projector.on();
        movie.play();
        popcorn.pop();
        beer.pop();
    }

    stop() {
        screen.up();
        projector.off();
        movie.stop();
        popcorn.off();
        beer.off();
    }
}


const screen = new Screen();
const projector = new Projector();
const movie = new Movie();
const popcorn = new Popcorn();
const beer = new Beer();

const playMovie = new PlayMovie(screen, projector, movie, popcorn, beer);
playMovie.play();
playMovie.stop();

play()stop() 으로 간단하게 처리할 수 있다.

어떠한 시스템에서 일련의 복잡한 작업을 단순화하고 통합한 클래스를 만들어 퍼사드 패턴을 완성하였다.
이러한 패턴은 클라이언트와 서비시스템이 서로 긴밀하게 연결되지 않아도 되고, 최소 지식 원칙을 지키는데에도 도움을 준다.

최소 지식 원칙 : 정말 친한 친구끼리만 공유하라!