let say we have some hardcoded actions like below:
enum ActionType {
case "goToScreenA"
case "goToScreenB"
case "presentAlert"
...
}
and also these actions will receive from JSON like below:
struct Action {
let type: String // one of actionTypes above
...
}
in each screen and we trigger them when user clicks on button or tableView cell. it is predefined between client and server and they both know these actions and server won't send new action that client doesn't understand.
I know we can create a switch/case or if/else for what JSON says and decide what to do based on it like this:
if action.type == "goToScreenA" {
// gotoScreenA
}else if action.type == "goToScreenB" {
// goToScreenB
}....
or Switch action.type {
case "gotoScreenA":
// gotoScreenA
case "goToScreenB":
//goToScreenB
}
BUT the question is how we can do it without switch/case in order to not break OCP role?
if we know when to trigger which actions, we can simply do this:
protocol Actionable {
func execute()
}
class ActionResolver {
func resolve(action: Actionable) {
action.execute()
}
}
struct ScreenA: Actionable {
func execute() {
}
}
and with some button click:
func onClick() {
ActionResolver(action: ScreenA())
}
the problem is we don't know when we can trigger which action and server decides about it.