-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathproxy.cr
72 lines (64 loc) · 2.19 KB
/
proxy.cr
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
# The proxy pattern is a design pattern that creates a surrogate,
# or placeholder class. Proxy instances accept requests from client objects,
# pass them to the underlying object and return the results.
# The class we're going to proxy
class FighterGenerator
def initialize(@ai_level : AILv)
puts "Initializing FighterGenerator with AI level = #{@ai_level}"
end
def generate(fighter_name : String)
Fighter.new(fighter_name, @ai_level)
end
end
class Fighter
def initialize(@name : String, @ai_level : AILv)
puts "Creating new fighter #{@name} with ai level #{@ai_level}"
end
end
enum AILv
Easy
Medium
Hard
end
# The proxy class. There are several types of proxies: in this case, we use
# a *cache proxy*, which memoizes the created fighters and returns
# them without creating new ones if possible.
class FighterGeneratorProxy
def initialize(ai_level : AILv)
@generator = FighterGenerator.new(ai_level)
@cache = {} of String => Fighter
end
# The proxy's interface mimics that of the proxied class
def generate(fighter_name : String)
puts "Requested fighter #{fighter_name}"
return @cache[fighter_name] if @cache.has_key?(fighter_name)
@generator.generate(fighter_name).tap { |fighter| @cache[fighter_name] = fighter }
end
end
generator = FighterGeneratorProxy.new(AILv::Hard)
generator.generate("Sub-Zero")
generator.generate("Scorpion")
generator.generate("Johnny Cage")
generator.generate("Sub-Zero")
generator.generate("Kitana")
generator.generate("Raiden")
generator.generate("Scorpion")
generator.generate("Raiden")
generator.generate("Johnny Cage")
generator.generate("Kitana")
# Initializing FighterGenerator with AI level = Hard
# Requested fighter Sub-Zero
# Creating new fighter Sub-Zero with ai level Hard
# Requested fighter Scorpion
# Creating new fighter Scorpion with ai level Hard
# Requested fighter Johnny Cage
# Creating new fighter Johnny Cage with ai level Hard
# Requested fighter Sub-Zero
# Requested fighter Kitana
# Creating new fighter Kitana with ai level Hard
# Requested fighter Raiden
# Creating new fighter Raiden with ai level Hard
# Requested fighter Scorpion
# Requested fighter Raiden
# Requested fighter Johnny Cage
# Requested fighter Kitana