by Steve Holzner, PhD Design Patterns FOR DUMmIES ‰ 01_798541 ffirs.qxp 3/27/06 2:19 PM Page iii 01_798541 ffirs.qxp 3/27/06 2:19 PM Page ii Design Patterns FOR DUMmIES ‰ 01_798541 ffirs.qxp 3/27/06 2:19 PM Page i 01_798541 ffirs.qxp 3/27/06 2:19 PM Page ii by Steve Holzner, PhD Design Patterns FOR DUMmIES ‰ 01_798541 ffirs.qxp 3/27/06 2:19 PM Page iii Design Patterns For Dummies ® Published by Wiley Publishing, Inc. 111 River Street Hoboken, NJ 07030-5774 www.wiley.com Copyright © 2006 by Wiley Publishing, Inc., Indianapolis, Indiana Gamma/Helm/Johnson/Vlissides, DESIGN PATTERNS: ELEMENTS OF REUSABLE OBJECT-ORIENTED SOFTWARE, © 1995 Pearson Education, Inc. Reprinted by permission of Pearson Education, Inc. Publishing as Pearson Addison Wesley. Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada 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 otherwise, except as permit- ted under Sections 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 Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at http://www.wiley.com/go/permissions. Trademarks: Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries, and may not be used without written permission. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REP- RESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CON- TENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CRE- ATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CON- TAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FUR- THER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER ENDORSES THE INFOR- MATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 800-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit www.wiley.com/techsupport. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Control Number: 2006920631 ISBN-13: 978-0-471-79854-5 ISBN-10: 0-471-79854-1 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 1B/RX/QU/QW/IN 01_798541 ffirs.qxp 3/27/06 2:19 PM Page iv About the Author Steve Holzner is the award-winning author of 100 books on computing. He’s a former contributing editor for PC Magazine, and has been on the faculty of Cornell University and MIT. In addition to his busy writing sched- ule, he gives programming classes to corporate programmers around the country and runs his own training company, which you can find at http://www.onsiteglobal.com/. 01_798541 ffirs.qxp 3/27/06 2:19 PM Page v 01_798541 ffirs.qxp 3/27/06 2:19 PM Page vi Dedication To Nancy, as always and forever. 01_798541 ffirs.qxp 3/27/06 2:19 PM Page vii 01_798541 ffirs.qxp 3/27/06 2:19 PM Page viii [...]... Four, or GoF, for short And those 23 design patterns became known as the GoF design patterns 2 Design Patterns For Dummies You see all 23 of those standard patterns in this book, and some additional ones as well I explain what each pattern does and when you should use the pattern You also see a programming problem that the design pattern solves, implemented in code In other words, every design pattern... you want to get the full patterns story from the beginning, jump into Chapter 1 first — that’s where all the action starts Also, for your convenience, all the code I provide in the book is available for downloading at www .dummies. com/go/designpatternsfd1e Part I Getting to Know Patterns I In this part n this part, your guided tour of design patterns begins Here, you see what patterns are all about... that have been well tested Through the use of patterns, they share their solutions with you, saving you lots of time and effort Chapter 1 Congratulations, Your Problem Has Already Been Solved In This Chapter ᮣ Introducing design patterns ᮣ Knowing how design patterns can help ᮣ Extending object-oriented programming ᮣ Taking a look at some specific design patterns A s a programmer, you know how easy... familiar with design patterns can make the design process all but automatic for you How do you turn into a software design expert, the envy of all, with hardly any work on your part? Easy You read this book and get familiar with the patterns I cover in depth You don’t have to memorize anything; you just get to know those patterns Then when you encounter a real-world issue that matches one of those patterns, ... attempt at designing the new cars 23 Handling Change with “has-a” Instead of “is-a” 27 Drawing Up Your Plans 29 Creating your algorithms 29 Using your algorithms 30 Selecting algorithms at runtime 33 Making Your Move with the Strategy Pattern 35 xiv Design Patterns For Dummies Chapter 3: Creating and Extending Objects with the Decorator and Factory Patterns. .. original 23 patterns were not used as much as some of the others You see them all in this book, but I emphasize the patterns that are used the most — and that includes some new, non-GoF patterns in Chapter 11 that have appeared since the GoF book debuted It’s important to realize that there is more going on here than just memorizing design patterns There are also specific design insights about object-oriented... I: Getting to Know Patterns blindly apply its design strategies without a lot of insight, and that can cause as many problems as it fixes A large part of understanding how to work with design patterns involves understanding the OOP insights behind them — encapsulating what changes most, for example, or knowing when to convert from is-a inheritance to has-a composites (see Chapter 2 for more on what these... know how, there’s nothing to it The design patterns covered in this book are essential for any programmer to know — and certainly for any professional programmer There’s a lot of ad hoc programming that goes on in the world, and that can lead to a lot of errors in critical code Why be the one sitting in front of the debugger all day? Put design patterns to work for you and just slip the solution into... Supporting undo .244 Testing the undo .247 Coordinating with the Mediator Pattern 249 Designing the Rutabagas-R-Us site 251 Connecting it all up with the mediator 254 Testing the Rutabagas-R-Us site .255 Part III: The Part of Tens 257 Chapter 11: Ten More Design Patterns 259 Creating a Factory Factory: The Abstract Factory Pattern 260 Cloning... handle object collections with the Iterator and Composite patterns, how to coordinate objects with the Command and Mediator patterns, and a great deal more in this part After you read this part, you’ll be an accomplished OOP meister 3 4 Design Patterns For Dummies Part III: The Part of Tens Chapter 11 tells you about the remainder of the standard patterns, some of which are not in common use anymore, . Holzner, PhD Design Patterns FOR DUMmIES ‰ 01_798541 ffirs.qxp 3/27/06 2:19 PM Page iii Design Patterns For Dummies ® Published by Wiley Publishing, Inc. 111 River Street Hoboken, NJ 0703 0-5 774 www.wiley.com Copyright. available in electronic books. Library of Congress Control Number: 2006920631 ISBN-13: 97 8-0 -4 7 1-7 985 4-5 ISBN-10: 0-4 7 1-7 985 4-1 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 1B/RX/QU/QW/IN 01_798541. READ. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 80 0-7 6 2-2 974, outside the U.S. at 31 7-5 7 2-3 993, or fax 31 7-5 7 2-4 002. For