구조패턴 중 하나인 퍼사드 패턴을 알아봅시다.
의도
퍼사드 패턴은 라이브러리에 대한, 프레임워크에 대한 또는 다른 클래스들의 복잡한 집합에 대한 단순화된 인터페이스를 제공하는 구조적 디자인 패턴입니다. 출처
쉽게 풀어 설명하면 내부 복잡한 구조와 로직에 대해 알 필요없이 단순화된 인터페이스만 제공하여 사용하면 되는 구조입니다.
문제
퍼사드 패턴이 나오게 된 배경에 대해 알아봅시다.
정교한 라이브러리나 프레임워크에 속하는 광범위한 객체들의 집합으로 어떠한 비즈니스 로직을 만든다고 상상해봅시다.
일반적으로 필요한 객체들을 초기화하고 객체들이 서로 상호작용하게 하며 로직에 따라 실행 순서를 정하는 등 작업을 수행합니다.
비즈니스 로직을 구현하였지만 로직을 사용하는 외부 클래스들이 세부 구현 사항들과 밀접하게 결합되어 있게 됩니다.
해결책
퍼사드는 복잡한 하위 시스템에 대해 간단한 인터페이스를 제공하는 클래스입니다. 하위 시스템과 직접 작업하는 것과 비교하면 퍼사드는 제한된 기능을 제공합니다. 복잡한 하위 시스템에 대해서 상위 시스템이 알 필요없게 하며 꼭 필요한 제한된 기능만 제공하며 결합도를 낮출 수 있습니다.
예시코드
아래 예시는 비디오 변환에 대한 내용입니다.
public interface Codec {
}
public class MPEG4CompressionCodec implements Codec{
public String type = "mp4";
}
public class OggCompressionCodec implements Codec {
public String type = "ogg";
}
public class CodecFactory {
public static Codec extract(VideoFile file) {
String type = file.getCodecType();
if (type.equals("mp4")) {
System.out.println("CodecFactory: extracting mpeg audio...");
return new MPEG4CompressionCodec();
} else {
System.out.println("CodecFactory: extracting ogg audio...");
return new OggCompressionCodec();
}
}
}
코덱(Codec) : compressor/decompressor 라고 하며 디지털 비디오와 오디오를 위한 압축/복원 기술
코덱 인터페이스, mp4, ogg 2종류의 코덱, 코덱팩토리 클래스를 만들었습니다.
public class VideoFile {
private String name;
private String codecType;
public VideoFile(String name) {
this.name = name;
this.codecType = name.substring(name.indexOf(".") + 1);
}
public String getName() {
return name;
}
public String getCodecType() {
return codecType;
}
}
public class BitrateReader {
public static VideoFile read(VideoFile file, Codec codec) {
System.out.println("BitrateReader: reading file...");
return file;
}
public static VideoFile convert(VideoFile buffer, Codec codec) {
System.out.println("BitrateReader: writing file...");
return buffer;
}
}
public class AudioMixer {
public File fix(VideoFile file) {
System.out.println("AudioMixer: fixing audio...");
return new File("tmp");
}
}
VideoFile과 Bitrate(비트레이트)Reader, AudioMixer 클래스를 만들었습니다.
비디오 파일을 통해 파일을 읽고 변환하기 위한 클래스들입니다.
public class VideoConversionFacade {
public File convertVideo(String fileName, String format) {
System.out.println("VideoConversionFacade: conversion started");
VideoFile file = new VideoFile(fileName);
Codec sourceCodec = CodecFactory.extract(file);
Codec destinationCodec;
if (format.equals("mp4")) {
destinationCodec = new MPEG4CompressionCodec();
} else {
destinationCodec = new OggCompressionCodec();
}
VideoFile buffer = BitrateReader.read(file, sourceCodec);
VideoFile intermediateResult = BitrateReader.convert(buffer, destinationCodec);
File result = (new AudioMixer()).fix(intermediateResult);
return result;
}
}
public class Demo {
public static void main(String[] args) {
VideoConversionFacade converter = new VideoConversionFacade();
File mp4Video = converter.convertVideo("youtubevideo.ogg", "mp4");
// ...
}
}
마지막으로 비디오변환퍼사드 클래스와 변환작업을 실행할 Demo 클래스를 만들었습니다.
퍼사드 클래스 내부에서 위에서 비디오 변환을 위해 생성한 여러 하위 시스템 클래스들을 초기화하고 로직을 구현하였습니다.
Demo 클래스 입장에서는 videoFile 변환작업을 하고 싶은데 내부 로직에 대해 알 필요가 없습니다.
단지 퍼사드 클래스의 converVideo 기능을 사용하여 원하는 결과를 얻어낼 수 있습니다.
구조

장점
- 복잡한 하위 시스템으로부터 코드를 분리할 수 있습니다.
단점
- 퍼사드 클래스가 모든 클래스가 결합되어 전지전능한 객체가 될 수 있습니다.
'리팩토링 > 디자인패턴' 카테고리의 다른 글
| [디자인 패턴] 데코레이터 패턴 (3) | 2022.12.26 |
|---|---|
| [디자인 패턴] 템플릿 메서드 패턴 (1) | 2022.12.10 |
| [디자인패턴] 어댑터 패턴 (1) | 2022.11.14 |
| [디자인패턴] 팩토리 메서드 패턴, 추상 팩토리 패턴 (1) | 2022.10.30 |
| [디자인패턴] 싱글턴 패턴 (2) | 2022.10.24 |