Model View Kata
When you find yourself trying to do too many unrelated things in a class, it’s time to split the class in two. For instance, you put the rules and conceptual behavior (the so-called business logic) in one class know as the model. You put visualization and display details in another class known as the view.
Often your model has parts and you want each of the parts to have an associated view. The view for the main model will have sub-views for each of the model’s parts. Thus we end up with a scenario like this:
- A ParticleSystem contains some Particles.
- A ParticleSystem is visualized by a DisplayObjectContainer.
- A Particle has an optional DisplayObject skin.
- When a particle is in a ParticleSystem, the particle’s skin is in system’s container.
How would you implement this scenario in your favorite language? Keep in mind the consequences of these for statements:
- If a particle is added to a system, add the particle’s skin to the system’s container.
- If the skin for a particle changes, add the new skin to the container of the particle’s system.
- If the container for a particle system changes, move all the skins of the system’s particles to the new container.
- Since skin has no more than one parent, a particle with a skin can only be in one particle system.
Also think about potentially desirable properties that do not necessarily follow:
- If particle is removed from a system, remove the skin from its parent container.
- If skin is removed from a particle, remove the skin from its parent container.
- Two particles cannot have the same skin.
- A particle can only be in one system.
What do you think of your implementation? Is it the sort of thing your language, libraries, and environment support nicely or is it the sort of thing that’s buggy and hard to get right? If its hard, what patterns, protocols, or tests do you use to make it easier?




§Commentary