-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathadapter.cr
51 lines (41 loc) · 1.06 KB
/
adapter.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
# The adapter design pattern is used to provide a link between two
# incompatible types by wrapping the "adaptee" with a class that supports
# the interface required by the client.
class GameServer
def add_client(client)
client.connect "Mortal Kombat Game Server"
end
end
class DesktopClient
def open_connection(server)
puts "New TCP connection to '#{server}'"
end
end
class WebClient
def initialize(@server : String)
end
def websocket_connection
puts "New Websocket connection to '#{@server}'"
end
end
abstract class Client
abstract def connect(server)
end
class DesktopClientAdapter < Client
def initialize
@client = DesktopClient.new
end
def connect(server)
@client.open_connection(server)
end
end
class WebClientAdapter < Client
def connect(server)
WebClient.new(server).websocket_connection
end
end
server = GameServer.new
server.add_client DesktopClientAdapter.new
server.add_client WebClientAdapter.new
# New TCP connection to 'Mortal Kombat Game Server'
# New Websocket connection to 'Mortal Kombat Game Server'