Java Reference
In-Depth Information
Figure 10.6
The SimulatorView
interface and
implementing
classes
«interface»
SimulatorView
implements
implements
GraphView
GridView
The previous versions of the foxes-and-rabbits project contained only one SimulatorView
class. This was a concrete class, and it provided the implementation of a grid-based view of the
field. As we have seen, the visualization is quite separate from the simulation logic (the field
and the actors), and different visualization views are possible.
For this project, SimulatorView was changed from a class to an interface, and the implemen-
tation of this view was moved into a class named GridView .
GridView is identical to the previous SimulatorView class. The new SimulatorView interface
was constructed by searching through the Simulator class to find all methods that are actually
called from outside and then defining an interface that specifies exactly those methods. They are:
view.setColor(classObject, color);
view.isViable(field);
view.showStatus(step, field);
view.reset();
We can now easily define the complete SimulatorView interface:
import java.awt.Color;
public interface SimulatorView
{
void setColor(Class animalClass, Color color);
boolean isViable(Field field);
void showStatus(int step, Field field);
void reset();
}
The one, slightly tricky detail in the definition above is the use of the type Class as the first
parameter of the setColor method. We will explain that in the next section.
The previous SimulatorView class, now called GridView , is specified to implement the new
SimulatorView interface:
public class GridView extends JFrame implements SimulatorView
{
...
}
It does not require any additional code, because it already implements the interface's methods.
However, after making these changes, it becomes fairly easy to “plug in” other views for the
 
Search WWH ::




Custom Search