views:

100

answers:

5

What classes in the .net framework implement the various design patterns like decorator,factory etc?

+2  A: 

Off the top of my head :

  • The Abstract Factory pattern is used in the ADO.NET 2.0 infrastructure (DbProviderFactory class)
  • The Template Method pattern is used in many parts of the framework (WinForms, WPF...)
  • The Builder pattern is also used quite often (DbConnectionStringBuilder, UriBuilder...)
  • I think the predefined instances (static properties) of StringComparer, Encoding, Color, etc are a variant of the Prototype pattern
  • The Proxy pattern is everywhere when you use .NET remoting
  • Events are a variant of the Observer pattern
  • The Iterator pattern is used in collections
  • The Strategy pattern is used, for instance, in cryptography classes

There are probably more examples, but that's all I can think of right now...

Thomas Levesque
+1  A: 
  • Visitor Pattern in ExpressionVisitor class (Syste.Linq.Expression namespace).
  • Adapter Pattern in System.Web.Abstractions - wrap up various Web classes (HttpRequest, HttpResponse) in a more unit testable way - i.e. HttpResponseBase.
  • Factory Method - Activator.CreateInstance - creates an instance of specified object.
  • Iterator pattern - all implementations of IEnumerable.
Igor Zevaka
The Facade pattern is not meant to provide an abstraction... it's a single entry point to several subsystems
Thomas Levesque
+7  A: 

Well, what your asking for is probably a VERY extensive list, as design patterns are use all over the .NET platform. Here are some examples I can think of off the top of my head:

Adapter

The adapter pattern, a common mechanism of bridging systems and platforms, is implemented in a variety of ways in the .NET framework. One of the most prevalent examples of this in .NET are Runtime Callable Wrappers, or RCW's. RCW's, generated with the tlbimp.exe program, provide adapters that let .NET managed code easily call into legacy COM code via a .NET API.

Factory Method

The factory method pattern is probably one of the most well-known patterns. It is implemented quite commonly throughout the .NET framework, particularly on primitive times, but also on many others. An excellent example of this pattern in the framework is the Convert class, which provides a host of methods to create common primitives from other common primitives.

Additionally, another pervasive form of this pattern are the .Parse() and .TryParse() methods found on many primitive and basic types.

Iterator

The Iterator pattern is implemented via a couple interfaces and some language constructs, like foreach and the yeild keyword in C#. The IEnumerable interface and its generic counterpart are implemented by dozens of collections in the .NET framework, allowing easy, dynamic iteration of a very wide variety of data sets:

IEnumerable<T>
IEnumerator<T>

foreach(var thing in someEnumerable)
{
   //
}

The yeild keyword in C# allows the true form of an iterator to be realized, only incurring the cost of processing an iteration through a loop when that iteration is demanded:

IEnumerable<string> TokenizeMe(string complexString)
{
    string[] tokens = complexString.Split(' ');
    foreach (string token in toekens)
    {
        yield return token;
    }
}

Builder

The Builder pattern is implemented a few times in the .NET framework. A couple of note are the connection string builders. Connection strings can be a picky thing, and constructing them dynamically at runtime can sometimes be a pain. Connection String Builder classes demonstrate the builder pattern ideally:

string connectionString = new SqlConnectionStringBuilder
{
    DataSource = "localhost",
    InitialCatalog = "MyDatabase",
    IntegratedSecurity = true,
    Pooling = false
}.ConnectionString;

Other classes throughout the .NET framework, such as UriBuilder, also implement the builder pattern.

Observer

The observer pattern is a common pattern that allows one class to watch events of another. As of .NET 4, this pattern is supported in two ways: via language-integrated events (tightly coupled observers), and via the IObservable/IObserver interfaces (loosely coupled events).

Classic language events make use of delegates, or strongly-typed function pointers, to track event callbacks in event properties. An event, when triggered, will execute each of the tracked callbacks in sequence. Events like this are used pervasively throughout the .NET framework.

public class EventProvider
{
    public event EventHandler SomeEvent;

    protected virtual void OnSomeEvent(EventArgs args)
    {
        if (SomeEvent != null)
        {
            SomeEvent(this, args); // Trigger event
        }
    }
}

public class EventConsumer
{
    public EventConsumer(EventProvider provider)
    {
        provider.SomeEvent += someEventHandler; // Register as observer of event
    }

    private void someEventHandler(EventArgs args)
    {
        // handle event
    }
}

New with the .NET 4 framework are loosely coupled events. These are accomplished by implementing the IObservable<out T> and IObserver<in T> interfaces, which more directly support the original Observer design pattern. While not directly implemented by any .NET framework types that I am aware of, the core infrastructure for the pattern is an integral part of .NET 4.

public class SomethingObservable: IObservable<SomethingObservable>
{
    private readonly List<IObserver<SomethingObservable>> m_observers;

    public IDisposable Subscribe(IObserver<SomethingObservable> observer)
    {
        if (!m_observers.Contains(observer))
        {
            m_observers.Add(observer);
        }
        var unsubscriber = new Unsubscriber(m_observers, observer)
        return unsubscriber;        
    }

    private class Unsubscriber: IDisposable
    {
        public Unsubscriber(IList<IObserver<SomethingObservable>> observers, IObserver<SomethingObservable> observer)
        {
            m_observers = observers;
            m_observer = observer;
        }

        private readonly IList<IObserver<SomethingObservable>>  m_observers;
        private readonly IObserver<SomethingObservable> m_observer;

        public void Dispose()
        {
            if (m_observer == null) return;
            if (m_observers.Contains(m_observer))
            {
                m_observers.Remove(m_observer);
            }
        }
    }
}

Decorator

The decorator pattern is a way of providing alternative representations, or forms, of behavior through a single base type. Quite often, a common set of functionality is required, but the actual implementation of that functionality needs to change. An excellent example of this in the .NET framework is the Stream class and its derivatives. All streams in .NET provide the same basic functionality, however each stream functions differently.

  • Stream
    • MemoryStream
    • BufferedStream
    • FileStream
      • IsolatedStorageFileStream
    • PipeStream
      • AnonymousPipeClientStream
      • AnonymousPipeServerStream
      • NamedPipeClientStream
      • NamedPipeServerStream
    • CryptoStream
    • GZipStream

Many, many other design patterns are used within the .NET framework. Almost every aspect of .NET, from language to framework to fundamental runtime concepts, are based on common design patterns. Significant portions of the .NET framework, such as ASP.NET, are in and of themselves patterns. Take, for example, the ASP.NET MVC framework, which is an implementation of the web variant of MVC, or Model-View-Controller. The WPF and Silverlight UI frameworks directly support a pattern called MVVM, or Model-View-ViewModel. The ASP.NET pipeline itself is a collection of patterns, including intercepting filter, page controller, router, etc. Finally, one of the most commonly used patterns, composition, is used so extensively in the .NET framework that it is probably one of the most fundamental patterns of the entire framework.

jrista
+1, very complete and detailed answer
Thomas Levesque
Nice. Is the chain of command pattern implemented anywhere?
CodeToGlory
A: 

Provider pattern - used in MembershipProvider, RoleProvider, ProfileProvider.

this. __curious_geek
And here is a nice Visual Studio Template that can help using this pattern: http://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=26.
Steven
Is that a pattern ? never heard of it... at least it's not a GoF pattern
Thomas Levesque
A: 

Also, NullObject

  • String.Empty;
  • EventArgs.Empty;
shatl