[TIL] ๐ŸŒผ24/04/02๐ŸŒผ#Facade ํŒจํ„ด

0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
76/104
post-thumbnail
post-custom-banner

[TIL] ๐ŸŒผ24/04/02๐ŸŒผ

Facade ํŒจํ„ด

  • SOLID ์›์น™ ์ค‘ Single Responsibility Principle(SRP)์˜ ์˜ˆ์‹œ๋กœ Facade ํŒจํ„ด์ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณด๊ณ , Facade ํŒจํ„ด์— ๋Œ€ํ•ด ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด์•˜๋‹ค.

๐Ÿ“Œ์ฐธ๊ณ ์ž๋ฃŒ

  • Facade ํŒจํ„ด์ด๋ž€?
    • ์ผ๋ จ์˜ ์ €์ˆ˜์ค€ ์ธํ„ฐํŽ˜์ด์Šค๋“ค์„ ํ•˜๋‚˜์˜ ๊ณ ์ˆ˜์ค€ ์ธํ„ฐํŽ˜์ด์Šค(= ํ†ตํ•ฉ ์ธํ„ฐํŽ˜์ด์Šค)๋กœ ๋ฌถ์–ด์ฃผ๋Š” ํŒจํ„ด
    • ํด๋ผ์ด์–ธํŠธ ๊ฐ์ฒด๋Š” ์—ฌ๋Ÿฌ ์ €์ˆ˜์ค€ ์ธํ„ฐํŽ˜์ด์Šค์— ๋Œ€ํ•ด ์˜์กด์„ฑ์ด ๋‚ฎ์•„์ง„๋‹ค
  • ์˜ˆ) Facade ํŒจํ„ด์„ ์ ์šฉ ์ „
// ์ €์ˆ˜์ค€ ์ธํ„ฐํŽ˜์ด์Šค 
interface Waiter {     
     fun takeOrder()     
     fun sendOrderToKitchen()     
     fun serveFood() 
}
interface Kitchen {    
    fun prepareFood()      
    fun callWaiter()      
    fun washesDishes()  
}
// ์ €์ˆ˜์ค€ ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ ํด๋ž˜์Šค
class WaiterImpl : Waiter {    
     override fun takeOrder() {       
         println("Taking order from customer")    
     }    
     override fun sendOrderToKitchen() {      
         println("Sending order to kitchen")    
     }    
     override fun serveFood() {       
         println("Serving food to customer")    
     }
}
class KitchenImpl : Kitchen {    
     override fun prepareFood() {         
         println("Preparing food")     
     }     
     override fun callWaiter() {         
         println("Calling waiter")     
     }
     override fun washesDishes() {        
         println("Washing dishes")     
     }
}
// ํด๋ผ์ด์–ธํŠธ ํด๋ž˜์Šค
class Customer {
   val waiter: Waiter = WaiterImpl()  
   val kitchen: Kitchen  = KitchenImpl() 
   
   fun main() {       
     orderFood("ORD123456789")
   } 
    
   private fun orderFood(orderId: String) {       
      waiter.takeOrder()     
      waiter.sendOrderToKitchen()       
      kitchen.prepareFood()       
      kitchen.callWaiter()       
      waiter.serveFood()       
      kitchen.washesDishes()   
   }  
}
  • ์˜ˆ) Facade ํŒจํ„ด ์ ์šฉ ํ›„
// ์ €์ˆ˜์ค€ ์ธํ„ฐํŽ˜์ด์Šค๋“ค์„ ํ†ตํ•ฉํ•˜๋Š” ๊ณ ์ˆ˜์ค€ ์ธํ„ฐํŽ˜์ด์Šค
interface OrderFood{
	fun orderFood(orderId: String)
}
// ๊ณ ์ˆ˜์ค€ ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ ํด๋ž˜์Šค
class OrderFoodImpl : OrderFood {
   val waiter: Waiter = WaiterImpl()  
   val kitchen: Kitchen  = KitchenImpl()  
 
   override fun orderFood(orderId: String) {       
      waiter.takeOrder()     
      waiter.sendOrderToKitchen()       
      kitchen.prepareFood()       
      kitchen.callWaiter()       
      waiter.serveFood()       
      kitchen.washesDishes()   
   }  
}
// ํด๋ผ์ด์–ธํŠธ ๊ฐ์ฒด
class Customer {
   private val orderFood = OrderFoodImpl()     
   fun main() {       
     orderFood.orderFood("ORD123456789")
   } 
}
  • Facade ํŒจํ„ด์˜ ์žฅ์ 
    • ๋‚ด๋ถ€์˜ ๋ณต์žกํ•œ ๋™์ž‘์„ ๋ชจ๋‘ Wrapping ์‹œ์ผœ ์™ธ๋ถ€์— ๊ฐ„๋‹จํ•œ ๋™์ž‘๋งŒ ๊ณต๊ฐœ
      (= ์ž˜ ์„ค๊ณ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค์˜ ํŠน์ง•)
  • Facade ํŒจํ„ด์˜ ๋‹จ์ 
    • ๊ณ ์ˆ˜์ค€ ๊ฐ์ฒด๊ฐ€ ๋„ˆ๋ฌด ๋ณต์žกํ•ด์งˆ ์ˆ˜ ์žˆ์Œ
      -> ์ €์ˆ˜์ค€ ๊ฐ์ฒด๋“ค์„ ์ตœ๋Œ€ํ•œ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜๊ธฐ
      -> ์—ฐ๊ด€์„ฑ ์žˆ๋Š” ๊ฐ์ฒด๋“ค๋ผ๋ฆฌ ๊ณ„์ธตํ™”ํ•˜๊ธฐ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€