Decorator Design Pattern in Java

The Decorator Design Pattern is a structural design pattern that allows you to add behavior or functionality to an object dynamically, without changing its structure. The Decorator pattern wraps an object and adds new functionality or behavior to the object at runtime by creating a decorator object that contains the original object.

The Decorator Design Pattern in Java has the following properties:

  1. Dynamic behavior: The Decorator Design Pattern provides a way to add behavior or functionality to an object dynamically at runtime, without modifying its structure or changing its original behavior.
  2. Composition: The Decorator Design Pattern uses composition to add behavior or functionality to an object. Decorator objects contain a reference to the original object and add their own behavior or functionality to it.
  3. Open-closed principle: The Decorator Design Pattern follows the open-closed principle, which states that classes should be open for extension but closed for modification. The Decorator pattern allows you to extend the behavior of an object without modifying its source code.
  4. Transparent to clients: The Decorator Design Pattern is transparent to clients. The clients only interact with the decorated object and are not aware of the decorators that are adding behavior or functionality to the object.
  5. Multiple decorators: The Decorator Design Pattern allows you to add multiple decorators to an object, each adding their own behavior or functionality to the object.
  6. Maintains object identity: The Decorator Design Pattern maintains the identity of the original object. Each decorator object only adds behavior or functionality to the object, without changing its identity or class.

In Java, the Decorator Design Pattern can be implemented in the following way:

  1. Define a common interface or abstract class that represents the component that you want to decorate.
    public interface Component {
        void operation();
    }
  2. Implement the interface or abstract class to create a concrete component.
    public class ConcreteComponent implements Component {
        @Override
        public void operation() {
            System.out.println("ConcreteComponent operation");
        }
    }
  3. Create a decorator class that implements the same interface or abstract class as the component.
    public abstract class Decorator implements Component {
        protected Component component;
    
        public Decorator(Component component) {
            this.component = component;
        }
    
        @Override
        public void operation() {
            component.operation();
        }
    }
  4. Implement the decorator class to add new behavior or functionality to the component.
    public class ConcreteDecorator extends Decorator {
        public ConcreteDecorator(Component component) {
            super(component);
        }
    
        @Override
        public void operation() {
            super.operation();
            addedBehavior();
        }
    
        public void addedBehavior() {
            System.out.println("Added behavior by ConcreteDecorator");
        }
    }

    In this implementation, the ConcreteDecorator class extends the Decorator class and adds new behavior to the component by implementing the addedBehavior() method. The operation() method of the ConcreteDecorator class calls the operation() method of the component and then adds the new behavior.

  5. Use the ConcreteComponent and ConcreteDecorator classes to create and decorate the component.
    public class Main {
        public static void main(String[] args) {
            Component component = new ConcreteComponent();
            component.operation();
    
            Component decoratedComponent = new ConcreteDecorator(component);
            decoratedComponent.operation();
        }
    }

In this example, the ConcreteComponent class represents the original object and the ConcreteDecorator class represents the decorator object that adds new behavior to the component. The decoratedComponent object is created by passing the original object to the constructor of the ConcreteDecorator class.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial