Q-1). What is the Decorator Pattern?

A-1). A pattern that adds functionality to objects dynamically without modifying their code.

Q-2). Give a real-world example of a Decorator.

A-2). Java I/O classes like BufferedReader wrap InputStreamReader.

Q-3). How does the Decorator Pattern differ from Inheritance?

A-3). Inheritance is static and fixed at compile-time, while decorators allow adding behavior at runtime.

Q-4). What design principle does a Decorator follow?

A-4). Open/Closed Principle — extend behavior without modifying the original class.

Q-5). How do you handle the order of multiple decorators?

A-5). Maintain a chain or pipeline, or manage it through configuration or dependency injection.

Q-6). What are the downsides of using many decorators?

A-6). Too many small classes, and it becomes hard to track object behavior and debug.

Q-7). How would you refactor legacy code using large inheritance hierarchies using the Decorator Pattern?

A-7). Extract common behavior into an interface and refactor concrete classes into components and decorators. Use composition over inheritance to add behavior modularly.

Q-8). How would you implement thread-safe decorators in a multi-threaded application?

A-8). Ensure decorators are stateless or synchronize access if they maintain internal state.