开发者

TDD with Strategy Pattern

I'm trying to implement the strategy pattern using TDD. Each strategy item implements an interface. What's the best way to do this with TDD?

Do you have to create a test fixture for each implem开发者_开发问答entation of the interface testing the same methods but on each implementation?

Any articles detailing the approach to take would be gratefully welcomed :)


I think I would write a separate test class for each implementation of the strategy.

You could make an abstract class for all of these to inherit from. This would help you make sure you implement all the tests for every strategy, but has the slight disadvantage that you'd have to implement stub methods at least before each test class would even compile.


  1. Write a test that is failing
  2. Write ugly code to make that test pass
  3. Refactor to make the code better

In step 2, write code that isn't implementing the Strategy Pattern (simplest thing that works, even if duplicated code is present).

Then in step 3 you refactor each class, one at a time, towards the Strategy pattern if it still makes sense to do so.

If you're truly doing TDD then you don't start out with a pattern -- you refactor to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜