-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProxy.rb
More file actions
84 lines (63 loc) · 996 Bytes
/
Proxy.rb
File metadata and controls
84 lines (63 loc) · 996 Bytes
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'etc'
class Bank
def initialize()
@money =0
end
def add(num)
@money += num
end
def use(num)
@money -= num
end
def show
puts "#{@money}"
end
end
class DefProxy
def initialize(real_obj, user_name)
@real_obj, @user_name = real_obj, user_name
end
def add(num)
@real_obj.add(num)
end
def use(num)
check_access
@real_obj.use(num)
end
def show
check_access
@real_obj.show
end
def check_access
if Etc.getlogin != @user_name
raise "Illegal access: #{@user_name}"
end
end
end
class ImProxy
def initialize()
puts "ImProxyを作成しました。Bankはまだ生成していません"
end
def use(num)
subject.use(num)
end
def add(num)
subject.add(num)
end
def show
subject.show
end
def subject
@subject || (@subject = Bank.new ; puts "Bankを作成しました")
@subject
end
end
bank = Bank.new
d1 = ImProxy.new()
d1.add(100)
d1.use(10)
d1.show
d2 = DefProxy.new(bank,"notuser")
d2.add(100)
d2.use(10)
d2.show