Daris
when you write and design code using TDD, it is common to push up as far as possible the code that composes objects, that define the system structure.
I started to do this, to keep together the creation of all objects that needs to be replaced to run integration and acceptance tests in the test environment, to mock external system. This is the type of flexibility I needed for testing.
This gave me also the flexibility in terms of changing and evolving the system: it enables you
- to make the system easily configurable,
- to make components easily replaceable with techniques such as 'branch by abstraction'
- to aggressively refactor compositional code to remove duplication and express intent, and
thereby raise the abstraction level at which program so less and less
code is needed to write more and more functionality as the system grows.
When you do like that, it become possible to create the factory in just one place of the system, at that point it does not need to be static any more, it's simply instantiated once and passed around using an interface.
No one except the instantiation code, and the test code, needs to know what's the concrete class.
To get there, it can take time and meanwhile you need to deal with static methods and variables that are already present in the system.
For this, you find few useful refactoring in the WELC book.
And here you find one example of how reduce the dependency from a static class and make the code testable. Until you reach the situation described initially and then you can get rid completely of the static.
1- the initial code here:
https://github.com/lucaminudel/TDDwithMockObjectsAndDesignPrinciples/tree/master/TDDMicroExercises/CSharp/TurnTicketDispenser2- a proposed solution:
https://github.com/lucaminudel/TDDwithMockObjectsAndDesignPrinciples/tree/master/TDDMicroExercises.ProposedSolution/CSharp3- the description of the code an #3 here:
https://github.com/lucaminudel/TDDwithMockObjectsAndDesignPrinciples/blob/master/TDDMicroExercises/README.mdHTH, Luca