Java Reference
In-Depth Information
For a fox-and-rabbits simulation, the sort of events we are talking about would be birth,
movement, hunting, and death from natural causes. What typically happens is that, as each
event occurs, a fresh event is scheduled for some point in the future. For instance, when a
birth event occurs, the event marking that animal's death from old age will be scheduled. All
future events are stored in an ordered queue, where the next event to take place is held at the
head of the queue. It is important to appreciate that newly scheduled events will not always be
placed at the end of the current queue; they will often have to be inserted somewhere before the
end, in order to keep the queue in time order. In addition, some future events will be rendered
obsolete by events that occur before them—an obvious example is that the natural-death event
for a rabbit will not take place if the rabbit is eaten beforehand!
Event-driven simulations lend themselves particularly well to the techniques we have
described in this chapter. For instance, the concept of an event is likely to be implemented
as an Event abstract class containing concrete details of when the event will occur, but only
abstract details of what the event involves. Concrete subclasses of Event will then supply the
specific details for the different event types. Typically, the main simulation loop will not need
to be concerned with the concrete event types, but will be able to use polymorphic method
calls when an event occurs.
Event-based simulations are often more efficient and are preferable where large systems and
large amounts of data are involved, while synchronous simulations are better for produc-
ing time-based visualizations (such as animations of the actors) because time flows more
Exercise 10.64 Find out some more about how event-driven simulations differ from time-
based simulations.
Exercise 10.65 Look at the java.util package to see if there are any classes that might
be well suited to storing an event queue in an event-based simulation.
Exercise 10.66 Challenge exercise Rewrite the foxes-and-rabbits simulation in the event-
based style.
Summary of inheritance
In the past three chapters, we have discussed many different aspects of inheritance techniques.
These include code inheritance and subtyping as well as inheriting from interfaces, abstract
classes, and concrete classes.
In general, we can distinguish two main purposes of using inheritance: we can use it to inherit
code (code inheritance), and we can use it to inherit the type (subtyping). The first is useful for
code reuse, the second for polymorphism and specialization.
When we inherit from (“extend”) concrete classes, we do both: we inherit the implementation
and the type. When we inherit from (“implement”) interfaces, we separate the two: we inherit
a type but no implementation. For cases where parts of both are useful, we can inherit from ab-
stract classes; here, we inherit the type and a partial implementation.
Search WWH ::

Custom Search