Object-Oriented Design & Patterns
Second Edition
Cay Horstmann San Jose State University
John Wiley & Sons, Inc. PUBLISHER: Bruce Spatz SENIOR EDITORIAL ASSISTANT: Bridget Morrisey PROJECT MANAGER: Cindy Johnson, Publishing Services DIRECTOR OF MARKETING: Frank Lyman SENIOR PRODUCTION MANAGER: Ken Santor COVER DESIGNER: Harold Nolan COVER PHOTO: © Corbis/Media Bakery
This book was set in Adobe Caslon by Publishing Services and printed and bound by Malloy, Inc. The cover was printed by Phoenix Color Corporation.
This book is printed on acid-free paper. ∞
Copyright © 2006 John Wiley & Sons, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or oth- erwise, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008. To order books or for customer service, please call 1-800-CALL-WILEY (225-5945).
ISBN 0-471-74487-5 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 Preface
Making Object-Oriented Design Accessible This book is an introduction to object-oriented design and design patterns at an elementary level. It is intended for students with at least one semester of program- ming in an object-oriented language such as Java or C++. I wrote this book to solve a common problem. When students first learn an object-oriented programming language, they cannot be expected to instantly mas- ter object-oriented design. Yet, students should learn the principles of object-ori- ented design early enough to put them to work throughout the computer science curriculum. This book is suitable for a second or third course in computer science—no back- ground in data structures is required, and students are not assumed to have experi- ence with developing large software systems. Alternatively, the book can be used as a companion text in a course in software engineering. (If you need a custom version of this book for integration into another course, please contact your Wiley sales representative.) This second edition is fully updated for Java 5.0, including the use of generic collections and the “for each” loop a detailed discussion of parameterized type constraints auto-boxing and varargs methods, particularly in the reflection API multithreading with the java.util.concurrent package Integration of Design Patterns The most notable aspect of this book is the manner in which the coverage of design patterns is interwoven with the remainder of the material. For example, Swing containers and components motivate the COMPOSITE pattern. Swing scroll bars motivate the DECORATOR pattern, and Swing borders are examined as a missed opportunity for that pattern. Java streams give a second example of the DECORATOR pattern. Seeing the pattern used in two superficially different ways greatly clarifies the pattern concept. iv PREFACE
Without memorable examples, design patterns are just words. In order to visualize design patterns, this book uses examples from graphical user interface programming. Students will remember how a component is decorated by scroll bars, and how layout managers carry out different strategies. (A small and carefully selected subset of Swing is used for this purpose.)
A Foundation for Further Study After covering the material in this book, students will have mastered the following topics in three subject areas: 1. Object-oriented design A simple design methodology CRC cards and UML diagrams Design patterns 2. Advanced Java language Interface types, polymorphism, and inheritance Inner classes Reflection Generic types Multithreading Collections 3. User interface programming Building Swing applications Event handling Java 2D graphics programming These skills clearly form a useful foundation for advanced computer science courses. In fact, students who have completed this book will have encountered all features of the Java language (but not, of course, the entire standard Java library, which is too huge for any one person to master). One advantage of using Java is indeed that students can compre- hend the entire language. Contrast that with C++, a language that is so complex that virtually no one can truthfully claim to understand all of its subtleties. In summary: Use this book if you want your students to understand object-oriented design and design patterns early in the curriculum. As a bonus, your students will gain a complete overview of the Java language, and they will be able to program simple Swing user interfaces. Programming and Design Tools Another important aspect of this book is the coverage of tools. While many C++ programmers live their entire programming life in a large and complex integrated Preface v
environment, the Java culture has embraced the use of different tools such as BlueJ, javadoc, and JUnit. Due to the reflective nature of the Java language, there are many interesting experimental tools. I highlight a number of them in the hope that students will gain an interest and aptitude in evaluating and selecting tools that fit their working style. Students who learn object-oriented design also should become familiar with drawing UML diagrams. An easy-to-use and no-cost tool for this purpose, the Violet UML edi- tor, is provided for their use. Chapter 8 of this book introduces the framework on which Violet is based. All UML diagrams in this book were drawn with Violet.
A Tour of the Book Chapter 1 A Crash Course in Java This chapter introduces the basic syntax of Java and can serve either as a refresher or as a transition for students with a background in C++. Topics covered include Defining classes and methods