Design patterns used in the .NET framework?
.net, design-patterns
Solution
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.
Problem
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.