0

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.

Hoven
  • 563
  • 1
  • 5
  • 24
  • If you want to instanciate object of type coming from JSON you can check with this question : Can we create instance of a data type from string data types names in swift ](https://stackoverflow.com/questions/60535979/can-we-create-instance-of-a-data-type-from-string-data-types-name-in-swift) – Ptit Xav Sep 22 '21 at 17:39
  • I don't understand how `ActionType` comes into play there... can you clarify at which point do you expect to use the action you received in JSON? – timbre timbre Sep 22 '21 at 20:23
  • I don't see this as a violation of the open-closed principle. It is more a question of coupling between your client and server. Regardless of your implementation detail, it is impossible for your client to support a new function from your server without code changes. Clients and servers are often closely coupled in this way. You can, of course, come in such a way that the client fails gracefully; I.E. That it ignores a function from the sever that it cannot handle. The only way you could avoid this is if the server actually sent new code to the client for it to execute – Paulw11 Sep 22 '21 at 20:49
  • This is not permitted under App Store rules. You could have the server send some sequence of primitive operations that combined to perform the function required and have your client implement this set of primitives and execute them as per the sequence sent from the server. This seems like it might be overly complicated. Your problem looks like a basic router class – Paulw11 Sep 22 '21 at 20:50
  • @KirilS. if we are in some screen for example A, server says (by json) the action of button click must be "goToScreenB" or "presentAlert" and when we parse the json we understand which action must trigger right now. server can change actions anytime – Hoven Sep 23 '21 at 06:33
  • @Paulw11 server and client won't have any action that is not defined! the problem is about navigation. server decides where screen user must go when a button clicks. My question is how to implement this without if/else or switch/case? how can I convert server action (some string inside json) into a triggerable action whiteout if else check. tell me if you need more clarifications – Hoven Sep 23 '21 at 06:38
  • There's not enough code in your question to make an informed statement about the OCP state. How are you planning to use those actions? Who will process them? – Cristik Sep 25 '21 at 18:26

0 Answers0