Why I wrote this book 

In 2002, Microsoft Research hosted an international meeting in Cambridge, U.K., to reveal its Rotor system, which would bring C# and .NET to non-Windows programmers. Once back home, writing software, papers, and a book on the topic, I came to realize that we had witnessed the beginning of a real revolution in programming. Since the advent of Java in 1996, programming had become platform-independent: with Java byte-code, programs could run anywhere. This independence, however, extended only to programs written in the Java language. .NET, on the other hand, was language-independent: it allowed programs in different languages to interact, but, up until that day, only on Windows.

In the ensuing five years, new platforms have come to support .NET (Mono, for example) and new hardware has come to support Intel chips (on which Windows runs). The result is that .NET now runs almost anywhere. Consequently, expertise in C# programming is a very transportable skill to acquire. But C# keeps improving as a language, and we are currently at the beginning of a new leap forward into C# 3.0, which offers enormous benefits in terms of productivity and ease of programming. Having already written an introductory C# text in 2003,* I realized that the benefits of the new features announced now in 2007, four years later, would be felt at a much more advanced level of software development. I wanted to write a second book that introduced C# 3.0 to developers who already knew the basic language—but what would be the formula that could introduce a language and address a reader’s needs of precision, examples, and a heavy dose of reality?

Enter design patterns. Design patterns encapsulate common, accepted, and proven ways of using language features together. They form a level of discourse at a higher plane, and they exercise and promote good programming practices. However, there is an element of unreality surrounding design patterns, and one gets the impression that they are more talked about than used. I wanted to change that and make design patterns really accessible to ordinary programmers, using the best language for them: C# 3.0. The result is this book.

Who the book is for

If you are a programmer who loves your code, for whom every line has a precise meaning and every feature has a correct place, this book is for you. It will help you with your primary job of making your code correct, elegant, extensible, and efficient. If you serve the business ends of your organization by focusing on the quality of your code, you need a book like C# 3.0 Design Patterns. Knowledge about design patterns is also a big step forward for those working up from low-level programmers to software engineers and architects.

Through reading this book, you will acquire skills in:

Although not written as a textbook, C# 3.0 Design Patterns could fit in very well for a mid-degree course on design patterns or advanced programming. The diagrams and code for all the patterns and associated examples and case studies in this book can be found on the book’s web site and here. 9780596527730

What you need to know

This book is for programmers who know how to program in C# 1.0 or Java 1.4 and who would like to move on to the more modern features of the latest language. All the new features of C# 3.0, as well as many novel features from C# 2.0, are introduced by example and summarized in sidebars for easy reference. Thus, the book serves as a programmers’ guide as well.

The book does not assume any acquaintance with design patterns. It covers the full set of 23 patterns that were originally proposed in Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides’s Design Patterns: Elements of Reusable Object- Oriented Software in 1994 and now form a common introductory base to the patterns that are emerging in many other domains (such as security, concurrency, and architectural design). At the end of the book, the reader will have a thorough grounding in design patterns as they are commonly understood.

How this book is organized

After the introduction in Chapter 1, the book takes a tour through the 23 core design patterns. Each chapter discusses two or three patterns, chosen because they have some common applicability and can be compared at the end of the chapter. The patterns are subdivided into three groups: structural, creational, and behavioral.

We begin with the structural patterns, studying the Decorator, Proxy, and Bridge patterns in Chapter 2; the Composite and Flyweight patterns in Chapter 3; and the Adapter and Façade patterns in Chapter 4. We then move on to the creational patterns, exploring the Prototype, Factory Method, and Singleton patterns in Chapter 5 and the Abstract Factory and Builder patterns in Chapter 6. The last four chapters deal with the largest category, the behavioral patterns: Chapter 7 focuses on the Strategy, State, and Template Method patterns; Chapter 8 on the Chain of Responsibility and Command patterns; Chapter 9 on the Iterator, Mediator, and Observer patterns; and Chapter 10 on the Visitor, Interpreter, and Memento patterns. Our discussion of each pattern will consist of the following parts:

A short, high-level description of the pattern and what it is meant to achieve

An example of where the pattern might be used in programming a modern computer system, illustrating a real-world context with a photo or diagram to help you remember the pattern

An identification of the interconnected players in the pattern and their roles, explained in a UML diagram, with links back to the key players in the illustration

A stepwise refinement development of a short program that illustrates the pattern using the terms introduced in the “Design” section

A second program that gives an example of the pattern, usually in terms of the illustrative example, where the programming moves away from a strict adherence to the pattern terms

A discussion of real-world scenarios where the pattern might be used, ending with a table listing the conditions that would make its use applicable

A list of exercises of differing degrees of difficulty designed to enhance your understanding of the pattern under discussion

At the end of each chapter you will find a comparison of the patterns discussed therein and a discussion of how they fit in with those that have gone before. The ordering of the patterns has been carefully chosen so as to represent a gradual progression in C# 3.0 maturity. Implementing the earlier patterns in each section requires very little that is not available in C# 1.0, while the later patterns are more sophisticated in their implementation and take advantage of more advanced features present in C# 3.0. This approach allows new features to be introduced inline, as they become relevant, rather than all at once at the start or end of the text.

This book is not intended to be a reference guide to the whole of C# or even C# 3.0, but rather to be a practical guide to using the most interesting features of the language. Although the emphasis is on developments in the most recent version, I also pick out some features of C# 1.0 and 2.0 that I think are really useful but that are not often employed in code. The list of C# features explicitly covered follows this Preface.
Special features of this book include:

• Pictorial illustrations of patterns, to help you focus on the meaning of what each pattern can accomplish in real life
• Quizzes that relate the illustrations to the UML diagrams
• Uncluttered “theory” code that can be adapted to many situations
• Tables that give guidance on when to choose a specific pattern
• Comparison tables for patterns that are similar, showing how they differ
• Lists of the advantages, disadvantages, and limitations of each pattern
• Challenges and exercises to help you take your knowledge further

What You Need to Use This Book

To run the programs in this book, you need:

• A computer that will run Windows XP or Vista. Compatible platforms are any PC or Intel-based Mac with a virtual machine.
• Microsoft’s .NET Framework 3.5. As of October 2007, this version is still in Beta 2, but it is very stable now. It is available at
• A program editor or programming environment. Visual Studio 2008 is an ideal companion to C# 3.0 programming, but it is not essential.
• The C# 3.0 reference documentation, available on the Microsoft web site (, 529 pages).

All the programs in this book were prepared on an iMac Intel Core 2 Duo running Mac OS X 10.4 and 5 (Tiger and Leopard) and Windows XP on top of the Parallels virtual machine. The editor used was SciTE.

Finding What You Need

This book is intended to serve as a learning resource. In learning about C# 3.0 and design patterns, there will be times when you will need to find a particular feature or a related pattern. These tables are given in Chapter 11 for easy reference. The UML class diagram notation is covered in Chapter 1. You will also find that there are a few programs that, for space reasons, are not presented in full in the text. These are all included in the Appendix.

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “C# 3.0 Design Patterns, by Judith Bishop. Copyright 2008 Judith Bishop, 978-0-596-52773-0.”

If you think your use of code examples falls outside fair use or the permission givenabove, feel free to contact us at


My first thanks are to John Osborn, my editor at O’Reilly, for keeping the faith and getting this book out on time. His care and expertise are much appreciated. To Jeff Pepper, who signed up the book quite a while ago, thanks as well. I’m sorry we could not see the project through together. Thanks also to the reviewers, Eric Lippert, Jim Whitehead, Stefan Gruner, and Pierre-Henri Kuaté, whose insightful comments—no holds barred—led to many revisions but an ultimately much better book. My department at the University of Pretoria provided me with the latest equipment, and my colleagues gave me the time to really concentrate when I needed to. In particular I thank Jan Eloff for his support and friendship. To Carlo Ghezzi of the Politecnico di Milano, who graciously hosted me for the summer of 2007, when much of the first draft was written, grazie mille. My former students Hans Lombard and D-J Miller helped at very short notice with some of the examples, and I really appreciated their fresh minds and dedication to the task.

Writing this book would have been a much less enjoyable experience without the constant support and interest of my talented friends Nigel Horspool, Rob Koenig, and Rudolph Vosser. They never knew quite when the book would really be finished, but now it is. And finally, to my mother, my sons, and my family, whose love and joie de vivre kept me going on this (yet another) book—thank you.