-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex21.4.lua
49 lines (38 loc) · 1.29 KB
/
ex21.4.lua
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
local map = {}
local AccountState = { balance = 0 }
AccountState.__index = AccountState
local AccountInterface = { }
AccountInterface.__index = AccountInterface
function AccountInterface:deposit (v)
local state = map[self]
state.balance = state.balance + v
end
function AccountInterface:get_balance ()
return map[self].balance
end
function AccountInterface:withdraw (v)
map[self].balance = map[self].balance - v
end
function AccountInterface:new (o)
o = o or {}
setmetatable(o, AccountState)
local proxy = {}
setmetatable(proxy, AccountInterface)
map[proxy] = o
return proxy
end
local account = AccountInterface:new()
assert(0 == account:get_balance())
account:deposit(100)
assert(100 == account:get_balance())
account:withdraw(200)
assert(-100 == account:get_balance())
--[=[
Using the proxy representation, each object has to be represented by two
separate parts: the interface and the state. The interface part and the state
part has to have its own inheritance. It also requires a mapping table which
maps the interface part to its corresponding state part.
The good point here is that this implementation make it explicit that the
interface and state are separate. The bad point is that it is complicated and
verbose.
--]=]