
CYAN YELLOW MAGENTA BLACK PANTONE 123 CV BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN SOFTWARE ENGINEERING Holub on Patterns: Holub Learning Design Patterns by Looking at Code Dear Reader, To be a good object-oriented designer, you have to know the design patterns cold, not just what they are but how to apply them to solve real problems. Most books on the subject leave you in the lurch in the how-to-apply-patterns department, however. They catalog the patterns for you but provide trivial Author of on examples that give you no real understanding of how the patterns work in the Taming Java Threads real world. Their examples don’t show you the complex interactions between Compiler Design in C patterns or the myriad ways that a pattern can be realized. Too many of the pat- tern books are filled with impenetrable academic prose that doesn’t make the Patterns subject any easier. I wrote this book to fix these problems. I’m a programmer, and I’ve written the book for programmers to read, centering the discussion around two non- trivial examples: a Game of Life implementation and a small embedded SQL interpreter. Taken together, these programs show you all the “Gang of Four” design patterns in context. You can see how the patterns work and how they Holub on interact. You can see how they’re actually used in nontrivial applications. I also discuss object-oriented-programming principles and how they apply to the patterns so that you can understand why the patterns do what they do. The book also includes a design-patterns quick reference that you can use to refresh you memory as you learn the patterns. You don’t need to know anything about design patterns to read this book (though you do need to know Java). When you’re done, you should understand the patterns thoroughly and be able to apply them to your own work with ease. Looking at Code by Learning Design Patterns Patterns —Allen Holub Learning Design Patterns by Looking at Code RELATED TITLES Fast Track UML 2.0 1-59059-320-0 User Interface Design Join online discussions: For Programmers forums.apress.com Learn design patterns by seeing them used in real programs. SOURCE CODE ONLINE 1-893115-94-1 FOR PROFESSIONALS BY PROFESSIONALS™ Joel on Software www.holub.com/goodies/patterns 1-59059-389-8 ISBN 1-59059-388-X Allen Holub www.apress.com Holub Shelve in Software Engineering (Design Patterns)/Java 6 89253 15880 7 9 781590 593882 User level: Intermediate–Advanced this print for content only—size & color not accurate 7" x 9-1/4" / CASEBOUND / MALLOY (0.8125 INCH BULK -- 432 pages -- 60# Thor) 388x_Ch00_FINAL.qxd 8/25/04 2:58 PM Page i Holub on Patterns: Learning Design Patterns by Looking at Code ALLEN HOLUB 388x_Ch00_FINAL.qxd 1/12/05 3:20 PM Page ii Holub on Patterns: Learning Design Patterns by Looking at Code Copyright © 2004 by Allen Holub Lead Editor: Gary Cornell Technical Reviewer: Ken Arnold Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore, Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser Project Manager: Tracy Brown Collins Copy Edit Manager: Nicole LeClerc Copy Editor: Kim Wimpsett Production Manager: Kari Brooks Production Editor: Janet Vail Proofreader: Nancy Sixsmith Compositor and Artist: Diana Van Winkle, Van Winkle Design Group Indexer: Ann Rogers Artist: Diana Van Winkle, Van Winkle Design Group Interior Designer: Diana Van Winkle, Van Winkle Design Group Cover Designer: Kurt Krames Manufacturing Manager: Tom Debolski Library of Congress Cataloging-in-Publication Data: Holub, Allen I. Holub on patterns : learning design patterns by looking at code / Allen Holub. p. cm. Includes index. ISBN 1-59059-388-X (alk. paper) 1. Software patterns. 2. Object-oriented programming (Computer science) I. Title. QA76.76.P37H65 2004 005.1—dc22 2004019635 All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed and bound in the United States of America 9 8 7 6 5 4 3 2 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Distributed to the book trade in the United States by Springer-Verlag New York, Inc. 233 Spring Street, 6th Floor, New York, New York 10013 and outside the United States by Springer-Verlag GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany. In the United States: phone 1-800-SPRINGER (1-800-777-4643), fax 201-348-4505, e-mail [email protected], or visit http://www.springer-ny.com. Outside the United States: fax +49 6221 345229, e-mail [email protected], or visit http://www.springer.de. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.holub.com/goodies/patterns. 388x_Ch00_FINAL.qxd 8/25/04 2:58 PM Page iii For Deirdre, Philip, and Amanda 388x_Ch00_FINAL.qxd 8/25/04 2:58 PM Page iv 388x_Ch00_FINAL.qxd 8/25/04 2:58 PM Page v Contents About the Author . xi About the Technical Reviewer . xiii Acknowledgments . xv Preface . xvii ■CHAPTER 1 Preliminaries: OO and Design Patterns 101 . 1 Patterns vs. Idioms . 1 So What Is a Design Pattern, Anyway? . 2 So, What’s It All Good For? . 5 The Role of Patterns in Design . 6 The Tension Between Patterns and Simplicity . 6 Classifying Patterns. 7 On Design, Generally. 9 Programming FORTRAN in Java . 10 Programming with Your Eyes Open. 12 What Is an Object? . 12 Balderdash!. 13 An Object Is a Bundle of Capabilities . 13 How Do You Do It Wrong?. 15 So How Do You Do It “Right?” . 17 Cellular Automata . 20 Getters and Setters Are Evil. 24 Render Thyself . 27 JavaBeans and Struts . 28 Refactoring . 29 Life Without Get/Set. 30 When Are Accessors and Mutators Okay?. 32 Summing Up the Getter/Setter Issues . 34 v 388x_Ch00_FINAL.qxd 8/25/04 2:58 PM Page vi vi ■CONTENTS ■CHAPTER 2 Programming with Interfaces, and a Few Creational Patterns . 37 Why extends Is Evil . 37 Interfaces vs. Classes . 38 Losing Flexibility. 39 Coupling. 40 The Fragile-Base-Class Problem. 41 Multiple Inheritance. 47 Frameworks and the Template-Method and Factory-Method Patterns. 48 Summing Up Fragile Base Classes . 55 When extends Is Appropriate. 56 Getting Rid of extends . 58 Factories and Singletons . 59 Singleton . 61 Threading Issues in Singleton . 62 Double-Checked Locking (Don’t Do It) . 64 Killing a Singleton . 65 Abstract Factory . 67 Pattern Stew . 70 Dynamic Creation in a Factory. 73 Command and Strategy . 75 Summing Up. 80 ■CHAPTER 3 The Game of Life . 81 Get a Life. 82 Charting the Structure of Life . 83 The Clock Subsystem: Observer. 86 Implementing Observer: The Publisher Class . 93 The Clock Subsystem: The Visitor Pattern. 104 The Menuing Subsystem: Composite. 108 The Menuing Subsystem: Facade and Bridge . 116 The MenuSite. 117 The Core Classes. 139 The Universe Class . 139 The Cell Interface. 145 The Resident Class . 148 The Neighborhood Class. ..
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages434 Page
-
File Size-