Decorator Design Pattern in Java

This is one of the structural design patterns. This pattern acts as a wrapper to the existing class. The Decorator pattern lets us attach additional responsibilities and modify an instance functionality dynamically without affecting the original object or other objects. Decorators provide a flexible alternative to subclassing for extending functionality, using composition instead of inheritance.

Class Diagram

Decorator Pattern in Java

Implementation

Create an Interface

Create an implementation class – VegPizza

Create an another implementation class – NonVegPizza

Create an abstract class – SubstanceDecorator

Create a concrete class – ChickenDecorator

Create another concrete class – CheeseDecorator

Create a test class for testing the Decorator Pattern

Run the above class. You will see the below output in the console.

Output 

That’s all. Thank you for your reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.