I have the following story to implement - I need to be able to build a scene in a game based on its predefined representation.
On one hand I have my scene - that contains the definition of: - the terrain - lights - cameras - players
On the other hand, there's this factory that knows just how to "create" them in the 3D engine I'm using.
Now - I tried several approaches, and each time I finish the refactionring after the first iteration, I end up with stuff I started
Would somebody please be willing to show me how to evolve the whole thing through the tests? If so, then start evolving the code from scratch - meaning that you should evolve the scene definition and the factory I mentioned as well.
Here is one of the approaches I had - it's not that bad and I think it is a way to go, but if you could take a look at it and tell me what did I screw up, that'd be great.
A word of information - in the Red Phase I always use pseudocode to get my idea down onto the paper, then I translate it and make it work during the Green Phase.
Iteration 1, phase Red
I want to be able to create something on my scene - let that something be a terrain.
... hmmmm - how to test that... - I don't want to get the implementation of how I create a terain in a particualr 3d Engine involved yet...
... Yet another approach ....
.. ok - at least this time I had a clear testing point...
Iteration 1, phase Green
The shortest steps to get things working....
YEAH - IT'S WORKING
Iteration 1, phase Refactoring
I messed it up quite badly - the code smells.
The Terrain class is just a marker - it's used only in the test - I'll remove it for now
Ok - seems kinda clean. I have my scene definition captured in one class, the way it should be built in the other ... Let's move on.
Iteration 2, phase Red
I want to be able to add not only the terrain, but also the light to the scene.
Iteration 2, phase Green
Ok - it's working. Now for some refactoring...
Iteration 2, phase Refactoring
I don't like it - Open-Close principle's good name is being dragged throught the mud here. I''m expecting all kinds of different entities here, and I can't keep adding methods each time a new one comes up...
One more thing - I noticed that I needed to introduce exactly the same change to the factory as the one I introduced to the SceneDefinition. Maybe their responsibilities are not so well separated after all...
Basically, after applying DIP I get the following code, where instead of having a factory, I end up with a bunch of creators that can handle particular objects creation process.
Everything works, and now I don't have to worry about breaking the OCP any more.
BUT - what I ended up with - I mean the SceneDefinition - that's just a wrapping for a list - does nothing more but store the creators and run a create on them all - it resembles more of a Compound then some separate entity - so I'll evolve it towards that...
Ok - now that's more like it
So basically that's my story. I can now create those things dinamically based on some arbitrary representation using parsers etc..