Decorator pattern in the .NET BCL

The decorator pattern attaches additional responsibilities to an object dynamically. The inheritance is always not feasible, because it is static and applies to an entire class. Decorators provide a flexible alternative to sub-classing for extending functionality. The pattern helps add behavior or state to individual objects at runtime. The .NET Framework uses decorators in the case of stream processing classes. The hierarchy of stream processing classes are as follows:

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

The following code snippets show how one can use FileStream to read contents from an operating ...

Get .NET Design Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.