Head First Design Patterns 4.5
Head First Design Patterns 4.5
Companion document to
Design Pattern Framework™ 4.5
by
Page 1 of 22
Design Pattern Framework™ 4.5
Index
Index ............................................................................................................................... 2
Chapter 1: Intro to Design Pattern ................................................................................... 4
Page 18: Testing the Duck code .............................................................................. 4
Chapter 2: Observer Pattern ........................................................................................... 5
Page 57: Implementing the Weather Station ............................................................ 5
Page 67: Reworking the Weather Station with built-in support ................................. 5
Page 72: Other places you’ll find the Observer Pattern ............................................ 5
Chapter 3: Decorator Pattern .......................................................................................... 6
Page 95: Writing the Starbuzz Code ........................................................................ 6
Page 100: Real world Decorators: Java (i.e. .NET) I/O............................................. 6
Chapter 4: Factory Pattern .............................................................................................. 7
Page 112: Identifying the aspects that vary .............................................................. 7
Page 131: It’s finally time to meet the Factory Method Pattern ................................. 7
Page 145: Families of Ingredients…......................................................................... 7
Chapter 5: Singleton Pattern ........................................................................................... 8
Page 173: Dissecting the classic Singleton Pattern .................................................. 8
Page 175: The Chocolate Factory ............................................................................ 8
Page 180: Dealing with Multithreading ..................................................................... 8
Page 182: Use “double-checked locking” ................................................................. 8
Chapter 6: Command Pattern.......................................................................................... 9
Page 204: Our first command object ........................................................................ 9
Page 210: Implementing the Remote Control ........................................................... 9
Page 216: Undo ....................................................................................................... 9
Page 224: Every remote needs a Party Mode! ......................................................... 9
Chapter 7: Adapter and Facade Patterns ...................................................................... 11
Page 238: If it walks like a duck and quacks like a duck… ..................................... 11
Page 249: Adapting an Enumeration to an Iterator ................................................. 11
Page 255: Home Sweet Home Theater .................................................................. 11
Chapter 8: Template Method Pattern ............................................................................ 12
Page 277: Whipping up some coffee and tea classes (in .NET) ............................. 12
Copyright © Data & Object Factory, LLC. All rights reserved. Page 2 of 22
Design Pattern Framework™ 4.5
Copyright © Data & Object Factory, LLC. All rights reserved. Page 3 of 22
Design Pattern Framework™ 4.5
The book titled Head First Design Patterns has taken the developer community by storm
and has been a bestseller ever since. What has attracted developers is its whimsical and
informal approach to explaining advanced OO concepts and design patterns.
The book comes with a downloadable set of examples in Java. This is a problem for
.NET developers because it is hard to deal with language differences while at the same
time learning pattern concepts that are not always easy to grasp.
To alleviate this, the .NET Design Pattern Framework includes a complete set of Head
First Design Pattern code samples in .NET (C# or VB, depending on the edition you
purchased). There are 46 projects in total, all within in a single .NET Solution for easy
access. Our goal during the translations from Java to .NET was to stay as close as
possible to the original Java code and avoid using .NET features that are not available in
Java. This way, the descriptions in the book will be relatively close to the .NET code.
Just to be clear, to study the .NET code samples you do need your own a copy of the
Head First Design Patterns book; this book does not come with this package.
We are hopeful that you will find the .NET code samples useful in your effort to learn the
patterns described in Head First Design Patterns..
Implemented as DoFactory.HeadFirst.Strategy
Copyright © Data & Object Factory, LLC. All rights reserved. Page 4 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Observer.WeatherStation
Implemented as DoFactory.HeadFirst.Observer.WeatherStationObservable
.NET does not support the Observer/Observable built-in types so this example uses two
alternative types: the IObserver interface and the Observable base class. However, a
better way in .NET would be to use .NET multicast delegates as demonstrated in the
next example.
Implemented as DoFactory.HeadFirst.Observer.DotNet
.NET does not support Swing and this example runs as a simple console application. In
.NET the Observer Pattern is implemented with multicast delegates, which is
demonstrated in this example.
Copyright © Data & Object Factory, LLC. All rights reserved. Page 5 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Decorator.Starbuzz
Implemented as DoFactory.HeadFirst.Decorator.IO
The IO namespace in .NET uses the Decorator pattern quite extensively. This example
demonstrates the use of a CryptoStream that decorates a FileStream. The
CryptoStream links data streams to cryptographic transformations (encryption and
decryption services).
To run this example you need a text file ‘MyInFile.txt’ with some text in the project
directory – you could use “I know the decorator pattern therefore I rule!” as
demonstrated in the Head First Design Patterns book. Two new files are created in the
same directory; one which is the same as the input file, and the other which is also the
same, but encrypted (using the decorator pattern).
Copyright © Data & Object Factory, LLC. All rights reserved. Page 6 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Factory.PizzaShop
Page 131: It’s finally time to meet the Factory Method Pattern
Implemented as DoFactory.HeadFirst.Factory.Method.Pizza
Note: page 137 details the DependentPizzaStore which also exists in this project.
Implemented as DoFactory.HeadFirst.Factory.Abstract.Pizza
Copyright © Data & Object Factory, LLC. All rights reserved. Page 7 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Singleton.Classic
Implemented as DoFactory.HeadFirst.Singleton.Chocolate
Implemented as DoFactory.HeadFirst.Singleton.Multithreading
This project includes an EagerSingleton which ‘eagerly creates the instance’. This
occurs when the class is loaded for the first time. Also, please know that this is a thread-
safe .NET solution to the multithreading issues discussed in this example.
Implemented as DoFactory.HeadFirst.Singleton.DoubleChecked
Copyright © Data & Object Factory, LLC. All rights reserved. Page 8 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Command.SimpleRemote
Implemented as DoFactory.HeadFirst.Command.Undo
Copyright © Data & Object Factory, LLC. All rights reserved. Page 9 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Command.Party
Copyright © Data & Object Factory, LLC. All rights reserved. Page 10 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Adapter.Duck
Implemented as DoFactory.HeadFirst.Adapter.IterEnum
Unlike Java, .NET does not have legacy Enumeration interfaces. This example builds on
.NET’s built-in facility to iterate over different types of collections.
Implemented as DoFactory.HeadFirst.Facade.HomeTheater
Copyright © Data & Object Factory, LLC. All rights reserved. Page 11 of 22
Design Pattern Framework™ 4.5
Page 277: Whipping up some coffee and tea classes (in .NET)
Implemented as DoFactory.HeadFirst.Template.SimpleBarista
This example also includes code for page 292: Hooked on Template Method…
Implemented as DoFactory.HeadFirst.Template.Sort
Implemented as DoFactory.HeadFirst.Template.WindowsService
Copyright © Data & Object Factory, LLC. All rights reserved. Page 12 of 22
Design Pattern Framework™ 4.5
Swing and Jframe do not exist in .NET. A good example of where .NET Template
methods are useful is when you are writing a Windows Services app which requires that
you implement several ‘hooks’ (or Template methods), such as OnStart() and
OnStop(). The Visual Studio.NET generated boilerplate code requires that you simply
implement the body of these methods. Note: this is a Windows Service and therefore
does not run as a standalone executable.
Implemented as DoFactory.HeadFirst.Template.Control
Applets are similar to controls in .NET. This example shows that a Windows event
handlers are simply ‘hooks’ that you can choose to implement or not. Typically, you will
implement only a limited number of these templated events.
Copyright © Data & Object Factory, LLC. All rights reserved. Page 13 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Iterator.DinerMerger
Implemented as DoFactory.HeadFirst..Iterator.DinerMergerI
Implemented as DoFactory.HeadFirst.Iterator.DinerMergerCafe
In following the book, this example uses the built-in.NET IEnumerator interface.
However, in .NET iterating over collections is far easier with the foreach statement. On
page 349 the book talks about iterators and collections in Java 5. Interestingly, the new
Java 5 for statement is similar to C#’s foreach statement.
Copyright © Data & Object Factory, LLC. All rights reserved. Page 14 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Composite.Menu
Implemented as DoFactory.HeadFirst.Composite.MenuIterator
The .NET implementation was simplified because the iterator with the Stack example in
Java is overly complex. The Java code includes dubious try/catch usage and adds little
value to learning Design Patterns principles.
Copyright © Data & Object Factory, LLC. All rights reserved. Page 15 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.State.Gumball
Implemented as DoFactory.HeadFirst.State.GumballState
Implemented as DoFactory.HeadFirst.State.GumballStateWinner
Copyright © Data & Object Factory, LLC. All rights reserved. Page 16 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Proxy.GumballMonitor
Implemented as:
RMI only exists in the Java world. The standard Communication Subsystem in .NET is
WCF. In this example we demonstrate the use of a .NET Proxy object which is used to
invoke a remote class. Three projects are required for this demonstration. Compile the
above projects and set the Client as the Startup Project in Visual Studio. When running
the Client you will see the ASP.NET Web Server starting up (see image on next page).
Copyright © Data & Object Factory, LLC. All rights reserved. Page 17 of 22
Design Pattern Framework™ 4.5
The client GumballMachineClient is a proxy object which ‘stands in’ for a remote object.
This proxy object will communicate with a remote instance of the GumballMachine. The
GumballMachine is exposed by the Host project. The results of the interaction are
printed onto the console screen. Note: please be aware that if you run this for the first
time, it may take a few moments before you start seeing results on the console.
Another note: if you have an Internet security program (such as Norton 360) and you are
running the GumBallMachineClient for the first time you may see a security warning.
Simply select that you "allow connections from this program to all ports".
Copyright © Data & Object Factory, LLC. All rights reserved. Page 18 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Proxy.VirtualProxy
Copyright © Data & Object Factory, LLC. All rights reserved. Page 19 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Proxy.DotNetProxy
Prior versions of the Design Pattern Framework included the dynamic proxy pattern
using the Reflection.Emit method. It was based on the Open Source Nmock project
(nmock.org). However, the internal details of NMock are beyond the scope of our pattern
discussions and there was little or no educational value to the Pattern student.
Therefore, starting with version 3.5 of the Design Pattern Framework we have removed
this project from the Head First Design Pattern solution.
Copyright © Data & Object Factory, LLC. All rights reserved. Page 20 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Combining.Ducks
Implemented as DoFactory.HeadFirst.Combining.Adapter
Implemented as DoFactory.HeadFirst.Combining.Decorator
Implemented as DoFactory.HeadFirst.Combining.Factory
Copyright © Data & Object Factory, LLC. All rights reserved. Page 21 of 22
Design Pattern Framework™ 4.5
Implemented as DoFactory.HeadFirst.Combining.Composite
Implemented as DoFactory.HeadFirst.Combining.Observer
Implemented as DoFactory.HeadFirst.Combined.MVC
As mentioned before, there is nothing like Java Swing in .NET. Therefore, this example
is built as a standalone WinForms application. A timer control is used to generate the
beat (with Beep). The image on page 530 most closely resembles the implementation in
this .NET example. The only exception is line 5 (“I need your state information”); there is
no need for the View to query the Model because the state (the BeatsPerMinute) is sent
as part of line 4 (“I have changed”) using event arguments.
Copyright © Data & Object Factory, LLC. All rights reserved. Page 22 of 22