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
Published at
2026-10-16 10:07:57 UTCEvent JSON
{
"id": "10ea19a8767688eb5dd9da1b62e4fe1a7c0a43b0ab2fc4d678eae4f74a7fd573",
"pubkey": "e68a0856e90d9997b1f721e905249896412dd570399a987b2e8fd5e7fcfe2279",
"created_at": 1792145277,
"kind": 1,
"tags": [
[
"t",
"tip"
]
],
"content": "Learn: Decorator\n\nWrap an object in another object of the same type that adds behavior before or after it.\n\n```csharp\nINotifier n = new Loud(new Plain());\nConsole.WriteLine(n.Send(\"hi\"));\n\ninterface INotifier { string Send(string m); }\nclass Plain : INotifier { public string Send(string m) =\u003e m; }\nclass Loud(INotifier inner) : INotifier { public string Send(string m) =\u003e inner.Send(m).ToUpper() + \"!\"; }\n```\n\nIt prints HI!.\nYou 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.\n\nRead more: https://www.geeksforgeeks.org/system-design/decorator-pattern/\n\n#tip · TIP-055",
"sig": "8ada45511f18eb25f78f292eedbdfbed13bd3a40d216a6b623adf08505a455e6af14b7514cd6da633a8fbbce980c07f738e14debae2fb367ce845dc08506d8e9"
}