Replace conditional with polymorphism - nice in theory but not practical

后端 未结 9 1544
时光说笑
时光说笑 2020-12-24 01:52

\"Replace conditional with polymorphism\" is elegant only when type of object you\'re doing switch/if statement for is already selected for you. As an example, I have a web

9条回答
  •  情书的邮戳
    2020-12-24 02:41

    You're right - "the conditionals are getting pushed up to the top of the chain" - but there's no "just" about it. It's very powerful. As @thkala says, you just make the choice once; from there on out, the object knows how to go about its business. The approach you describe - BaseAction, ViewAction, and the rest - is a good way to go about it. Try it out and see how much cleaner your code becomes.

    When you've got one factory method that takes a string like "View" and returns an Action, and you call that, you have isolated your conditionality. That's great. And you can't properly appreciate the power 'til you've tried it - so give it a shot!

提交回复
热议问题