Java Reference
In-Depth Information
This new structure is more flexible because it allows easier addition of non-animal actors.
In fact, we could even rewrite the statistics-gathering class, FieldStats , as an Actor —it
too acts once every step. Its action would be to update its current count of animals.
Flexibility through abstraction
By moving towards the notion of the simulation being responsible for managing actor objects,
we have succeeded in abstracting quite a long way away from our original very specific sce-
nario of foxes and rabbits in a rectangular field. This process of abstraction has brought with
it an increased flexibility that may allow us to widen even further the scope of what we might
do with a general simulation framework. If we think through the requirements of other similar
simulation scenarios, then we might come up with ideas for additional features that we could
For instance, it might be useful to simulate other predator-prey scenarios such as a marine
simulation involving fish and sharks, or fish and fishing fleets. If the marine simulation were
to involve modeling food supplies for the fish, then we would probably not want to visualize
plankton populations—either because the numbers are too vast or because their size is too
small. Other environmental simulations might involve modeling the weather, which, while it is
clearly an actor, also might not require visualization.
In the next section, we shall investigate the separation of visualization from acting, as a further
extension to our simulation framework.
Selective drawing
One way to implement the separation of visualization from acting is to change the way it is
performed in the simulation. Instead of iterating over the whole field every time and drawing
actors in every position, we could iterate over a separate collection of drawable actors. The
code in the simulator class would look similar to this:
// Let all actors act.
for(Actor actor : actors) {
// Draw all drawables.
for(Drawable item : drawables) {
All of the actors would be in the actors collection, and those actors we want to show on
screen would also be in the drawables collection. For this to work, we need another super-
class called Drawable , which declares an abstract draw method. Drawable actors must then
inherit from both Actor and Drawable . (Figure 10.4 shows an example where we assume that
we have ants, which act but are too numerous to visualize.)
Search WWH ::

Custom Search