-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDualRepresentationAccount.lua
53 lines (41 loc) · 1.01 KB
/
DualRepresentationAccount.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
50
51
52
53
local Account = { balance = 0 }
Account.__index = Account;
function Account:Deposit(value)
self.balance = self.balance + value;
end
function Account:WithDraw(value)
self.balance = self.balance - value;
end
function Account:GetBalance()
return self.balance;
end
local ProxyToAccount = {}
function Account:New(o)
o = o or {}
setmetatable(o, Account)
return o
end
local AccountProxy = {}
AccountProxy.__index = AccountProxy
function AccountProxy:Deposit(value)
ProxyToAccount[self]:Deposit(value)
end
function AccountProxy:WithDraw(value)
ProxyToAccount[self]:WithDraw(value)
end
function AccountProxy:GetBalance()
return ProxyToAccount[self]:GetBalance()
end
function AccountProxy:New(o)
o = o or {}
local new_account = Account:New()
ProxyToAccount[o] = new_account
setmetatable(o, AccountProxy)
return o
end
local account_proxy = AccountProxy:New()
account_proxy:Deposit(100)
print(account_proxy:GetBalance())
account_proxy:WithDraw(50)
print(account_proxy:GetBalance())
print(Account:GetBalance())