Head First Design Patterns 4.0
Head First Design Patterns 4.0
by
Copyright 2006-2010, Data & Object Factory, LLC All rights reserved
Page 1 of 22
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 youll 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: Its 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 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 2 of 22
Page 280: Sir, may I abstract your Coffee, Tea? .................................................... 12 Page 300: Sorting with Template Method ............................................................... 12 Page 306: Swinging with Frames .......................................................................... 12 Page 307: Applets .................................................................................................. 13 Chapter 9: Iterator and Composite Pattern .................................................................... 14 Page 317: Menu ..................................................................................................... 14 Page 327: Reworking Menu with Iterator ................................................................ 14 Page 333: Cleaning things up with java.util.Iterator (.NET Iterator) ........................ 14 Page 360: Implementing the Menu Component...................................................... 14 Page 369: The Composite Iterator.......................................................................... 15 Chapter 10: State Pattern .............................................................................................. 16 Page 388: State Machines 101 .............................................................................. 16 Page 401: Implementing our State classes ............................................................ 16 Page 413: We still need to finish the Gumball 1 in 10 game .................................. 16 Chapter 11: Proxy Pattern ............................................................................................. 17 Page 431: Coding the Monitor ................................................................................ 17 Page 451: Getting the GumballMachine ready for remote service .......................... 17 Page 462: Get ready for Virtual Proxy .................................................................... 19 Page 474: Using .NET API Proxy to create a protection proxy ............................... 20 Chapter 12: Compound Patterns ................................................................................... 21 Page 501: Duck reunion ......................................................................................... 21 Page 503: When ducks are around, geese cant be far .......................................... 21 Page 506: Were going to make those Quackologists happy .................................. 21 Page 508: We need a factory to produce ducks! .................................................... 21 Page 513: Lets create a flock of ducks .................................................................. 21 Page 516: Can you say Observer? ....................................................................... 22 Page 534: Using MVC to control the beat............................................................... 22
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 3 of 22
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 concepts that initially are not easy to grasp. To alleviate this, the .NET Design Pattern Framework from Data & Object Factory, LLC includes a complete set of Head First Design Pattern code samples in C# and/or VB. It includes 46 projects -- all within in a single .NET Solution for easy access. With the .NET translations we have attempted to stay relatively close to the original Java code. With a smaller gap between the two languages, we felt that a .NET developer would get more out of this book. Finally, just to be clear, to study the .NET code samples you do need a copy of the Head First Design Patterns book available to you.
This document does three things: 1) It associates the original Java projects with the .NET projects, 2) It references the .NET projects back to the page where the pattern is discussed, and 3) It highlights noteworthy items that came up during the translation process
We are hopeful that you will find the .NET code samples useful in your effort to better understand and apply design patterns in your own work.
Implemented as DoFactory.HeadFirst.Strategy
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 4 of 22
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, therefore, this example runs as a console application. In .NET the Observer Pattern is implemented with multicast delegates, which is demonstrated in this example.
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 5 of 22
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 the same as the input file, the other the same but encrypted (using the decorator pattern).
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 6 of 22
Implemented as DoFactory.HeadFirst.Factory.PizzaShop
Page 131: Its 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 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 7 of 22
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. Furthermore, this is a thread-safe .NET solution to the multithreading issues discussed in this example.
Implemented as DoFactory.HeadFirst.Singleton.DoubleChecked
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 8 of 22
Implemented as DoFactory.HeadFirst.Command.SimpleRemote
Implemented as DoFactory.HeadFirst.Command.Undo A .NET enumeration named CeilingFanSpeed was added to replace the HIGH, LOW, MEDIUM, and OFF constants in Java.
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 9 of 22
Implemented as DoFactory.HeadFirst.Command.Party A .NET enumeration named CeilingFanSpeed was added to replace the HIGH, LOW, MEDIUM, and OFF constants in Java.
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 10 of 22
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 .NETs built-in facility to iterate over different types of collections.
Implemented as DoFactory.HeadFirst.Facade.HomeTheater
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 11 of 22
Page 277: Whipping up some coffee and tea classes (in .NET)
Implemented as DoFactory.HeadFirst.Template.SimpleBarista
Implemented as DoFactory.HeadFirst.Template. Barista This example also includes code for page 292: Hooked on Template Method
Implemented as DoFactory.HeadFirst.Template.WindowsService
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 12 of 22
Swing and Jframe do not exist in .NET. A good example of where .NET Template methods are used is when you write 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 very few of the templated events that you can respond to.
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 13 of 22
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, iterating over collections is far easier with the .NET foreach statement. On page 349 the book talks about iterators and collections in Java 5. It is interesting to note that the new Java 5 for statement is similar to C#s foreach statement.
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 14 of 22
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 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 15 of 22
Implemented as DoFactory.HeadFirst.State.Gumball
An enumeration GumballMachineState replaces the Java contants SOLD_OUT, NO_QUARTER, HAS_QUARTER, and SOLD.
Implemented as DoFactory.HeadFirst.State.GumballState
Implemented as DoFactory.HeadFirst.State.GumballStateWinner
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 16 of 22
Implemented as DoFactory.HeadFirst.Proxy.GumballMonitor
Implemented as:
RMI only exists in the Java world. The new .NET 3.5 Communication Subsystem 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 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 17 of 22
The client GumballMachineClient is a proxy object that stands in for a remote object. This proxy object will communicate with a remote instance of the GumballMachine that is exposed by the Host project. The results of the interaction are printed on the console screen. Note: if you have an Internet security program and you are running the GumBallMachineClient for the first time you may see this dialog box. Simply select OK.
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 18 of 22
Implemented as DoFactory.HeadFirst.Proxy.VirtualProxy
This simple .NET Windows Application uses an ImageProxy object. ImageProxy retrieves a book cover image from amazon.com on a separate thread. In the meantime (while retrieving) it provides a placeholder image that is stored locally. Click twice on the button to see Virtual Proxy in action. Note: you do need Internet access to make this work. In addition, if you have an Internet Security program running you may see the following dialog box when running for the first time. Simply select the recommended action.
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 19 of 22
Implemented as DoFactory.HeadFirst.Proxy.DotNetProxy
A dynamic proxy dynamically generates a class that conforms to a particular interface, proxying all invocations to a single 'generic' method. This functionality is standard in Java but not in .NET. In .NET there are two ways to implement this: one is to use the built-in RealProxy class and another way is to use Reflection.Emit.
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, with version 3.5 of the Design Pattern Framework we have removed this project from the Head First Design Pattern solution.
Copyright 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 20 of 22
Implemented as DoFactory.HeadFirst.Combining.Ducks
Implemented as DoFactory.HeadFirst.Combining.Adapter
Implemented as DoFactory.HeadFirst.Combining.Decorator
Implemented as DoFactory.HeadFirst.Combining.Factory
Page 21 of 22
Implemented as DoFactory.HeadFirst.Combining.Composite
Implemented as DoFactory.HeadFirst.Combining.Observer
Implemented as DoFactory.HeadFirst.Combined.MVC
As mentioned before, there is nothing similar to 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 2006-2010, Data & Object Factory, LLC. All rights reserved.
Page 22 of 22