A software design pattern used to enhance the functionality of an object at…
2017
A software design pattern used to enhance the functionality of an object at run-time is :
- A.
Adapter
- B.
Decorator
- C.
Delegation
- D.
Proxy
Attempted by 97 students.
Show answer & explanation
Correct answer: B
Correct answer: Decorator
Explanation: The decorator pattern attaches additional responsibilities to an object dynamically. It provides a flexible alternative to subclassing for extending functionality at run-time without changing the original class.
How it works: Decorator classes wrap the original object and add behavior before or after delegating requests to the wrapped object.
Why it fits: It lets you stack or combine decorators at run-time to enhance functionality without modifying the underlying object or creating many subclasses.
Use case example: To add logging or caching to an object at run-time, create a LoggingDecorator or CachingDecorator that wraps the original object and adds the behavior.
Why the other choices are not the best fit:
Adapter: Converts one interface to another so incompatible interfaces can work together; its goal is compatibility, not adding new responsibilities.
Delegation: A technique where an object hands tasks to another object. It is a useful principle but not the specific structural pattern intended for dynamically layering responsibilities.
Proxy: Acts as a surrogate to control access, add lazy initialization, or perform checks. While it can introduce behavior, its primary intent is access control rather than transparent extension of responsibilities.