LLMpediaThe first transparent, open encyclopedia generated by LLMs

Decorator (programming)

Generated by DeepSeek V3.2
Note: This article was automatically generated by a large language model (LLM) from purely parametric knowledge (no retrieval). It may contain inaccuracies or hallucinations. This encyclopedia is part of a research project currently under review.
Article Genealogy
Parent: TypeScript Hop 4
Expansion Funnel Raw 62 → Dedup 49 → NER 12 → Enqueued 11
1. Extracted62
2. After dedup49 (None)
3. After NER12 (None)
Rejected: 37 (not NE: 37)
4. Enqueued11 (None)
Similarity rejected: 1
Decorator (programming)
NameDecorator
TypeStructural pattern
IntentAttach additional responsibilities to an object dynamically.
SolutionProvide a flexible alternative to subclassing for extending functionality.

Decorator (programming). In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to individual objects, dynamically, without affecting the behavior of other objects from the same class. This pattern is a structural pattern, as defined in the influential book Design Patterns by the Gang of Four. It provides a flexible alternative to subclassing for extending functionality, adhering to the Open/Closed Principle by allowing classes to be open for extension but closed for modification.

Overview

The decorator pattern is used to extend or alter the functionality of objects at run-time by wrapping them in an object of a decorator class. This provides a more flexible approach than inheritance, as it avoids creating a complex hierarchy of subclasses. The pattern is fundamental in many frameworks, such as the Java I/O classes and the .NET Framework Stream implementations. The concept is also prevalent in graphical user interface libraries, like those in Qt or Java Swing, for adding features like scrolling or borders to components.

Design pattern structure

The pattern involves four key participants: the Component, which defines the interface for objects that can have responsibilities added; the ConcreteComponent, which defines an object to which additional responsibilities can be attached; the Decorator, which maintains a reference to a Component object and conforms to the Component interface; and one or more ConcreteDecorator classes, which add responsibilities to the component. This structure is often visualized using a UML class diagram. The Gang of Four described this as a way to achieve multiple inheritance-like behavior through object composition.

Implementation examples

A classic example is enhancing Java I/O streams. The InputStream class is the Component, FileInputStream is a ConcreteComponent, and FilterInputStream is the Decorator. ConcreteDecorators like BufferedInputStream and DataInputStream add buffering or data-type reading methods. In Python, the pattern is supported syntactically via decorators, which are functions that modify the behavior of other functions or methods. In C++, it can be implemented using templates and inheritance, as seen in the STL's use of iterator adaptors.

Use cases and applications

Common applications include adding graphical user interface embellishments like borders, scrolling, or shadows to widgets in toolkits such as Java Swing or WPF. It is also extensively used in middleware and web frameworks; for instance, ASP.NET uses decorators (called attributes) for adding concerns like authorization or logging to controllers. In game development, the pattern can dynamically add abilities or states to characters or items without altering their core class definitions.

Advantages and disadvantages

Primary advantages include greater flexibility than static inheritance, adherence to the single-responsibility principle by dividing functionality into classes with distinct concerns, and compliance with the Open/closed principle. Disadvantages can involve a large number of small objects, making a system hard to understand and debug, and the complexity of instantiating heavily decorated objects, which can impact performance. Overuse can lead to designs that are less straightforward than using subclassing or mixins in some languages.

The decorator pattern is often compared with the Adapter pattern, which changes an object's interface, whereas Decorator changes its responsibilities. The Composite pattern can be used with Decorator to build complex structures. The Strategy pattern offers a different approach to changing an object's behavior by encapsulating algorithms. The Proxy pattern provides a similar structural wrapper but typically controls access rather than adding functionality. These patterns are all catalogued in the seminal work by Gang of Four. Category:Software design patterns