Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Separating behaviors into disparate objects makes sense when the separation takes advantage of polymorphism. Polymorphism allows two objects to be treated identically, using the same methods, even though the objects implement these methods in quite different ways. It is this concept of "same appearance, different behavior" that gets the 0 word, polymorphism. (You can tell how important this concept is by the number of syllables the word has!)
Polymorphism works because the classes that implement the same method differently both derive, at some point, from a common superclass. A general program is written that operates on objects of the superclass type. The program is oblivious to the fact that what it thinks of as a superclass object is, in reality, an object that is a member of one of the subclass types. When the program invokes a method defined in the superclass, the method that gets called is actually the subclass method that overrides the superclass version.
Polymorphism allows very general programs to be written for a superclass, letting the subclasses take care of the details.
An example of this is a drawing program that can write a loop that processes a list of graphic objects including lines, squares,
and circles. It can work from the bottom most graphic object to the top, invoking the draw() method on each object in turn. The drawing program manages the sequence of drawing operations, which depends on which objects
are "on top." Each object, in turn, is responsible for drawing itself. The program works on objects of type Graphic, knowing that every Graphic object always implements the draw() method. The subclasses Line, Square, and Circle, all derive from the superclass Graphic, and each overrides the draw() method in its own fashion. The program can then be extended by adding an Elipse or Rectangle class. As long as the new classes
derive from Graphic, and as long as they implement the draw() method appropriately, the basic drawing program continues to work, unchanged.
(See Philip Bishop's JavaWorld article, "Polymorphism and Java," for more information on this subject.)
The State pattern, described in the book Design Patterns, by Gamma, Helm, Johnson, and Vlissides, Addison-Wesley, 1995 (see Resources for further book information), is an example of a tremendously useful software pattern that takes advantage of polymorphism. (The State pattern is just one of 23 useful patterns contained in that book.) The State pattern uses polymorphism to define different behaviors for different states of an object. It is a valuable pattern to master, because it can be used in practically any sizable application.