要增加你的设计模式的理解的一种方法是,以发现如何图案在.NET框架中使用。
One way to increase your understanding of design patterns is to discover how patterns are used in the .NET framework.
你有没有发现,设计模式任何的例子在.NET框架?在你的答案请给图案的简短说明它是如何使用的框架,例如。
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.
示例答案:
战略设计模式脱钩从使用它的算法封装到一个单独的类的类的算法。这允许算法切换
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.
List类的排序方法是策略模式的一个例子。
The Sort method of the List class is an example of the Strategy pattern.
public void Sort(IComparer<T> comparer)
通过接受一个的Icomparable接口类的用户可以在运行时切换排序算法。
By accepting an IComparer interface, users of the class can switch the sorting algorithm at runtime.
在装饰图案是用在Stream类:
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 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.