[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 ํจํด์ ๋จ์
- ๊ณ ์์ค ๊ฐ์ฒด๊ฐ ๋๋ฌด ๋ณต์กํด์ง ์ ์์
-> ์ ์์ค ๊ฐ์ฒด๋ค์ ์ต๋ํ ๊ฐ๋จํ๊ฒ ๊ตฌํํ๊ธฐ
-> ์ฐ๊ด์ฑ ์๋ ๊ฐ์ฒด๋ค๋ผ๋ฆฌ ๊ณ์ธตํํ๊ธฐ