참치코더의 꿈 메모장

디자인 패턴 / 퍼사드(Facade) 패턴 본문

디자인 패턴

디자인 패턴 / 퍼사드(Facade) 패턴

참치깡 2025. 10. 12. 15:22
728x90

 

퍼사드(Facade) 패턴

 

- 퍼사드 패턴은 복잡한 시스템의 내부 구현을 숨기고, 단순한 인터페이스를 제공하는 디자인 패턴이다.

 

- 여러 하위 시스템이나 서비스들이 있는데, 클라이언트가 각각을 직접 호출하지 않고, 하나의 통합된 인터페이스로

  쉽게 접근하도록 도와준다.

 

특징

 

- 단순화 : 클라이언트가 복잡한 서브시스템을 몰라도 됨

- 캡슐화 : 내부 구조 변경 시 클라이언트 코드 영향 최소화

- 응집력 증가 : 관련 기능들을 묶어서 제공

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import org.springframework.stereotype.Service;
 
@Service
class PaymentService {
    public void pay() {
        System.out.println("Payment processed.");
    }
}
 
@Service
class InventoryService {
    public void checkStock(){
        System.out.println("Inventory checked.");
    }
}
 
@Service
class ShippingService {
    public void ship() {
        System.out.println("Order shipped.");
    }
}
 
// 퍼사드 서비스
@Service
class OrderFacadeService {
    private final PaymentService paymentService;
    private final InventoryService inventoryService;
    private final ShippingService shippingService;
 
    public OrderFacadeService(PaymentService paymentService,
                              InventoryService inventoryService,
                              ShippingService shippingService){
        
        this.paymentService = paymentService;
        this.inventoryService = inventoryService;
        this.shippingService = shippingService;    
    }
 
    public void placeOrder() {
        inventoryService.checkStock();
        paymentService.pay();
        shippingService.ship();
        System.out.println("Order completed via Spring Boot facade");
    }
}
 
// 컨트롤러에서 사용
// 컨트롤러는 OrderFacadeService만 호출하면 된다.
// 내부 서비스 호출 순서나 복잡한 로직은 퍼사드 서비스 안에서 관리한다
// Spring DI(Dependency Injection)와 함께 사용하면 의존성 관리도 깔끔해진다.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
 
@RestController
public class OrderController {
    private final OrderFacadeService orderFacadeService;
 
     public OrderController(OrderFacadeService orderFacadeService) {        
        this.orderFacadeService = orderFacadeService;
    }
 
    @GetMapping("/order")
    public String order() {
        orderFacadeService.placeOrder();
    }
}
 
 
 
 
cs

 

 - 여러 서비스 호출을 묶어 서비스 레이어에서 퍼사드를 구현하고, 컨트롤러는 단순 호출한다. 

728x90
Comments