Clean Code

[Design Pattern] 구조 패턴 - 퍼사드 패턴

yaini 2022. 3. 14. 22:50
반응형

Facade

건물의 출입구로 이용되는 정면 외벽 부분

즉, 건물 안쪽은 복잡할지라도 사용자는 건물의 정면만 볼 수 있도록 해준다는 뜻이다.

퍼사드 패턴

클래스 라이브러리와 같은 어떤 소프트웨어의 커다란 코드 부분에 대한 간략화된 인터페이스를 제공한다.

  • 소프트웨어 라이브러리를 쉽게 사용할 수 있게 해준다.
  • 라이브러리의 세부 코드에 의존하는 일을 감소시켜 준다.

전체 코드

public class Facade {
    public static void main(String[] args) {
        Computer computer = new Computer();
        computer.on();
    }
}

class Computer {

    PowerSupply powerSupply;
    OperatingSystem operatingSystem;
    BootLoader bootLoader;

    Computer() {
        this.powerSupply = new PowerSupply();
        this.operatingSystem = new OperatingSystem();
        this.bootLoader = new BootLoader();
    }

    public void on() {
        powerSupply.on();
        if (operatingSystem.check()) {
            bootLoader.load();
            operatingSystem.execute();
        }
    }
}

class PowerSupply {
    public void on() {
        System.out.println("power on ...");
    }
}

class OperatingSystem {
    public boolean check() {
        System.out.println("check os ... ");
        return true;
    }

    public void execute() {
        System.out.println("os execute ...");
    }
}

class BootLoader {
    public void load() {
        System.out.println("bootloader load ...");
    }
}

우리는 컴퓨터가 내부적으로 어떤 동작들을 한 후 전원이 켜지는 지 알지 못해도 전원 버튼만 알면 컴퓨터를 킬 수 있다.

이와 같이 클라이언트는 Computer 클래스만 알고 있다면 PowerSuppy, OperatingSystem, BootLoader 클래스를 알지 못해도 컴퓨터를 동작 시킬 수 있다.

 

 

 

반응형