-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathProxy.kt
35 lines (32 loc) · 1.01 KB
/
Proxy.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
31
32
33
34
35
package org.vld.sdp.structural
/**
* Payment Subject interface to be implemented by the real object [Account] and the proxy [PaymentProxy]
*/
interface Payment {
fun pay(amount: Double)
}
/**
* Account real Subject interface implementation
*/
class Account(var balance: Double) : Payment {
/**
* Subtracts the amount directly from the balance without any balance/amount validation
*/
override fun pay(amount: Double) {
balance -= amount
}
}
/**
* Payment Subject interface Proxy implementation
*/
class PaymentProxy(private val account: Account) : Payment {
/**
* Checks the account balance and delegates the [Payment] operation to the [Account] real object
* if the balance can satisfy the payment [amount] otherwise throws IllegalArgumentException
*/
override fun pay(amount: Double) {
if (account.balance < amount)
throw IllegalArgumentException("Not enough balance to satisfy the payment amount: $amount")
else account.pay(amount)
}
}