Design patterns are general templates for solving problems in a generic way. The tradeoff is exactly what you are seeing. This happens because you need to customize the generic approach. 12 command classes does not seem like a lot to me, though, personally.
With the command pattern, hopefully the commands are simple (just an execute method, right?) and hence easy to test. Also, they should be testable in isolation, i.e. you should be able to test the commands easily with little or no dependencies.
The benefit you should be seeing are two-fold:
1) You should have seen your specific, complicated approach simplified by using the pattern(s) you chose. i.e. something that was getting ugly quickly should now be more elegant.
2) Your should be going faster, due to the simplified approach and the ease of testing your individual components.
Can you make use other patterns, like composite, and use good OO design to avoid duplicating code (if you are duplicating code...)?
That doesn't seem like a lot of command classes, but I agree with you that it smells a little if they make up more than 60% of your classes. If the project is complex enough to merit the use of the command pattern, I suspect you'll find some classes begging to be split up. If not, perhaps the command pattern is overkill.
The other answers here have great suggestions for reducing the complexity of your commands, but I favor simplicity where I can find it (ala the bowling game).
发布评论
评论(2)
设计模式是以通用方式解决问题的通用模板。这种权衡正是您所看到的。发生这种情况是因为您需要自定义通用方法。不过,就我个人而言,12 个命令类对我来说似乎并不算多。
使用命令模式,希望命令很简单(只是一个执行方法,对吧?),因此易于测试。此外,它们应该可以单独测试,即您应该能够在很少或没有依赖性的情况下轻松测试命令。
您应该看到的好处有两个:
1) 您应该已经看到通过使用您选择的模式简化了特定的、复杂的方法。即那些很快就变得丑陋的东西现在应该变得更加优雅。
2) 由于简化的方法和易于测试各个组件,您的速度应该会更快。
您能否使用其他模式(例如复合模式)并使用良好的 OO 设计来避免重复代码(如果您正在重复代码......)?
Design patterns are general templates for solving problems in a generic way. The tradeoff is exactly what you are seeing. This happens because you need to customize the generic approach. 12 command classes does not seem like a lot to me, though, personally.
With the command pattern, hopefully the commands are simple (just an execute method, right?) and hence easy to test. Also, they should be testable in isolation, i.e. you should be able to test the commands easily with little or no dependencies.
The benefit you should be seeing are two-fold:
1) You should have seen your specific, complicated approach simplified by using the pattern(s) you chose. i.e. something that was getting ugly quickly should now be more elegant.
2) Your should be going faster, due to the simplified approach and the ease of testing your individual components.
Can you make use other patterns, like composite, and use good OO design to avoid duplicating code (if you are duplicating code...)?
这似乎不是很多命令类,但我同意你的观点,如果它们占你的类的 60% 以上,那就有点臭了。如果项目足够复杂,值得使用命令模式,我怀疑您会发现一些类需要拆分。如果不是,则命令模式可能有点过分了。
这里的其他答案对于降低命令的复杂性有很好的建议,但我喜欢简单的地方,我可以找到它(ala 保龄球游戏)。
That doesn't seem like a lot of command classes, but I agree with you that it smells a little if they make up more than 60% of your classes. If the project is complex enough to merit the use of the command pattern, I suspect you'll find some classes begging to be split up. If not, perhaps the command pattern is overkill.
The other answers here have great suggestions for reducing the complexity of your commands, but I favor simplicity where I can find it (ala the bowling game).