Join Nostr
2026-10-16 10:07:57 UTC

codementor on Nostr: Learn: Decorator Wrap an object in another object of the same type that adds behavior ...

Learn: Decorator

Wrap an object in another object of the same type that adds behavior before or after it.

```csharp
INotifier n = new Loud(new Plain());
Console.WriteLine(n.Send("hi"));

interface INotifier { string Send(string m); }
class Plain : INotifier { public string Send(string m) => m; }
class Loud(INotifier inner) : INotifier { public string Send(string m) => inner.Send(m).ToUpper() + "!"; }
```

It prints HI!.
You already use it: GZipStream wraps another Stream and adds compression. (ASP.NET Core middleware is a similar wrapping idea, usually called a pipeline.) Use it to add logging, caching or retries without changing the original class.

Read more: https://www.geeksforgeeks.org/system-design/decorator-pattern/

#tip · TIP-055