Release Team[oR] 2001
[x] java
Mitchell Waite Signature Series: Object-Oriented Design
in Java
by Stephen Gilbert and Bill McCarty
Sams © 1998, 731 pages
Mastery of object-oriented design would be closer only by
moving in with the two superb authors.
ISBN: 1571691340
Table of Contents
Back Cover
Synopsis by Rebecca Rohan
Object-Oriented Design in Java is a characteristically beautiful volume from
the Mitchell Waite Signature Series. Using helpful analogies, professional and
appealing drawings, and excellent, accessible writing, the book teaches broad
design concepts and techniques that can be applied in any software milieu,
while focusing on a practical Java example. The majority of the book is given
to designing new creations, but chapters 4 and 11 are, respectively, devoted
to avoiding bad classes and making use of proven designs. The pattern of
information, summary, and questions throughout Object-Oriented Design in
Java make it an excellent self-study book for beginning through advanced
programmers.
- What Is Design and Why Is it Needed? - 10
- Object-Oriented Software Development - 29
- Teach Yourself Java in 21 Minutes - 57
- Encapsulation: Classes and Methods - 98
- Designing Classes and Objects - 144
- Round-Trip Design: A Case Study - 166
- Object Relationships: Implementing Associations - 194
- Object Relationships: Compositions and Collections - 221
- Implementing Class Relationships: Inheritance and Interfaces - 246
Table of Contents
Object-Oriented Design in Java™ - 4
Introduction - 7
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10 - Designing with Inheritance and Interfaces: A Case Study - 281
Chapter 11 - Patterns: Proven Designs - 315
Chapter 12 - Designing Concurrent Objects - 352
Chapter 13 - Designing Remote Objects - 392
Chapter 14 - Designing Persistent Objects: Database Design and
Chapter 15 - User Interface Design and Implementation - 440
Chapter 16 - Designing with Components - 476
Implementation - 417
- 2 -
Chapter 17 - Designing with Class Libraries - 508
Chapter 18 - Architectures: Design-in-the-Huge - 544
Appendix A - Glossary - 567
Appendix B - Bibliography - 584
Appendix C - Product Reference - 587
Appendix D - Quiz Answers - 588
Back Cover
Object-Oriented Design in Java is an important addition to the Mitchell Waite
Signature Series. This unique book focuses on an area that gets little or no
coverage in conventional programming language texts: how to design
programs. To take full advantage of Java, or of any of today's object-oriented
languages, programmers must learn fundamental design principles as well as
the syntax of code. This tutorial teaches program design in a format even
beginning programmers can understand.
You'll learn to use a design notation system that makes it easier to represent
the design features of a program. Unlike conventional texts in software
design, this book starts with concrete solutions and uses examples to
demonstrate theory and abstract principles. This approach is aimed at hands-
on programmers who learn best by working through problems in step-by-step
fashion.
Object-Oriented Design in Java, like the other titles in the Mitchell Waite
Signature Series, is a highly developed, carefully crafted book that delivers
clear, thorough coverage. Written by expert authors, these books are
subjected to ardent development, exhaustive review, and meticulous
production. They are designed to satisfy, without compromise, the time-
honored, award winning standards for high-quality publications established by
The Waite Group and its founder, Mitchell Waite.
• Understand the life cycle of object-oriented software
• Discover how to implement class relationships
• Find out how to design a class using Java code
• Utilize the rules to designing with inheritance and interfaces
• Explore the class libraries and their design techniques
• Learn design techniques with components like Java Beans
• Prepare to design your own remote objects
• Cover object-oriented design in Java from the ground up
About the Authors
Bill McCarty teaches management information systems and computer science
at Azusa Pacific University. He holds a B.S. in computer science and a Ph.D.
in the management of information systems from The Claremont Graduate
School. He learned to write computer programs using the IBM 1130 while still
a high school student back in the days of the IBM model 026 keypunch.
Stephen Gilbert teaches computer science at Orange Coast College in Costa
Mesa, CA. Steve saw his first computer play tic-tac-toe in the summer of 1960
and wanted one of his own ever since. When he purchased his first machine,
a VIC-20 (which he subsequently trained to play an unbeatable game), his life
took a sudden turn after years spent as a carpenter, chicken picker, and offset
press operator. Since then, he has been a programmer and systems designer
for DRI Management Systems of Newport Beach, as well as for other clients
in California, Oregon, and Washington. Steve holds a B.S. degree in business
- 3 -
computer methods and a M.S. degree in applied computer science and
technology from Azusa Pacific University.
Waite Group Press
A Division of Macmillan Computer Publishing
Corte Madera, CA
Object-Oriented Design in Java™
Stephen Gilbert and Bill McCarty
Signature Series
Developed by Mitchell Waite and Robert Lafore
PUBLISHER: Mitchell Waite
ASSOCIATE PUBLISHER: Charles Drucker
EXECUTIVE EDITOR: Susan Walton
ACQUISITIONS EDITOR: Susan Walton
PROJECT DEVELOPMENT EDITOR: Laura Brown
CONTENT EDITOR: Scott Rhoades
TECHNICAL EDITOR: Keith Ermel
PROJECT EDITOR: Maureen A. McDaniel
COPY EDITORS: Susan Shaw Dunn, Kate Givens, Tonya R. Simpson, Kate Talbot,
Dana Rhodes Lesh
MANAGING EDITOR: Jodi Jensen
INDEXER: Erika Millen
INDEXING MANAGER: Johnna L. VanHoose
EDITORIAL ASSISTANT: Carmela Carvajal
SOFTWARE SPECIALIST Dan Scherf
PRODUCTION MANAGER: Cecile Kaufman
PRODUCTION TEAM SUPERVISOR: Brad Chinn
- 4 -
COVER DESIGNER: Sandra Schroeder
INTERIOR DESIGNER: Diana Jean Parks
PRODUCTION: Marcia Deboy, Michael Dietsch, Jennifer Earhart, Cynthia Fields,
Maureen West
© 1998 by The Waite Group, Inc.®
Published by Waite Group Press ™
200 Tamal Plaza, Corte Madera, CA 94925
Waite Group Press™ is a division of Macmillan Computer Publishing.
All rights reserved. No part of this manual shall be reproduced, stored in a retrieval
system, or transmitted by any means, electronic, mechanical, photocopying, desktop
publishing, recording, or otherwise, without permission from the publisher. No patent
liability is assumed with respect to the use of the information contained herein. While
every precaution has been taken in the preparation of this book, the publisher and author
assume no responsibility for errors or omissions. Neither is any liability assumed for
damages resulting from the use of the information contained herein.
All terms mentioned in this book that are known to be registered trademarks, trademarks,
or service marks are listed below. In addition, terms suspected of being trademarks,
registered trademarks, or service marks have been appropriately capitalized. Waite
Group Press cannot attest to the accuracy of this information. Use of a term in this book
should not be regarded as affecting the validity of any registered trademark, trademark,
or service mark. Sun’s Java Workshop and JDK is copyrighted [1997] by Sun
Microsystems, Inc. Sun, Sun Microsystems, the Sun logo, Java, Java Workshop, JDK,
the Java logo, and Duke are trademarks or registered trademarks of Sun Microsystems,
Inc. in the United States and other countries. All Microsoft products mentioned are
trademarks or registered trademarks of Microsoft Corporation.
The Waite Group is a registered trademark of The Waite Group, Inc.
Waite Group Press and The Waite Group logo are trademarks of The Waite Group, Inc.
Printed in the United States of America
98 99 10 9 8 7 6 5 4 3 2
Library of Congress Cataloging-in-Publication Data: 97-46425
International Standard Book Number: 1-57169-134-0
About the Authors
Bill McCarty teaches Management Information Systems and Computer Science at Azusa
Pacific University. He holds a B.S. in Computer Science, and a Ph.D. in the Management
of Information Systems from The Claremont Graduate School. He learned to write
computer programs using the IBM 1130 while still a high school student back in the days
of the IBM model 026 keypunch. Although on cold days he misses the warmth provided
by the 026’s vacuum tubes, he finds Southern California winters, for the most part,
bearable. He passes them by reading and by writing software in the cheery solitude of his
condo, which he shares with his wife, two children, three cats, and an unknown number
of software bugs.
- 5 -
Stephen Gilbert teaches computer science at Orange Coast College in Costa Mesa,
California. Steve saw his first computer play tic-tac-toe in the summer of 1960 and
wanted one of his own ever since. When he purchased his first machine, a Vic-20 (which
he subsequently trained to play an unbeatable game), his life took a sudden turn after
years spent as a carpenter, chicken picker, and offset press operator. Since then, he has
been a programmer and systems designer for DRI Management Systems of Newport
Beach, as well as for other clients in California, Oregon, and Washington. Steve holds a
B.S. degree in Business Computer Methods from California State University at Long
Beach, and an M.S. degree in Applied Computer Science and Technology from Azusa
Pacific University. Having long ago given up his ambition of becoming a professional
surfer, he can still be found sitting on the Newport Beach pier with his wife Kathleen, or
his children, Judah and Hosanna, whenever the surf’s up.
Steve and Bill are the co-authors of Mitchell Waite Signature Series: Object-Oriented
Programming in Java.
Acknowledgments
Stephen Gilbert: This has been a year of change. In the midst of starting a new job and
preparing an entirely new set of courses, I received an email from Bill McCarty,
suggesting that we work together on a book about object-oriented design, using Java.
Bill, as is his usual habit, was abuzz with a half-dozen wild and crazy ideas, and his
enthusiasm was infectious. Nevertheless, it was with no little trepidation that I
approached my wife, expecting to hear the words that would come most naturally to my
tongue, were our situations reversed: “Not again!!!?” (or words to that effect). Instead of
discouraging me, however, my wife Kathleen and my children, Hosanna and Judah,
picked up the slack and gave me the time I needed to finish the project. My gratitude to
them knows no bounds.
Finally, in a year of change, I’d be remiss if I failed to acknowledge the one constant in
my life—the care and guidance of my Lord, Jesus Christ. In the words of the old hymn, “I
don’t know who holds tomorrow, but I know who holds my hand.”
Bill McCarty: Jennifer, my wife, and Patrick and Sara, my children, who patiently
answered the phone or made lunch while Dad wrote “another book,” deserve most of the
credit for this work. After all, they did most of the real work. As they’ve finally figured out,
I’d much rather write than answer the phone or make lunch.
The remainder of the real work was done by my co-author, Stephen Gilbert, who
regularly turned my terse and stultifying prose into something delightful. Thanks, Steve,
let’s do it again some time!
I also thank the “author and finisher of [my] faith” (Heb. 12:2, KJV), of whom it is written,
“Not by works of righteousness which we have done, but according to His mercy He
saved us, by the washing of regeneration and the renewing of the Holy Ghost” (Titus 3:5,
KJV).
Stephen Gilbert and Bill McCarty: We want to thank the wonderful folks at the Waite
Group, whose expertise is matched only by their kindness. Mitch, Charlie, Robert, Susan,
Laura, Andrea, Dan, and “honorary Waite Groupers” Scott and Keith: you guys are the
greatest! Thanks so much for all your help and encouragement, without which this book
would be a mere dream.
We also want to thank the many professors who reviewed chapter manuscripts, patiently
bearing with our inchoate expressions and all-too-frequent errors, and graciously offering
numerous improvements. Prof. Leon Tabak of Cornell College was a particularly abundant
source of suggestions that substantially improved the quality of the book. Errors,
omissions, bad style, and corny humor should not be laid at their doorstep, but at ours.
- 6 -
Introduction
What Is This Book About, and How Is It Different?
Why does the world need another book on design? More to the point, why should you
read this book on object-oriented design in Java? A trip to amazon.com, the Internet
bookstore where you can find almost any book ever written, reveals over 200 titles that
contain the word design or designing applied to computers or programming. If you
decided to read one of these books each week, it would take you more than four years to
finish the current crop. Even narrowing the field to books whose titles contain the words
design and object-oriented, still yields a hefty 75 possibilities. Contemplating that task,
you may find yourself agreeing with King Solomon, who wrote several thousand years
ago: “Of making many books there is no end; and in much study there is a weariness of
the flesh.”
Why are there so many books on software design? A cynic would suggest that books on
design flourish for the same reason that books on dieting fill the bookstores: because
their readers have a boundless hope that this regimen will work! While there might be
some justification for this viewpoint—you don’t have to travel far to find a program that is
hard to use or that breaks down regularly—a more fundamental reason is that different
software developers mean different things by the word design.
You’ve probably heard the ancient Indian legend (or read John Godfrey Saxe’s version)
of the six blind men examining an elephant. Each of them feels only a portion of the
elephant—the side, a tusk, the trunk, or a leg—and each concludes that the elephant is
like a wall, a spear, a snake, or a tree.
Books on software design exhibit similar tendencies. Authors coming from a software-
engineering background see design in terms of specifications and blueprints; those from
the information systems world think of an activity of the software development life cycle,
along with their favorite diagrams; the data base analysts think of normalization and entity-
relationships; those immersed in computer-human interaction naturally see design as the
specification of dialogs between people and machines; those responsible for interface
design view design from a perspective that emphasizes visual integrity and communication.
Like the blind men in the story, each of these constituencies has its own view on design,
and each view is correct and valuable. But none of them is the whole story.
Our Viewpoint
In writing Object-Oriented Design in Java, we have tried to keep in mind two objectives.
Our first objective was to avoid too narrow and specific a focus. We believe that design is
important in every aspect of software development, and that every developer should have
a toolkit of different design techniques and methods to call on. While your “real job” may
require an in-depth knowledge of database design, GUI craftsmanship, or object and
class design, such specialized knowledge is no substitute for a broad understanding of
the design issues and techniques that are used throughout the software development
process. You will find this book especially valuable if you are
•
A student who has learned to program in Java, and you now find yourself asking “How
does this all fit together?” Learning a programming language is a little like learning the
syntax and semantics of English: you may have learned how to write and speak
grammatically, but such knowledge doesn’t prepare you to write a poem or a detective
novel or a business letter. It is a necessary condition, but it is not sufficient. Object-
Oriented Design in Java will help you to put the pieces of fundamental programming
knowledge together, helping you to build software products that perform their intended
function efficiently and effectively, and are a pleasure to use.
• A small software developer who, literally, “has to do it all.” The introduction of Java and
the use of the Internet for software distribution and marketing have revived the
- 7 -
•
fortunes of the independent developer, who only a few years ago was considered an
endangered species. Object-Oriented Design in Java will help you make sure that
you’ve “touched all the bases” in developing your product.
A technical manager or developer involved in corporate software develop-ment or
Information Technology. Object-Oriented Design in Java will not only teach you the
basics of each design perspective, but will also let you see where each of the parts of
design “fit together.” Thus, even if you are already an expert in interface design, you
can use this text to learn about database design. And, should the occasion arise, the
“To Learn More” sections at the end of each chapter will give you a set of references
that you can use for further, in-depth study.
Because software design draws from such a wide range of related disciplines, it’s
impossible for a single book to cover every aspect of design comprehensively. Yet, we
believe it’s useful and important for designers to know something about each of the
disciplines that bears on the practice of design. Of course, not everyone will agree with
our choice of that “something.” A reader with a strong background in, say, software
engineering might bemoan our failure to cover one or more topics the reader thinks
important. Like the blind man who believes an elephant is like a tree, such a reader might
wish this book had a bigger trunk or more branches. But, just as an elephant is not a tree,
design is not software engineering. Neither is it information systems, database analysis
and design, computer-human interaction, nor interface design.
In designing the book, we worked hard to include all the fundamentals, the things a
practicing designer needs to know and is called on to apply on a daily basis. Because in
each chapter we wanted to write more than we could, each chapter includes a section titled
“To Learn More,” which lists books and articles that can help you further explore topics of
personal interest and relevance. We expect you’ll find Object-Oriented Design in Java a
useful book for both study and reference.
Our Method
Besides adopting a “breadth-first” viewpoint, our objective was to adopt a specific method
of presentation. Object-Oriented Design in Java is a book that was written to teach a wide
variety of object-oriented design techniques to Java programmers. Design books with an
Information-Systems or Software-Engineering bent usually begin by teaching analysis,
followed by design and implementation. This book is different because the basic
elements of object-oriented design are presented beginning with the implementation, and
the design notations and methods are then built upon this concrete example.
Albert Schweitzer (the philosopher, physician, musician, clergyman, missionary, and
writer on theology who won the 1952 Nobel Peace Prize) once remarked that there
are three ways to learn—by example, by example, and by example. We find it easier to
learn by going from the concrete to the abstract than from the abstract to the concrete,
and we think you will too. For example, in Chapter 4, following a brief problem statement,
we show you how to model a solution using the Java language. This chapter provides
specific instruction on writing classes and presents several rules of thumb (heuristics) you
can use to build more reliable and reusable classes. This chapter, which uses an
informal, intuitive approach to class and object design, is followed (rather than preceded)
by a chapter on systematic analysis (use case and CRC cards) and class/object
notations. Following what we call the “round-trip” methodology, the next chapter then
introduces you to a case study that is worked out “from scratch.”
This same pattern is followed through the rest of the book:
• Present a concept by creating an implementation example.
• Discuss and present informal design measures and heuristics.
• Show how to represent that implementation using a design notation (we use the
- 8 -