26

Which design patterns are build-in supported by C# regardless framework version? I'm thinking of patterns such as Observer pattern that can be found in interface IObservable. ObservableCollection, INotifyPropertyChanged etc.

Please provide with the namespace of the pattern in your answers!

Amir Rezaei
  • 4,948
  • 6
  • 33
  • 49
  • For quite a long time I was thinking that extension methods implement the decorator pattern, but they don't: Decorator overrides and extends existing methods transparently whilst extension methods add new methods to an existing class. Still I'd be interested in whether there is a design pattern for that! – chiccodoro Oct 28 '10 at 08:56

8 Answers8

27

Action<T> (normally used as visitor pattern)

Discover the Design Patterns You're Already Using in the .NET Framework (MSDN Magazine)

Example

public class Root
{
    //Private and not exposed in a IList property = Encapsulation
    private List<Node> _nodes = new List<Node>(); 

    public void Accept(Action<Node> visitor)
    {
        // Controlled enumeration, can for instance handle exceptions in here.
        foreach (var item in _nodes)
        {
            visitor(node);
        }
    }
}

// usage
root.Accept(node => Console.WriteLine(node));
jgauffin
  • 99,844
  • 45
  • 235
  • 372
11

Creational Patterns

Abstract Factory

  • System.Data.Common.DbProviderFactory

Builder

  • System.Text.StringBuilder
  • System.Data.Common.DbConnectionStringBuilder

Factory Method

  • System.Activator
  • System.Net.WebRequest

Prototype

  • System.ICloneable

Singleton

  • System.StringComparer.InvariantCulture
  • System.StringComparer.InvariantCultureIgnoreCase

Structural Patterns

Adapter

  • System.IO.StreamReader

Bridge

  • System.Globalization.CultureInfo

Composite

  • System.ComponentModel.IComponent

Decorator

  • System.IO.Stream

Facade

  • System.Environment
  • System.String

Flyweight

  • System.StringComparer

Proxy

  • System.Net.WebClient
  • System.Runtime.Remoting.Proxies.RealProxy
  • System.ServiceModel.ICommunicationObject

Behavioral Patterns

Chain Of Responsibility

  • Microsoft.Practices.EnterpriseLibrary.Logging.Logger

Command

  • System.Windows.RoutedEventArgs

Interpreter

  • System.IFormatProvider
  • System.Text.RegularExpressions.Regex

Iterator

  • System.Collections.IEnumerable
  • System.Data.IDataReader

Mediator

  • System.Threading.Timer

Memento

  • System.Runtime.Serialization.ISerializable

Observer

  • System.EventHandler
  • System.IObservable

State

  • ??

Strategy

  • System.Collections.Generic.IComparer

Template Method

  • System.Web.UI.Page

Visitor

  • System.Linq.Expressions.ExpressionVisitor
6

Iterator is one (all collection classes and arrays can use the foreach statement for iteration).

Another is the observer pattern - pretty much this is what events are. In 4.0 the IObservable and ObservableCollection were added.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
4

StringBuilder uses the builder design pattern..
And there is the DbDataAdapter class (adapter pattern).
The Null Object pattern is broadly used as well.

Oren A
  • 5,870
  • 6
  • 43
  • 64
4

Observer pattern. All our events and delegates are raised through the observer pattern.

Factory pattern. Connection strings and db providers from factory.

Iterator Pattern: Ienumerable, Ienumerators in our foreach statements

Adapter: COM communication. Runtime Callable Wrappers(RCW)

Template: Used in several places esp in ASP.NET classes where you can override to provide new implementation

Proxy: For all our webservice calls. in c# 3.0 we got proxy collections as well.

There could be many more. But these are the ones that came to my mind

Hunter
  • 2,370
  • 2
  • 20
  • 24
3

Abstract Factory: System.Data.Common.DbProviderFactory

Community
  • 1
  • 1
Ruben
  • 6,367
  • 1
  • 24
  • 35
0

The delegation pattern (that's what the delegates and expresssions are for)

Adrian Grigore
  • 33,034
  • 36
  • 130
  • 210
0

The proxy pattern is used often.