Created
January 5, 2023 16:29
-
-
Save fitomad/c57863226067f8093ea819b08166a03d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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