디자인 패턴

Facade Pattern - 구조

iksadnorth 2023. 7. 30. 15:34

👣 문제 상황

자주 사용되거나 너무 복잡한 일련의 작업 묶음이 있다면
이것을 추상화해 내부 작업들에 대한 의존도를 낮추거나 
단순히 하나의 메소드로 엮어서 사용하기 편하게 만들 필요가 생길 수 있다.

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

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

class HardDrive {
	public byte[] read(long lba, int size) {
		...
	}
}
class You {
    public static void main(String[] args) throws ParseException {
        // 컴퓨터 Turn On 과정.
        CPU cpu = new CPU();
        Memory memory = new Memory();
        HardDrive hardDrive = new HardDrive();
        cpu.freeze();
        memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));
        cpu.jump(BOOT_ADDRESS);
        cpu.execute();
    }
}

위와 같이 결국 하나의 기능을 수행하고 싶은 것은 컴퓨터를 켜는 행위일 뿐인데 
이것을 위한 과정이 너무 복잡하고 의존도가 강해 유지보수가 어렵다.

목적 : 일정 단위의 작업을 추상화를 통해 의존도를 낮추고 간편하게 사용하게 하고 싶을 때

👣 해결 방법

/* Complex parts */

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

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

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

/* Façade */

class Computer {
    public void startComputer() {
        CPU cpu = new CPU();
        Memory memory = new Memory();
        HardDrive hardDrive = new HardDrive();
        cpu.freeze();
        memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));
        cpu.jump(BOOT_ADDRESS);
        cpu.execute();
    }
}

/* Client */

class You {
    public static void main(String[] args) throws ParseException {
        Computer facade = new Computer();
        facade.startComputer();
    }
}

 

👣 결과

결국, 위와 같은 과정을 통해 복잡한 내부 과정을 숨기고 하나의 메서드로 사용할 수 있게 만들었기에
유지보수가 편하고 사용하기도 편하다.

'디자인 패턴' 카테고리의 다른 글

컴포지트 패턴 적용 - SecurityFilterChain 적용  (0) 2023.08.30
Composite Pattern - 구조  (0) 2023.07.30
객체 지향 프로그래밍  (0) 2023.07.21
Programming Paradigm  (0) 2023.07.19
MVC Pattern - 아키텍쳐  (0) 2023.07.19