반응형

구조 패턴 - Facade

 

해결하려는 문제

  • 복잡한 서브시스템에 인터페이스를 단순하게 구성하고 할 때
  • 시스템 간의 결합도를 줄이고자 할 때
  • 서브 시스템을 계층화 시키고 싶을 때

 

 

 


파사트 패턴 예제

  • 컴퓨터 부팅 로직
public class Main {
    public static void main(String[] args) {
        Power power = new Power();
        
        if (!power.checkElectric())
            System.out.println("전력이 비정상");
            System.out.println("전력이 비정상");
            return;
        }
        
        Mainboard mainboard = new mainboard();
        ReadOnlyMemory rom = mainboard.supply(power);
        Bios bios = rom.getBios();
        if (bios.post()) {
            System.out.println("장치가 불량임");
            return;
        }
        
        BootLoader bootloader = bios.getBootLoader(rom);
        HardDiskDirve hdd = new HardDiskDirve();
        
        OperationSystem os = bootLoader.findOperationSystem(hdd);
        RandomAccessMemory ram = new RandomAccessMemory();
        os.bootStrap();
    }
}
public class Power {
    public boolean checkElectric() {
        return true;
}
public class Mainboard {
    public ReadOnlyMemory supply(Power power) {
        return new ReadOnlyMemory();
    }
}
public class ReadOnlyMemory {
}
public class ReadOnlyMemory {
    public Bios getBios() {
        return new Bios();
    }
}
public class Bios {
    public boolean post() {
        return true;
    }
    
    public BootLoader getBootLoader(ReadOnlyMemory rom) {
        return new BootLoader();
    }
}
public class BootLoader {
    public OperationSystem findOperationSystem(HardDiskDirve hdd) {
        return new OperationSystem;
    }
}
public class HardDiskDirve {
}
public class OperationSystem {
    public void bootStrap(RandomAccessMemory ram) {
        System.out.println("운영체제 부팅시작");
    }
}
public class RandomAccessMemory {
}

 

 


파사드 패턴 적용

  • 현실세계에서의 부팅로직
  • Computer() 메소드의 turnOn()을 호출하는 방식
public class Main {
    public static void main(String[] args) {
    
        Computer computer = new Computer();
        computer.turnOn();
        /*       
        Power power = new Power();
        
        if (!power.checkElectric())
            System.out.println("전력이 비정상");
            System.out.println("전력이 비정상");
            return;
        }
        
        Mainboard mainboard = new mainboard();
        ReadOnlyMemory rom = mainboard.supply(power);
        Bios bios = rom.getBios();
        if (bios.post()) {
            System.out.println("장치가 불량임");
            return;
        }
        
        BootLoader bootloader = bios.getBootLoader(rom);
        HardDiskDirve hdd = new HardDiskDirve();
        
        OperationSystem os = bootLoader.findOperationSystem(hdd);
        RandomAccessMemory ram = new RandomAccessMemory();
        os.bootStrap();
        */
    }
}
  • Computer()가 파사드 역할을 수행
  • turnOn()을 이용하여 복잡한 내부와 상호작용을 숨김
public class Computer {
    private Power power = new Power();
    private Mainboard mainboard = new mainboard();
    private HardDiskDirve hdd = new HardDiskDirve();
    private RandomAccessMemory ram = new RandomAccessMemory();
        
    public void turnOn() {
        
        if (!power.checkElectric())
            System.out.println("전력이 비정상");
            System.out.println("전력이 비정상");
            return;
        }
        
        
        ReadOnlyMemory rom = mainboard.supply(power);
        Bios bios = rom.getBios();
        if (bios.post()) {
            System.out.println("장치가 불량임");
            return;
        }
        
        BootLoader bootloader = bios.getBootLoader(rom);
        
        OperationSystem os = bootLoader.findOperationSystem(hdd);
        
        os.bootStrap();
    }
}

 

 


반응형

+ Recent posts