Page 28 - The Definitive Guide to Building Java Robots
P. 28
Preston_5564C01.fm Page 9 Friday, September 16, 2005 6:36 AM
CHAPTER 1 ■ A PRIMER 9
Table 1-2. Important Java Concepts
Concept Description
Events Usually done for graphical user interfaces, events are ways to send a
class a notification of a state change. For example, when you receive
data on a serial port, it will throw an event, informing the listening
class that there’s data in a buffer that must be read.
Interfaces I’m going to keep the use of interfaces simple. They’ll define patterns
of behavior for similar classes.
Super Classes I’m going to create super classes for all classes that have a “type-of”
relationship. For example, a Parallax Stamp, a Scott Edwards, and a
MiniSSC-II servo controller are types of controllers that communicate
to a serial port. I would create a super class to handle common types
of functionality.
Delegate Class A delegate is a class that shares some common functionality but
does not share a “type-of” relationship. For example, a controller
isn’t a type of sensor, but there might be common functionality I
want to add to both. I would then create a delegate class to handle
the functionality similarly for both classes.
Proxy Class This is a class that has the functionality of many classes or compo-
nents which are combined to act as a single class. I would use two
types of proxy classes: a physical proxy representing a robotic
device like a sensor, and an abstract proxy representing a behavior
like navigation.
Server A Java program that will not interact with a user interface or
command line.
Client A Java program that will interact with a user interface or a
command line and a server.
Threads If you want to do more than one thing at a time, you should use
multiple threads.
Thread Safe Concurrent Let’s say you have multiple threads trying to access a single serial
Access port on a PC. You need to find a way to limit access in a thread-safe
way so you don’t create dead-lock conditions.
Pausing Your Programs
I was at a conference when I heard a talk on state machines and thought it would be a good way
to introduce timing and synchronization. A state machine is a model composed of three things:
states, transitions, and actions. To illustrate an example of a state machine, I will use a retract-
able pen. It’s a very simple state machine. The pen is either extended and ready to write, or it’s
retracted so you can put it in your pocket. To change its state, you click its top, or twist it if it’s
a fancy pen.
Let’s define some terms as such: a state is some information about the model. A transition
is an indicator of a change in state caused by some action. The simple state machine shown in
Figure 1-7 shows how they are typically drawn. Starting at the top circle (called the initial state)
the pen tip is retracted. Click the top of the pen and it’s at a new state, extended and ready to
write, which is now represented by the bottom circle. Click it again, and you’re at your original
state. Pretty simple, huh?