Skip to content

Instantly share code, notes, and snippets.

@fitomad
Created January 5, 2023 16:29
Show Gist options
  • Save fitomad/c57863226067f8093ea819b08166a03d to your computer and use it in GitHub Desktop.
Save fitomad/c57863226067f8093ea819b08166a03d to your computer and use it in GitHub Desktop.
final class MessageManager {
private var handlers: [MessageHandler]
init() {
handlers = [
OrionMessageHandler(),
VoyagerMessageHandler(),
PerseveranceMessageHandler()
]
buildHandlersChain()
}
private func buildHandlersChain() {
for index in 0 ..< handlers.count - 1 {
handlers[index].nextHandler = handlers[index + 1]
}
}
func process(_ message: String) {
do {
try self.handlers.first?.process(message)
} catch MessageError.unknown {
print("🚨 El mensaje no pertenece a ninguno de los handler registrados")
} catch let error {
print("ERROR: \(error.localizedDescription)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment