-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path07_adapter_sample1.rb
More file actions
54 lines (43 loc) · 896 Bytes
/
07_adapter_sample1.rb
File metadata and controls
54 lines (43 loc) · 896 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
# Targetにはないインターフェイスを持つ (Adaptee)
class OldPrinter
def initialize(string)
@string = string.dup
end
def show_with_paren
puts "(#{@string})"
end
def show_with_aster
puts "*#{@string}*"
end
end
# 利用者(Client)へのインターフェイス (Target)
class Printer
def initialize(obj)
@obj = obj
end
def print_weak
@obj.print_weak
end
def print_strong
@obj.print_strong
end
end
# Targetが利用できるインターフェイスに変換 (Adapter)
class Adapter
def initialize(string)
@old_printer = OldPrinter.new(string)
end
def print_weak
@old_printer.show_with_paren
end
def print_strong
@old_printer.show_with_aster
end
end
# ===========================================
# 利用者(Client)
p = Printer.new(Adapter.new("Hello"))
p.print_weak
#=> (Hello)
p.print_strong
#=> *Hello*