views:

412

answers:

6

One way to increase your understanding of design patterns is to discover how patterns are used in the .NET framework.

Have you found any examples of design patterns in the .NET framework? In your answer please give a short description of the pattern, and example of how it is used in the framework.

Example answer:

The Strategy Design Pattern decouples an algorithm from the class that uses it by encapsulating the algorithm into a separate class. This allows for switching of algorithms.

The Sort method of the List class is an example of the Strategy pattern.

public void Sort(IComparer<T> comparer)

By accepting an IComparer interface, users of the class can switch the sorting algorithm at runtime.

+2  A: 

Events in the .Net Framework follow the Observer Pattern

Andreas Grech
+2  A: 
  • ADO.Net is all about Abstract Factory for getting rid of the details of connecting to data sources
  • Events are an implementation of the Observer pattern
  • .Net iterators are an implementation of the Iterator pattern
womp
+2  A: 

An obvious one is the Iterator pattern. using the IEnumerator class in the framework:

Iterators in the .NET Framework are called "enumerators" and represented by the IEnumerator interface. IEnumerator provides a MoveNext() method, which advances to the next element and indicates whether the end of the collection has been reached; a Current property, to obtain the value of the element currently being pointed at; and an optional Reset() method, to rewind the enumerator back to its initial position. The enumerator initially points to a special value before the first element, so a call to MoveNext() is required to begin iterating.

Joel Martinez
+3  A: 

The Decorator Pattern is used on the Stream classes:

  • System.IO.Stream
    • System.IO.BufferedStream
    • System.IO.FileStream
    • System.IO.MemoryStream
    • System.Net.Sockets.NetworkStream
    • System.Security.Cryptography.CryptoStream

The subclasses decorate Stream because they inherit from it, and they also contain an instance of Stream that is set up in the constructor.

CMS
+1  A: 

Adapter Pattern in the DataAdapter used with various data sources such as OleDB, Sql, and Oracle.

JB King
+2  A: 

This artical seems to be good:- http://msdn.microsoft.com/en-us/magazine/cc188707.aspx#S5

Sutikshan Dubey