One More Cup of Cocoa 'Fore I Go
August 14, 2008 9:01 PM
Well, the Summer of Code is winding down, and I'm going to be spending the next few days whipping up a wiki and a code repository. I just want to make one more quick change before it all comes crashing down around me.
Remember how, in the last post, we made the ALifeWindowController a little smarter about reading configuration files? The improvement was somewhat ironic, since the purpose of that blog was to improve the headless bugs application, and we didn't even modify it to be able to use the new shuffling functionality.
Implementing shuffling naively in the headless bugs implementation would involve copying the new code from ALifeWindowController, which is not very DRY; and in any case, the problem seems to be that the ALifeWindowController has gotten too big for its britches. A mere window doesn't need to know anything about configuration files and options. That's just silly.
Luckily, it's going to be a pretty quick fix to factor out that functionality into a new class: ALifeSimulationController. Instead of the ALifeWindowController doing all the configuration work on its own, it'll outsource it to this rather sassy young class.
Particularly, we're going to put most of the code from ALifeWindowController's - (id)initWithSimulationClass:(Class method into ALifeSimulationController, and add a new @property for the simulation controller to the ALifeWindowController class.
Now, all the window controller does is to create a new simulation controller with the options passed to it, and set up its own views and statistics controllers, which is really what it should have been doing in the first place.
This also lets our headless application take advantage of the new value shuffling code with just a few new lines:
simulationController = [[ALifeSimulationController alloc] initWithSimulationClass:selectedPlugin
configuration:configuration];
...
for (step = 0; step < numberOfSteps; step++) {
[simulationController.lifeController update];
}
Huzzah! We have DRYed out our controllers just a little bit more.
BUT! Lest you think we have arrived at the pinnacle of architecture perfection, this refactoring has brought out a weakness in our abstractions: now we have ALifeControllers, and ALifeSimulationControllers (that have a lifeController attribute that actually does all the work). It seems like we have two different names for the same thing: a class that manages an ALife simulation.
Indeed, I plan to make the current ALifeController protocol into a full-fledged superclass for ALifeControllers, and refactor the ALifeSimulationController functionality into that class. That will let us DRY up the plugins significantly as well, by implementing default functionality for things like statistics and configuration dictionaries.
However, that's a project for another day (or week). For now, I'm just scrambling to finish up this SoC stuff. Goodbye for now, blog, we'll meet again when I catch my breath.

Leave a comment