I have a Command object-hierarchy that is being serialized and then deserialized (manually) in two sides of a connection, I'm assigning every Inherited object a different enum value (initializes the abstract Command in a different manner...) so I can know what I'm receiving.
Anyway... Now I'm working on a polymorphic handle of a command. I could just write a virtual method called "Handle", and every other Command would operate differently, but then I would have to supply some resources, like database connection, and the functionality to send a response to the sender, and that would of course be a major breach of encapsulation concept of OOP. On the other hand, My current design uses a switch statement on the enum value, and operates a handle method which is on the interpreter level, can talk to the Socket module, and the Database module.
Which one of the designs is better?
A. Virtual Handle method - possibly prettier polymorphic code, but with the need to provide access to the Socket and Database modules - bad encapsulation.
B. Ugly switch statement - semi-polymorphic code... Visual Studio Code Metrics doesn't seem to be very fond of it (<60 in maintainability and more cases will be added in the future...), but encapsulation is preserved...
Maybe there's a tweak that I'm missing? What would you guys recommend?