Java Reference
In-Depth Information
When we reuse a piece of code, we tap the developers who built it, as well as
the testers, technical writers, and users who have used the original.
But inheritance is not a panacea. Deep inheritance trees are complex to
read and use, and they can hide some unpredictable behaviors. Using
inheritance as a shortcut can cause serious trouble. If a sales department, for
example, wants special extensions of the corporate customer class, a tempta-
tion is to create a SalesCustomer class. If other departments follow suit, we
will rapidly lose the ability to create real-world subclasses of the Customer
class. Still, it is another tool in our bag of tricks that can elegantly capture
powerful, real-world relationships.
2.3.3
Polymorphism enables flexible reuse
With inheritance, many different forms of an object can use the same type of
interface. A real-world appliance is a good example of polymorphism. Many
forms of an appliance do different things through the same interface. Every
electrical appliance has the same interface with the wall—a plug. Though
appliances can have dramatically different functions, the builders of a house
need to install a single kind of interface—the electric outlet. Polymorphism
works the same way.
A good example is the desktop document. To print it, we simply drag and
drop it on a printer object. The desktop documents are like polymorphic
appliances because we can have many different forms of documents with dif-
ferent behaviors and the same interface. The printer object is like the power
outlet. Whether the object is a spreadsheet, document, or graphic, though the
logic to print is different, we print it with the same gesture. The interface is
the same, even though the implementation may be dramatically different. The
printer object does not need to know that a document is a spreadsheet. It can
simply execute a generic print method specified in a standard interface, and let
the specific print method do the specialized job. In this way, application pro-
grammers are able to tap the functionality of specialized documents through a
standard interface.
2.3.4
Mini-antipatterns: Excessive Layering
I successfully exit my boat just as my body is sucked over yet another waterfall.
After a protracted swim through three rapids with drops, jagged trees, and boul-
ders, I am lucky to escape unscathed.
The guides misunderstood our request for an easy section. Up to that day, their
“advanced” section was a new, expert-level Class V run that had not yet made it
into guidebooks. Though the easy section in the guidebooks had no rapids harder
Search WWH ::




Custom Search