<< Chapter < Page | Chapter >> Page > |
In this module we will explore many OOP concepts by examining the program "Ballworld". Download the code for Ballworld
here (
Link Temporarily Disabled. Please contact the authors for the code.).
To run Ballworld, load the files into DrJava and right-click the BallControl file. Select "
ABall
class is typed into the text field on the left. The "
In a nutshell, the way Ballworld works is akin to a flip-book animation: The
BallControl
class contains a
Timer
that "ticks" every 50 milliseconds. Every time the timer ticks, the panel in the
BallGUI
upon which the balls are to be drawn is requested to repaint itself. When the panel repaints, it also tells the
Dispatcher
to notify every
ABall
in the system to update itself. When an
ABall
updates, it updates any internal values it has (its "state") such as its color, radius, and/or velocity. It then moves its position by an amount corresponding to its velocity and draws ("paints") itself onto the panel. Since this is happening 20 times a second, the balls appear to be moving and/or changing and thus the animation is acheived. The
ILambda
interface3 enables the
BallGUI
to communicate in a generic, decoupled manner to the
BallControl
and the
Randomizer
class is a utility class that provides methods to produce various random values needed by the system. Much of the code in Ballworld is significantly more sophisticated than what has been covered in the course so far--it will be covered soon, don't worry!
First, we will focus on the union design pattern between
ABall
,
WanderBall
,
CurveBall
and
StraightBall
. In a union design pattern, we see that the superclass represents an abstraction of the union of its subclasses. For instance, a fruit is an abstraction of specific concrete classes such as apple and pear. A fruit embodies the common characteristics of its subclasses even if it doesn't completely describe the exact nature of those characteristics. In a fruit, there is a seed. However, the notion of "fruit" doesn't specify exactly the number, size, color or shape of its seed(s). It only specifies that it does indeed have a seed. Likewise, a fruit has the behavior of ripening. Apples, oranges, and bananas all ripen differently and at different rates. The abstract fruit notion does not specify the specific nature of the ripening behavior, just simply that it does have that behavior. In such, we see that we can never have a fruit that is not a specific class of fruit, such as an orange or grape.
Notification Switch
Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?