
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 ᭹ Objects and object references ᭹ Exploring objects with BlueJ ᭹ Documentation comments ᭹ Numbers, strings, and arrays ᭹ Packages ᭹ Exception handling ᭹ Common utility classes: ArrayList and Scanner ᭹ Programming style guidelines Chapter 2 The Object-Oriented Design Process This chapter introduces the process of object-oriented design, CRC cards, and UML notation. It presents a case study of a simple voice mail system to illustrate the design process, starting with the project’s specification and culminating in its Java implementa- tion. Topics covered include ᭹ Identifying classes and methods ᭹ Relationships between classes ᭹ CRC cards ᭹ UML class, sequence, and state diagrams ᭹ Case study vi PREFACE Chapter 3 Guidelines for Class Design Unlike Chapter 2, which took a top-down view of the discovery of classes and their rela- tionships, this chapter focuses on the design of a single class or a small group of related classes. Topics covered include ᭹ Designing and implementing the interface of a class ᭹ The importance of encapsulation ᭹ Analyzing the quality of an interface ᭹ Programming by contract: preconditions, postconditions, and invariants Chapter 4 Interface Types and Polymorphism This chapter introduces the notation of the Java interface type, without mentioning inheritance. This approach has an important advantage: The reader learns about poly- morphism in its purest form, without being burdened by technical matters such as super- class construction or the invocation of superclass methods. The chapter also introduces the Swing user interface toolkit and AWT drawing opera- tions. It starts with the Icon interface type, which allows the placement of arbitrary drawings in a frame. Anonymous classes are introduced as an easy mechanism for “ad-hoc” objects that imple- ment a particular interface type. They are then put to use for Swing user interface actions. Up to this point, all interface types have been supplied in the standard library. The chap- ter ends with the design of a custom interface type. Topics covered include ᭹ Frames, images, and shapes ᭹ The Icon interface type ᭹ The Comparable and Comparator interface types ᭹ Anonymous classes ᭹ User interface actions ᭹ Designing interface types Chapter 5 Patterns and GUI Programming This chapter introduces the concept of patterns and covers a number of patterns that arise in the Swing user interface toolkit and the Java collections library. Topics include ᭹ Alexander’s architectural patterns ᭹ Software design patterns ᭹ The ITERATOR pattern as an example of a design pattern ᭹ The OBSERVER pattern, model/view/controller, and Swing listeners ᭹ The STRATEGY pattern and layout managers ᭹ The COMPOSITE pattern, user interface components and containers ᭹ The DECORATOR pattern, scroll panes, and borders Preface vii Chapter 6 Inheritance and Abstract Classes This chapter introduces the mechanics of inheritance using examples from the AWT graphics library. There is an extensive discussion of abstract classes, a topic that many beginners find challenging. An abstract shape class lays the foundation for the graph edi- tor framework created in Chapter 8. Several inheritance hierarchies are examined, including the hierarchies of Swing components, geometric shapes, and exception classes. The chapter discusses advanced exception handling, including the definition of new exception classes (which, of course, requires inheritance). The chapter
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages475 Page
-
File Size-