Decorator – A Structural Design Pattern

Decorator Design Pattern – Initial Concept

This springcavaj – Structural Design Pattern page briefly describes the Initial Concept of Structural Design Patterns, advantages, disadvantages, benefits, various types, common problems, and a few interview questions discussed.

This guide provides a comprehensive understanding of Structural Design Patterns for designing and architecting an application.

What is a Decorator Design Pattern

The Decorator Pattern is a structural design pattern that allows behavior to be added to an individual object dynamically, without affecting the behavior of other objects from the same class. It wraps the original object with a decorator object that adds new behavior.

What are the features of the Decorator Design Pattern?

  • Open/Closed Principle – Extend behavior without modifying code.
  • Flexibility – Promotes flexibility over static inheritance.
  • Composable objects – Objects are composable at runtime.

What are the advantages of the Decorator Design Pattern?

  • Responsibilities – Add responsibilities dynamically.
  • Class Explosion – Avoid class explosion (as with inheritance).
  • Extensible – Easily extend the behavior of a class.
  • Composable – Multiple Decorators can be stacked.

What are the disadvantages of the Decorator Design Pattern?

  • Too many classes – Can result in many small classes.
  • Debugging & Tracing – Debugging and tracing can be difficult.
  • Order of behavior – Order of decorators can affect behavior and is not always obvious.

What are the use cases of the Decorator Design Pattern?

  • Java I/O (e.g., BufferedReader over InputStreamReader)
  • UI components with scrollbars, borders, etc.
  • Logger functionality – adding timestamp, log level, etc.
  • Encryption/compression wrappers.

Brief Description of Implementation

I have developed a small program to help you understand the working principle of the Bridge Design Pattern. The code is uploaded to GitHub as the springcavaj-designpattern repository. Please clone the application and import it into any of the supported IDs, such as STS or Eclipse.

After successfully importing the above code base, one will find a class named DecoratorMasterclassApplication.java in the com.springcavaj.designpattern.decorator package. Please see the screenshot attached below.

Here you can see the class under the com.springcavaj.designpattern.decorator package is DecoratorMasterclassApplication.java. Right-click on the class, select the Run As option, and then Java Application. It will run and generate the output as provided below.

INFO com.springcavaj.designpattern.decorator.DecoratorMasterclassApplication - Decorator Design Pattern Example

INFO com.springcavaj.designpattern.decorator.DecoratorMasterclassApplication - Latte Coffee Rs. 235.0

INFO com.springcavaj.designpattern.decorator.DecoratorMasterclassApplication - Latte Coffee, Milk, Sugar Rs. 237.0

INFO com.springcavaj.designpattern.decorator.DecoratorMasterclassApplication - Cappuccino Coffee Rs. 435.0

INFO com.springcavaj.designpattern.decorator.DecoratorMasterclassApplication - Cappuccino Coffee, Milk, Sugar Rs. 437.0

Detailed Explanation

The goal of the above code is to enhance the behavior of Coffee by adding ingredients Sugar, Milk, etc., without changing the LatteCoffee and CappuccinoCoffee objects.

Coffee.java

It is the common contract for all types of coffee, including decorators. getDescription() and getCost() are the two methods, so every decorator or concrete coffee must implement these.

public interface Coffee {
	String getDescription();
    double getCost();
}

LatteCoffee.java – A Concrete Implementation

public class LatteCoffee implements Coffee {
	
	public String getDescription() {
        return "Latte Coffee";
    }
    public double getCost() {
        return 235.0;
    }
}

CappuccinoCoffee.java – A Concrete Implementation

public class CappuccinoCoffee implements Coffee {
	
	public String getDescription() {
        return "Cappuccino Coffee";
    }
    public double getCost() {
        return 435.0;
    }
}

CoffeeDecorator.java

It is an abstract class that provides a wrapper that forwards calls to the wrapped object. It’s not meant to be used directly, but extended by concrete decorators. Furthermore, it holds the reference to Coffee, the object being decorated.

public abstract class CoffeeDecorator implements Coffee {
	
	protected Coffee coffee;
	
	public CoffeeDecorator(Coffee coffee) {
		super();
		this.coffee = coffee;
	}

	@Override
	public String getDescription() {
		return coffee.getDescription();
	}

	@Override
	public double getCost() {
		return coffee.getCost();
	}

}

MikDecorator.java & SugarDecorator.java – Concrete Implementations

public class MilkDecorator extends CoffeeDecorator {
	
	public MilkDecorator(Coffee coffee) {
        super(coffee);
    }

    public String getDescription() {
        return super.getDescription() + ", Milk";
    }

    public double getCost() {
        return super.getCost() + 1.5;
    }

}
public class SugarDecorator extends CoffeeDecorator {

	public SugarDecorator(Coffee coffee) {
        super(coffee);
    }

    public String getDescription() {
        return super.getDescription() + ", Sugar";
    }

    public double getCost() {
        return super.getCost() + 0.5;
    }
}

Lastly, the class named DecoratorMasterclassApplication.java is there to run the application and see how the behavior of Coffee is enhanced by adding Milk, Sugar.

Steps to run the application

The complete Run application steps are provided in this README.md file, but still, I am still providing the steps to run points below.

  • Clone the sprincavaj-designpattern application from the GitHub repository.
  • Import the application as a Maven application, either in STS or Eclipse.
  • Find the DecoratorMasterclassApplication.java class.
  • Right-click on the file and select Run As -> Java Application.

GitHub Code Link

Download the Source Code from GitHub

Common Problems Faced in Implementing the Decorator Pattern

Decorator Design Pattern Common Problems

Interview FAQs

Decorator Design Pattern Interview FAQs

Other Useful Links

Implementations of various components of Spring Boot

Other Design Pattern Links

Creational Design Patterns

Factory Design Pattern

Abstract Factory Design Pattern

Builder Design Pattern

Prototype Design Pattern

Singleton Design Pattern

Structural Design Patterns

Adapter Design Pattern

Bridge Design Pattern

Composite Design Pattern