Decorator design pattern is a Structural design pattern. This pattern is used to provide additional functionalities along with the existing functionalities for an object. Here, we do not modify the existing functionalities of the object, instead we add additional functionalities to it.

GoF Definition : Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for extending functionality.

Main advantage of decorator design pattern is that we can create decorator classes for any existing classes in a system without modifying the existing objects structure or functionality. We can attach/detach decorator classes any time to add/remove functionalities to the existing objects.

Decorator Pattern Example in Java

Following is an example implementation of decorator design pattern. Here, the basic idea is to create decorator class(es) and pass actual object instance to it, so that we can add additional functionalities to the actual object.

We create a Component interface, with required method(s) first. We create ConcreteComponent and DecoratorComponent implementation classes. Then we create two decorator classes called DecoratorComponentEX1 and DecoratorComponentEX2. Finally, we test the design pattern by creating the class DecoratorExample.

Create a Component interface with required method as shown below. This interface is implemented by actual object and decorator object and makes sure that all the required methods are implemented. In our example, we have one method called callMethod().

Component.java

package com.asb.decorator;

public interface Component {
	
	public void callMethod();
}

Create a ConcreteComponent class as shown below. This class will contain actual implementation and we will create decorator class(s) for this class in the next part.

ConcreteComponent.java

package com.asb.decorator;

public class ConcreteComponent implements Component {

	@Override
	public void callMethod() {
		
		System.out.println("Concrete component method called!!");
	}
}

Create a decorator class called DecoratorComponent as shown below. This class will have ConcreteComponent class’s instance and calls it’s callMethod() method internally.

DecoratorComponent.java

package com.asb.decorator;

public abstract class DecoratorComponent implements Component {

	protected Component cp;
	
	public void setComponent(Component cp) {
		this.cp = cp;
	}
	
	@Override
	public void callMethod() {
		
		if(null != cp) {
			this.cp.callMethod();
		}
	}
}

Now, we will create a decorator class to decorate the actual object’s functionality. This class extends DecoratorComponent class and overrides super class’s callMethod() method as shown below. It also adds some additional functionality by adding a message, along with actual object’s functionality.

DecoratorComponentEX1.java

package com.asb.decorator;

public class DecoratorComponentEx1 extends DecoratorComponent {

	@Override
	public void callMethod() {
		
		super.callMethod();
		System.out.println("Adding extra decorations to Concrete Component from EX1 class");
	}
}

Add another decorator class DecoratorComponentEX2 as shown below. This is similar to the previous class with a different message. 🙂 to show how multiple decorator classes can be added according to our requirement.


DecoratorComponentEX2.java

package com.asb.decorator;

public class DecoratorComponentEx2 extends DecoratorComponent {

	@Override
	public void callMethod() {
		
		super.callMethod();
		System.out.println("Adding extra decorations to Concrete Component from EX2 class");
	}
}

Finally, to test the created classes, create a DecoratorExample class with main method as shown below. Here, we create a Concrete object instance first. To decorate this object, we use our two decorator classes by creating instances of the decorator class and passing the concrete object instance to it’s setComponent() method. finally, we are calling the callMethod() method of each decorated object. Output of the executed program is shown in the below image.

DecoratorExample.java

package com.asb.decorator;

public class DecoratorExample {

	public static void main(String[] args) {
		
		System.out.println("Decorator design Pattern Demo...");

		ConcreteComponent component = new ConcreteComponent();
		
		DecoratorComponetEx1 ex1 = new DecoratorComponetEx1();
		DecoratorComponentEx2 ex2 = new DecoratorComponentEx2();
		
		ex1.setComponent(component);
		ex2.setComponent(component);
		
		System.out.println("\nCalling Decorator EX1::");
		ex1.callMethod();
		System.out.println("\nCalling Decorator EX2::");
		ex2.callMethod();
	}
}

Output :

Decorator Design Pattern in Java

You May Also Interested in :