Programming against an enum in a switch statement, is this your way to do?

前端 未结 12 2279
青春惊慌失措
青春惊慌失措 2020-12-24 09:15

Look at the code snippet:

This is what I normally do when coding against an enum. I have a default escape with an InvalidOperationException (I do not use ArgumentExc

12条回答
  •  粉色の甜心
    2020-12-24 09:54

    I never use switch. The code similar to what you show was always a major pain point in most frameworks I used -- unextensible and fixed to a limited number of pre-defined cases.

    This is a good example of what can be done with simple polymorphism in a nice, clean and extensible way. Just declare a base DrivingStrategy and inherit all version of driving logic from it. This is not over-engineering -- if you had two cases it would be, but four already show a need for that, especially if each version of Do... calls other methods. At least that's my personal experience.

    I do not agree with Jon Skeet solution that freezes a number of states, unless that is really necessary.

提交回复
热议问题