views:

659

answers:

6

I would like to know: are any GoF design patterns are used in the .NET Framework?

BOUNTY:

I have seen the MSDN link below in an answer. Are there any post/video or can you can list patterns and most importantly WHERE it is used?

+4  A: 

Definitely. For instance, Factory pattern is used in ADO.NET data provider classes. Singleton pattern is used in .NET remoting. Dispose pattern is used in resource management.

Mehrdad Afshari
+13  A: 

Here's an article that discusses this very topic:

http://msdn.microsoft.com/en-us/magazine/cc188707.aspx

And now the MVC pattern can be added with ASP.NET MVC. :)

EDIT: Since your edit / request for more info:

Here's an article that lists several patterns and where they are used in the framework. http://www.jot.fm/issues/issue_2006_11/article1.pdf

The Providers in .NET are all the Provider model pattern. http://msdn.microsoft.com/en-us/library/aa479030.aspx

The provider patterns in .NET also use the Strategy Pattern.

The factory pattern is used in several places and here's a sample where it's used in ASP.NET. http://msdn.microsoft.com/en-us/library/ms954600.aspx

Here's a webcast on DP's in .NET: http://msevents.microsoft.com/cui/WebCastEventDetails.aspx?culture=en-US&EventID=1032293567&CountryCode=US

I haven't watched it so I am not sure how much it goes into how they are used in the Framework...

As already mentioned in a comment, the GoF patterns are likely all in use in the .NET framework. Where is not exactly the easiest to answer as the framework is massive and unless MS publishes as such listed in some of the examples given it is not always obvious. The more familiar one is with a pattern the more likely you would notice a framework class that was employing it.

Hopefully the extra links I have added help you.

Additionally, http://www.dofactory.com/Framework/Framework.aspx has a for sale kit ($79-99) that is about teaching how to use/implement GoF patterns in .NET BUT they do list on the reading they will also explain where MS uses them in the Framework.

klabranche
I hate lists like that. The vast majority of GoF Design Patterns are not appropriate for .NET programmers. There are far better patterns that solve the same goals, but do so using the features offered by the .NET platform. Unfortunately they don't have fancy names so they tend to be ignored in literature.
Jonathan Allen
Just giving him what he asked for. :)
klabranche
@Grauenwolf and @Paco: Paco thinks that probably all of them are but Grauenwolf contends that most are probably not appropriate. i have a feeling that Grauenwolf is closer to being correct but i would LOVE to hear a discussion on Grauenwolf's position or a link to such a discussion. Or can you name a pattern or two that are not appropriate and why?
Paul Sasik
As examples, the traditional 'observer' and 'strategy' patterns which are typically implemented with interfaces and classes in other languages are better implemented in .Net/C# with delegates
Kevin Jones
The "Visitor" pattern is one I particularly dislike. The design assumes that you can modify interfaces after the fact as new classes are created. But of course we all know that interfaces in .NET should never be changed after they are published.
Jonathan Allen
A: 

The BeginXXX and EndXXX methods in the BCL are based loosely on the Asynchronous Completion Token pattern.

Brian Gideon
+7  A: 

Read the "Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries". This book will show you the real design patterns that .NET was based on.

Jonathan Allen
I was thinking of suggesting that one... :)
klabranche
Second that! It's Chapter 9 ("Common Design Patterns") in the first edition - I keep that copy at home and the second edition at work. :)
TrueWill
+1: I've got the second edition of this book. It really gives great insight how to design a framework and indeed some examples are given how parts of the .Net framework are designed.
Marc
A: 
  • System.Net.WebClient is an example for the Façade Pattern.
  • System.IO.Stream: Classic Async Pattern
  • System.ComponentModel.BackgroundWorker: Event-Based Async pattern
  • As already mentioned: Dispose pattern is used nearly everywhere in the framework
  • System.Control: Template Method Pattern
PVitt
+5  A: 

The .NET framework uses many of the Gang of Four patterns. Here are just a few examples:

  • Abstract Factory : System.Data.Common.DbProviderFactory. Every member function of this class is a factory method.
  • Builder : The WCF channel construction infrastructure.
  • Factory Method : System.Data.IDbConnection.BeginTransaction(). The type of transaction created depends on the underlying IDbConnection implementation.
  • Adapter : The ADO.NET providers, eg System.Data.SqlClient.SqlConnection, System.Data.OleDb.OleDbConnection etc. Each provider is an adapter for its specific database.
  • Composite : many examples:
    • System.Windows.Forms.Control and its derived classes.
    • System.Web.UI.Control and its derived classes.
    • System.Xml.XmlNode and its derived classes.
  • Decorator : System.Windows.Controls.Decorator (in WPF).
  • Facade : System.Xml.Serialization.XmlSerializer. XmlSerializer hides a complex task (that includes generating assemblies on the fly!) behind a very easy-to-use class.
  • Proxy : The web service proxies generated by svcutil.exe and deriving from System.ServiceModel.ClientBase<TChannel>.
  • Chain of Responsibility : System.Web.UI.Control.OnBubbleEvent() and System.Web.UI.Control.RaiseBubbleEvent().
  • Command : System.Windows.Input.ICommand (in WPF).
  • Interpreter : System.Linq.Expressions.Expression and related classes.
  • Iterator : many examples :
    • System.Collections.IEnumerable.
    • System.Collections.Generic.IEnumerable<T>.
    • System.Data.IDataReader.
  • Memento : The .NET Serializable pattern is a variation on the Memento pattern.
  • Observer : The .NET event mechanism.
  • Strategy : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy and related classes (ok it's not in the framework itself).
  • Visitor : System.Linq.Expressions.ExpressionVisitor (used internally by LINQ).
Paul Lalonde
+1 ... Great list!
Filip Navara