I've always wanted to find a reference of all classic design patterns, crafted by the "Gang of Four", and see their implementation in C#. Knowing patterns in theory looks nice on your resume, but being able to apply them in work is even more valuable. With this goal in mind I picked up a copy of Design Patterns in C# by Steven John Metsker.
Pros
Steven knows what he's talking about. All patterns are nicely organized. I really liked chapter introductions and summaries because they were at times much clearer than chapter content itself. Small typos here and there didn't bother me that much. Overall, the book is proof-read quite well.
Cons
Each chapter presents a number of challenges, or quizzes. They appear intermittently with text and therefore distract you from the discussion each time because their solutions are listed in the back and you have to flip back and forth to follow code.
In a couple of places Steven throws a quiz at you and afterwards presents the subject at hand. Normally, you present material first and then quiz. Doing it the other way around is quite a strange educational technique.
Steven is an author of a number book on Java, and it shows in his C# code. Nothing wrong with Java per se, but c'mon! For example, he refers to the book Concurrent Programming in Java as an excellent resource when discussing multithreaded programming in .NET. The next thing he does is present the following implementation of a singleton:
public class Factory {
private static Factory _factory;
private static Object _classLock = typeof (Factory);
public static Factory GetFactory() {
lock (_classLock) {
if (_factory == null)
_factory = new Factory ();
return _factory;
}
}
}
There are several problems with this code that stand out right away. First, locking on the type itself is a bad idea because it may lead to deadlocks. Jeffrey Richter has explained why. Second, even with the if (factory == null) check the code isn't thread safe.
A more efficient implementation of a singleton with a double-check lock is found at Microsoft's Patterns and Practices:
public class Factory {
private static Factory _factory;
private static Object _classLock = new object ();
public static Factory GetFactory() {
if (factory == null) {
lock (_classLock) {
if (factory == null)
_factory = new Factory ()
}
return _factory;
}
}
}
I really wish Steven stayed closer to the "C# community" to know this.
And last but not least: all samples throughout the book are about a fictitious firework factory. I'm glad Steven read The Chemistry of Fireworks, but I didn't, and I doubt any of you did. Some of the processes at the fictitious plant are often times hard to follow and very confusing.
Conclusion
The book is valuable. I didn't think it was a waste of money. Still, it fails to be the best book on the subject of Design Patterns in C# around. I strongly suggest you take a look at the Data & Object Factory site where all these patterns are listed with much clearer examples.
My verdict: 3 stars out of 5.
If any of you, guys, want to make history and write a solid book on design patterns in C#, the opportunity is right in front of you!