Book Review: Design Patterns in C#

Posted on November 04, 2004  |  

Posted in Books

4 comments

Design patterns in C# bookI'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!

4 comments

Chris Martin
on November 12, 2004

If you think that's a great singleton implementation you should read this: http://www.yoda.arachsys.com/csharp/singleton.html.


Milan Negovan
on November 12, 2004

Thank you for the link, Martin. It's a nice collection of singletons. I'm sorry I have to delete the rest of your comment, because it's just plain disrespectful. ;)


Jackson
on February 21, 2006

I don't people that buy tech books are dumb. I 'm not that's for sure.


Jadawin
on August 8, 2007

Quizzing before the material is presented, isn't all that 'strange' in the world of educational techniques, though. It's certainly not a technique used for slow or remedial learners, though. I've seen it used in three ways:

- to test between 'reading before a class' and 'presenting the material after the class'

- in concert with a follow-up, very similar quiz, to test knowledge 'before' and 'after' material is presented

- as an 'end of class' activity, for the next day's work, to gauge the level of teaching required.

Admittedly, each of these is targeted at different information gathering goals, and I haven't seen any instructors (that I can recall) use even two of them, but I've experienced each. Even done the second one myself. :)