-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCommand.kt
30 lines (24 loc) · 875 Bytes
/
Command.kt
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
package org.vld.sdp.behavioral
/**
* Command interface order a dish
*/
typealias Order = () -> String
/**
* Command interface implementation and Receiver/Implementor. Command stores the request arguments (name) in a closure
*/
fun cookStarter(name: String): Order = { "$name Starter" }
fun cookMainCourse(name: String): Order = { "$name Main Course" }
fun cookDessert(name: String): Order = { "$name Dessert" }
/**
* Invoker implementation keeps the list of pending orders
*/
class Waiter(private val pendingOrders: MutableList<Order> = mutableListOf()) {
/**
* Accepts [orders] and queues the orders for execution
*/
fun acceptOrder(vararg orders: Order) = pendingOrders.addAll(orders)
/**
* Serves orders by executing the orders through the Command interface
*/
fun serveOrders(): List<String> = pendingOrders.map { it() }
}