Head First Design Patterns

Why Rent from Knetbooks?

Because Knetbooks knows college students. Our rental program is designed to save you time and money. Whether you need a textbook for a semester, quarter or even a summer session, we have an option for you. Simply select a rental period, enter your information and your book will be on its way!

Head First Design Patterns introduces software design patterns using the unique and highly effective Head First learning approach made popular in our bestselling Head First Java. This book teaches underlying concepts so that beginner- and intermediate-level developers really understand the implications of applying a particular pattern for application design and performance. They'll learn how patterns are built using Object Oriented design fundamentals, so they can construct their own "elegant" design solutions. Plenty of examples show how design patterns are used.

Eric Freeman is a computer scientist with a passion for media and software architectures and coauthor of Head First Design Patterns. He just wrapped up four years at a dream job-- directing internet broadband and wireless efforts at Disney--and is now back to writing, creating cool software, and hacking Java and Macs. Eric spent a lot of the '90s working on alternatives to the desktop metaphor with David Gelernter (and they're both still asking the question, "Why do I have to give a file a name?"). Based on this work, Eric landed a Ph.D. at Yale University in 1997. He also co-founded Mirror Worlds Technologies (now acquired) to create a commercial version of his thesis work, Lifestreams.

In a previous life, Eric built software for networks and supercomputers. You might know him from such books as JavaSpaces Principles Patterns and Practice. Eric has fond memories of implementing tuple-space systems on Thinking Machine CM-5s and creating some of the first internet information systems for NASA in the late 1980s.

When he's not writing text or code you'll find him spending more time tweaking than watching his home theater and trying to restore a circa 1980s Dragon's Lair video game. He also wouldn't mind moonlighting as an electronica DJ.

Write to him at eric at wickedlysmart dot com or visit him at http://www.ericfreeman.com .

Elisabeth Robson (formerly Freeman) is coauthor of O'Reilly's Head First Design Patterns and Head First HTML with CSS & XHTML. She is currently Special Projects Director at O'Reilly where she is developing new brain-friendly learning ideas and products.

Bert Bates is a 20-year software developer, a Java instructor, and a co-developer of Sun's upcoming EJB exam (Sun Certified Business Component Developer). His background features a long stint in artificial intelligence, with clients like the Weather Channel, A&E Network, Rockwell, and Timken.

Kathy Sierra has been interested in learning theory since her days as a game developer (Virgin, MGM, Amblin'). More recently, she's been a master trainer for Sun Microsystems, teaching Sun's Java instructors how to teach the latest technologies to customers, and a lead developer of several Sun certification exams. Along with her partner Bert Bates, Kathy created the Head First series. She's also the original founder of the Software Development/Jolt Productivity Award-winning javaranch.com, the largest (and friendliest) all-volunteer Java community.

Intro to Design Patterns

Welcome to Design Patterns: Someone has already solved your problems

The SimUDuck app

p. 2

Joe thinks about inheritance...

p. 5

How about an interface?

p. 6

The one constant in software development

p. 8

Separating what changes from what stays the same

p. 10

Designing the Duck Behaviors

p. 11

Testing the Duck code

p. 18

Setting behavior dynamically

p. 20

The Big Picture on encapsulated behaviors

p. 22

HAS-A can be better than IS-A

p. 23

The Strategy Pattern

p. 24

The power of a shared pattern vocabulary

p. 28

How do I use Design Patterns?

p. 29

Tools for your Design Toolbox

p. 32

Exercise Solutions

p. 34

The Observer Pattern

Keeping your Objects in the Know: Don't miss out when something interesting happens!

The Weather Monitoring application

p. 39

Meet the Observer Pattern

p. 44

Publishers + Subscribers = Observer Pattern

p. 45

Five minute drama: a subject for observation

p. 48

The Observer Pattern defined

p. 51

The power of Loose Coupling

p. 53

Designing the Weather Station

p. 56

Implementing the Weather Station

p. 57

Using Java's built-in Observer Pattern

p. 64

The dark side of java.util. Observable

p. 71

Tools for your Design Toolbox

p. 74

Exercise Solutions

p. 78

The Decorator Pattern

Decorating Objects: Just call this chapter "Design Eye for the Inheritance Guy."

Welcome to Starbuzz Coffee

p. 80

The Open-Closed Principle

p. 86

Meet the Decorator Pattern

p. 88

Constructing a Drink Order with Decorators

p. 89

The Decorator Pattern Defined

p. 91

Decorating our Beverages

p. 92

Writing the Starbuzz code

p. 95

Real World Decorators: Java I/O

p. 100

Writing your own Java I/O Decorator

p. 102

Tools for your Design Toolbox

p. 105

Exercise Solutions

p. 106

The Factory Pattern

Baking with OO Goodness: Get ready to cook some loosely coupled OO designs

When you see "new", think "concrete"

p. 110

Objectville Pizza

p. 112

Encapsulating object creation

p. 114

Building a simple pizza factory

p. 115

The Simple Factory defined

p. 117

A Framework for the pizza store

p. 120

Allowing the subclasses to decide

p. 121

Let's make a PizzaStore

p. 123

Declaring a factory method

p. 125

Meet the Factory Method Pattern

p. 131

Parallel class hierarchies

p. 132

Factory Method Pattern defined

p. 134

A very dependent PizzaStore

p. 137

Looking at object dependencies

p. 138

The Dependency Inversion Principle

p. 139

Meanwhile, back at the PizzaStore...

p. 144

Families of ingredients...

p. 145

Building our ingredient factories

p. 146

Looking at the Abstract Factory

p. 153

Behind the scenes

p. 154

Abstract Factory Pattern defined

p. 156

Factory Method and Abstract Factory compared

p. 160

Tools for your Design Toolbox

p. 162

Exercise Solutions

p. 164

The Singleton Pattern

One of a Kind Objects: The Singleton Pattern: your ticket to creating one-of-a-kind objects, for which there is only one instance

One and only one object

p. 170

The Little Singleton

p. 171

Dissecting the classic Singleton Pattern

p. 173

Confessions of a Singleton

p. 174

The Chocolate Factory

p. 175

Singleton Pattern defined

p. 177

Hershey, PA, we have a problem...

p. 178

BE the JVM

p. 179

Dealing with multithreading

p. 180

Singleton Q&A

p. 184

Tools for your Design Toolbox

p. 186

Exercise Solutions

p. 188

The Command Pattern

Encapsulating Invocation: In this chapter we take encapsulation to a whole new level: we're going to encapsulate method invocation

Home Automation or Bust

p. 192

The Remote Control

p. 193

Taking a look at the vendor classes

p. 194

Meanwhile, back at the Diner...

p. 197

Let's study the Diner interaction

p. 198

The Objectville Diner Roles and Responsibilities

p. 199

From the Diner to the Command Pattern

p. 201

Our first command object

p. 203

The Command Pattern defined

p. 206

The Command Pattern and the Remote Control

p. 208

Implementing the Remote Control

p. 210

Putting the Remote Control through its paces

p. 212

Time to write that documentation

p. 215

Using state to implement Undo

p. 220

Every remote needs a Party Mode!

p. 224

Using a Macro Command

p. 225

More uses of the Command Pattern: Queuing requests

p. 228

More uses of the Command Pattern: Logging requests

p. 229

Tools for your Design Toolbox

p. 230

Exercise Solutions

p. 232

The Adapter and Facade Patterns

Being Adaptive: In this chapter we're going to attempt such impossible feats as putting a square peg in a round hole