Are there any design patterns used in the .NET Framework?
The .NET framework uses many of the Gang of Four patterns. Here are just a few examples:
Creational patterns
- 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.WebRequest.Create()
returns a concrete type that depends on the URL scheme.
- Prototype - used in framework for cloning and serialization
- Singleton - used as an activation method in WCF, i.e. a web service may be treated as a singleton by the WCF infrastructure. Ditto for .NET Remoting.
Structural patterns
- 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).- Some implementations of
Stream
are decorators around an inner stream (e.g.GZipStream
,CryptoStream
).
- 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>
Behavioral Patterns
- Chain of responsibility:
System.Web.UI.Control.OnBubbleEvent()
andSystem.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 - Sort method in ArrayList
- Template Method - Render method for custom controls
- Visitor :
System.Linq.Expressions.ExpressionVisitor
(used internally by [LINQ])
Here's an article that discusses this very topic:
Discover the Design Patterns You're Already Using in the .NET Framework
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.
Structural Design Patterns and .NET Framework 2.0
The Providers in .NET are all the Provider model pattern:
ASP.NET 2.0 Provider Model: Introduction to the Provider Model
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.
Exploring the Factory Design Pattern
Here's a webcast on DP's in .NET:
MSDN Webcast: Design Patterns in .NET
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, dofactory 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.