-
Notifications
You must be signed in to change notification settings - Fork 44
/
Contents.swift
95 lines (70 loc) · 2.67 KB
/
Contents.swift
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
85
86
87
88
89
90
91
92
93
94
95
import Foundation
protocol Receivable {
var id: String { get }
func receive(message: [String : Any])
}
enum MediatorError: Error {
case missingReceiver
}
protocol Mediator {
var receivers: [Receivable] { get set }
/// Sends the specified message to all the registered receivers except the sender
///
/// - Parameters:
/// - message: is a message that will be delivered to all the receivers
/// - sender: is a sender that conforms to Receivable protocol
func send(message: [String : Any], sender: Receivable)
/// Sends the specified message to the concrete receiver
///
/// - Parameters:
/// - message: is a message that will be delivered to all the receivers
/// - receiver: is a receiver that conforms to Receivable protocol
func send(message: [String : Any], receiver: Receivable) throws
}
extension Mediator {
func send(message: [String : Any], sender: Receivable) {
for receiver in receivers where receiver.id != sender.id {
receiver.receive(message: message)
}
}
func send(message: [String : Any], receiver: Receivable) throws {
guard let actualReceiver = receivers.first(where: { $0.id == receiver.id }) else { throw MediatorError.missingReceiver }
actualReceiver.receive(message: message)
}
}
class Player: Receivable {
var id: String = UUID.init().uuidString
var name: String
init(name: String) {
self.name = name
}
func receive(message: [String : Any]) {
print("Player under id: ", id, " has received a message: ", message)
}
}
class FlyingMob: Receivable {
var id: String = UUID.init().uuidString
var name: String
init(name: String) {
self.name = name
}
func receive(message: [String : Any]) {
print("FlyingMod under id: ", id, " has received a message: ", message)
}
}
class CharactersMediator: Mediator {
var receivers: [Receivable] = []
}
let player = Player(name: "Mario")
debugPrint("Player id: ", player.id)
let flyingMobBlue = FlyingMob(name: "Blue")
debugPrint("FlyingMobBlue id: ", flyingMobBlue.id)
let flyingMobYellow = FlyingMob(name: "Yellow")
debugPrint("FlyingMobYellow id: ", flyingMobYellow.id)
let flyingMobBoss = FlyingMob(name: "Boss")
debugPrint("FlyingMobBoss id: ", flyingMobBoss.id)
let mediator = CharactersMediator()
let characters: [Receivable] = [ player, flyingMobBlue, flyingMobYellow, flyingMobBoss ]
mediator.receivers += characters
mediator.send(message: ["death note" : "you are going to die soon!😄"], sender: player)
try? mediator.send(message: ["you are going to be slapped at" : (x: 10, y: 23)], receiver: player)