Head First Design Patterns, 2nd Edition
by Eric Freeman, Elisabeth Robson, Kathy Sierra, and Bert Bates
Copyright 2021 Eric Freeman and Elisabeth Robson. All rights reserved.
Printed in Canada.
Published by OReilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (.
Editors 1st Edition: Mike Hendrickson, Mike Loukides
Editors 2nd Edition: Michele Cronin, Melissa Duffield
Cover Designer: Ellie Volckhausen
Pattern Wranglers: Eric Freeman, Elisabeth Robson
Printing History:
October 2004: First edition
December 2020: Second edition
Release History:
2020-11-10 First release
The OReilly logo is a registered trademark of OReilly Media, Inc. Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc., in the United States and other countries. OReilly Media, Inc. is independent of Sun Microsystems.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks.
Where those designations appear in this book, and OReilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and the authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
In other words, if you use anything in Head First Design Patterns to, say, run a nuclear power plant, youre on your own. We do, however, encourage you to use the DJ View app.
No ducks were harmed in the making of this book.
The original GoF agreed to have their photos in this book. Yes, they really are that good-looking.
ISBN: 978-1-492-07800-5
[MBP]
To the Gang of Four, whose insight and expertise in capturing and communicating Design Patterns has changed the face of software design forever, and bettered the lives of developers throughout the world.
But seriously, when are we going to see a second edition? After all, its been only twenty-five years.
Authors of Head First Design Patterns
Eric is described by Head First series co-creator Kathy Sierra as one of those rare individuals fluent in the language, practice, and culture of multiple domains from hipster hacker, corporate VP, engineer, think tank.
By training, Eric is a computer scientist, having earned his PhD at Yale University. Professionally, Eric was formerly CTO of Disney Online & Disney.com at the Walt Disney Company.
Eric now co-directs the Head First series and devotes his time to creating print and video content at WickedlySmart, which is distributed across the leading educational channels.
Erics Head First titles include Head First Design Patterns, Head First HTML & CSS, Head First JavaScript Programming, Head First HTML5 Programming, and Head First Learn to Code.
Eric lives in Austin, Texas.
Elisabeth is a software engineer, writer, and trainer. She has been passionate about technology since her days as a student at Yale University, where she earned a Masters of Science in Computer Science.
Shes currently cofounder of WickedlySmart, where she creates books, articles, videos, and more. Previously, as Director of Special Projects at OReilly Media, Elisabeth produced in-person workshops and online courses on a variety of technical topics and developed her passion for creating learning experiences to help people understand technology.
When not in front of her computer, youll find Elisabeth hiking, cycling, kayaking, and gardening in the great outdoors, often with her camera nearby.
Creators of the Head First Series
Kathy has been interested in learning theory since her days as a game designer for Virgin, MGM, and Amblin, and a teacher of New Media Authoring at UCLA. She was a master Java trainer for Sun Microsystems, and she founded JavaRanch.com (now CodeRanch.com), which won Jolt Cola Productivity awards in 2003 and 2004.
In 2015, she won the Electronic Frontier Foundations Pioneer Award for her work creating skillful users and building sustainable communities.
Kathys recent focus has been on cutting-edge, movement science and skill acquisition coaching, known as ecological dynamics or Eco-D. Her work using Eco-D for training horses is ushering in a far, far more humane approach to horsemanship, causing delight for some (and sadly, consternation for others). Those fortunate (autonomous!) horses whose owners are using Kathys approach are happier, healthier, and more athletic than their fellows who are traditionally trained.
You can follow Kathy on Instagram: @pantherflows.
Before Bert was an author, he was a developer, specializing in old-school AI (mostly expert systems), real-time OSes, and complex scheduling systems.
In 2003, Bert and Kathy wrote Head First Java and launched the Head First series. Since then, hes written more Java books, and consulted with Sun Microsystems and Oracle on many of their Java certifications. Hes also trained hundreds of authors and editors to create books that teach well.
Berts a Go player, and in 2016 he watched in horror and fascination as AlphaGo trounced Lee Sedol. Recently hes been using Eco-D (ecological dynamics) to improve his golf game and to train his parrotlet Bokeh.
Bert and Kathy have been privileged to know Beth and Eric for 16 years now, and the Head First series is extremely fortunate to count them as key contributors.
You can send Bert a message at CodeRanch.com.
Table of Contents (the real thing)
Your brain on Design Patterns. Here you are trying to learn something, while here your brain is doing you a favor by making sure the learning doesnt stick. Your brains thinking, Better leave room for more important things, like which wild animals to avoid and whether naked snowboarding is a bad idea. So how do you trick your brain into thinking that your life depends on knowing Design Patterns?
Someone has already solved your problems. In this chapter, youll learn why (and how) you can exploit the wisdom and lessons learned by other developers whove been down the same design problem road and survived the trip. Before were done, well look at the use and benefits of design patterns, look at some key object-oriented (OO) design principles, and walk through an example of how one pattern works. The best way to use patterns is to