On the previous page, the function makeRotatingRevolvingBox
, which builds the scene graph, and the function update,
which repeatedly updates the animation, are closely coupled. The scene is represented by a two-level scene graph. The parent node revolves the box around the z-axis while its child node rotates the box on its own axis. Each of these nodes has an rps property that sets rotation rate. To maintain the animation, the update
function must know that the scene graph has this particular structure, and where to access and how to use the nodes’ rps properties to update their angular rotations.
Had we structured the scene graph differently, we would have had to revise the update
function accordingly. As long as the animation logic is located in update,
the function remains tightly coupled to the scene graph and to the makeRotatingRevolvingBox
function that produces it.
In this relatively simple program, such interdependence between functions not a big problem. But imagine modeling our solar system, or any nested solar system in which moon orbits moon orbits moon to arbitrary depth. And in which the moons’ orbits vary by rate and inclination of local rotation, by rate and plane of orbit, and in other ways. How is update
to ‘read’ complex scene graphs containing many properties encoding the behaviors of so many objects?
Is there a God that manages the movement of the many planets and moons that make up our solar system, not to mention the living things that inhabit them? I couldn’t say. But I’m confident that we’re not able to write a program to realize reality in its full complexity, or even a fair facsimile of it.
Better to let every object be responsible for its own behavior. (Parents, take note.) Since behavior depends on time, we notify every object that behaves — that is, every animated object — of time’s passage. Whenever an animated object is notified of elapsed time, it updates its properties on which its own behavior depends.
This design is supported by the well-known observer design pattern which we turn to next.