Java Reference
In-Depth Information
To deal with more general actors, it seems like a good idea to introduce an
Actor
super-
class. The
Actor
class would serve as a superclass to all kinds of simulation participants,
independent of what they are. Figure 10.3 shows a class diagram for this part of the simu-
lation. The
Actor
and
Animal
classes are abstract, while
Rabbit
,
Fox
, and
Hunter
are
concrete classes.
Figure 10.3
Simulation structure
with
Actor
Simulator
Actor
Animal
Hunter
Rabbit
Fox
The
Actor
class would include the common part of all actors. One thing all possible actors
have in common is that they perform some kind of action. We will also need to know whether
an actor is still active or not. So the only definitions in class
Actor
are those of abstract
act
and
isActive
methods:
//
all comments omitted
public abstract class Actor
{
abstract public void act(List<Actor> newActors);
abstract public boolean isActive();
}
This should be enough to rewrite the actor loop in the
Simulator
(Code 10.6), using class
Actor
instead of class
Animal
. (Either the
isAlive
method could be renamed to
isActive
or a separate
isActive
method in
Animal
could simply call the existing
isAlive
method.)
Exercise 10.52
Introduce the
Actor
class into your simulation. Rewrite the
simulate-
OneStep
method in
Simulator
to use
Actor
instead of
Animal
. You can do this even if
you have not introduced any new participant types. Does the
Simulator
class compile? Or is
there something else that is needed in the
Actor
class?
Search WWH ::
Custom Search