Refactor long switch statement

前端 未结 6 1436
我寻月下人不归
我寻月下人不归 2020-12-06 06:09

I\'m program in c# which you controlling by dictating command so now i have a long switch statement. Something like

switch (command)

{
    case \"Show comma         


        
6条回答
  •  悲哀的现实
    2020-12-06 06:42

    Here's what you can do here. You can create an interface [ICommand] where you can place a common function [eg: Execute].

    Then you just needs to initiate that member with appropriate type and call the Execute function. This might include more functions in the future and is thus extended.

    Also, you can create a factory method where you can pass the parameter and get the appropriate class to work with.

    Hope that helps.

提交回复
热议问题