본문 바로가기
좋은 코드란

퍼사드 패턴(facade pattern) 이란?

by You_mool 2023. 8. 11.
반응형

퍼사드 패턴은 복잡한 서브시스템에 대한 단순화된 인터페이스를 제공하는 디자인 패턴입니다. 이 패턴은 클라이언트가 서브시스템의 내부 복잡성을 알 필요 없이 서브시스템의 기능을 쉽게 이용할 수 있도록 도와줍니다.

class CPU {
    start() {
        console.log('CPU is starting...');
    }
}

class Memory {
    load() {
        console.log('Memory is loading...');
    }
}

class HardDrive {
    read() {
        console.log('Hard drive is reading...');
    }
}

// Facade
class Computer {
    constructor() {
        this.cpu = new CPU();
        this.memory = new Memory();
        this.hardDrive = new HardDrive();
    }

    start() {
        this.cpu.start();
        this.memory.load();
        this.hardDrive.read();
        console.log('Computer is started.');
    }
}

// 클라이언트
const myComputer = new Computer();
myComputer.start();

 

우리가 구현하려는 Computer 클래스 안에 CPU, HardDrive, Memory 코드가 다 들어있으면 코드 가독성이 더 떨어집니다.

그래서 모든 기능을 포함하도록 코드를 작성하는 것이 아니라 위와 같이 기능을 쪼개서 함수로 만들어 준뒤 그 기능들을 하나의 함수/클래스 안에 불러와서 구현하는 것이라고 생각하면 좋을 것 같아요.

반응형

'좋은 코드란' 카테고리의 다른 글

Early Exit 패턴이란?  (0) 2023.08.02