시그마 삽질==six 시그마

Facade pattern 본문

프로그래밍/디자인패턴

Facade pattern

Ethan Matthew Hunt 2020. 4. 30. 08:33

The facade pattern (also spelled façade) is a software-design pattern commonly used in object-oriented programming. Analogous to a facade in architecture, a facade is an object that serves as a front-facing interface masking more complex underlying or structural code. A facade can:
-improve the readability and usability of a software library by masking interaction with more complex components behind a single (and often simplified) API
-provide a context-specific interface to more generic functionality (complete with context-specific input validation)
-serve as a launching point for a broader refactor of monolithic or tightly-coupled systems in favor of more loosely-coupled code

source: https://en.wikipedia.org/wiki/Facade_pattern

파사드는 복잡한 구조의 코드를 숨기는 인터페이스로 사용되는 객체를 의미

보통 추상화된 고차원 객체의  메소드안에 여러 저차원 객체의 메소드를 숨겨 simple하게 구성

클라이언트는 복잡한 내부구조를 알 필요가 없음

 

https://en.wikipedia.org/wiki/Facade_pattern

 

/* Complex parts */

class CPU {
    public void freeze() { ... }
    public void jump(long position) { ... }
    public void execute() { ... }
}

class HardDrive {
    public byte[] read(long lba, int size) { ... }
}

class Memory {
    public void load(long position, byte[] data) { ... }
}

/* Facade */

class ComputerFacade {
    private final CPU processor;
    private final Memory ram;
    private final HardDrive hd;

    public ComputerFacade() {
        this.processor = new CPU();
        this.ram = new Memory();
        this.hd = new HardDrive();
    }

    public void start() {
        processor.freeze();
        ram.load(BOOT_ADDRESS, hd.read(BOOT_SECTOR, SECTOR_SIZE));
        processor.jump(BOOT_ADDRESS);
        processor.execute();
    }
}

/* Client */

class You {
    public static void main(String[] args) {
        ComputerFacade computer = new ComputerFacade();
        computer.start();
    }
}


souce :https://en.wikipedia.org/wiki/Facade_pattern

 

Comments