IT lexicon › Programming › Decorator pattern

Decorator pattern

Programming På svenska → Updated: 2026-05-23

Gang-of-Four structural pattern — wrap an object and add functionality around it without modifying the original class.

Classic example: BufferedReader(FileReader(...)) — each wrapper adds behaviour (buffered reads, gzip, encryption). Python's @decorator syntax is language-level support for the same idea, applied at definition time.

Better than inheritance when you need to compose features at runtime. Risk: deep wrapping hierarchies become hard to debug. Sweet spot: I/O streams, middleware stacks (Express, ASP.NET), AOP.

← Back to the lexicon