Decorator Design Pattern — Coffee Shop Design

Akshat Sharma
5 min readMay 19, 2023

--

Hey everyone 😄

As a software engineer , we are often asked to design this particular thing . So today in this article I will show you how we can design a coffee shop using Decorator Design Pattern . Before we dive into code let’s have a look on some fundamentals .

What is Decorator Design Pattern ?

The Decorator design pattern is a structural design pattern that allows behavior to be added to an object dynamically without affecting the behavior of other objects in the same class.

Key participants in the Decorator pattern are:

  1. Component: This is the interface or abstract class that defines the methods that will be implemented by concrete components and decorators.
  2. Concrete Component: This class implements the Component interface and defines the base behavior.
  3. Decorator: This is the abstract class that implements the Component interface and maintains a reference to a Component object. It acts as a base class for concrete decorators and provides a common interface for all decorators.
  4. Concrete Decorator: These classes extend the Decorator class and add additional behavior or state. They can wrap around a Component object and modify its behavior dynamically.

The typical flow of the Decorator pattern is as follows:

  1. The client code creates a specific component object or chain of decorators, using the Component interface or class.
  2. The decorators can be stacked one upon another, with each decorator wrapping around the previous one or the core component.
  3. Each decorator modifies the behavior of the component it decorates by adding its own functionality before or after invoking the decorated object’s methods.
  4. The client code interacts with the decorated object through the Component interface, unaware of the specific decorators applied to it.

Let’s understand all of the above theory by designing a simple coffee shop .

Coffee Shop Design -:

Use Case -:

Let’s have a look on all the use cases of our coffee shop design -:

  1. Order beverage .
  2. Add new beverage .
  3. Add new decorators (caremel , milk ,chocolate syrup etc).
  4. Customize beverage using decorators .
  5. Must be able to add multiple decorators .

So these are few use cases which we are going to cover here you can add more to it .

Coffee Shop Implementation -:

Here we are having a Beverage Interface which contians all the basic functionalities that a particular beverage can have . Then we have three different types of Coffee that implements beverage class and we also have a beverage decorator interface that implements beverage interface . Now the question is why this decorator interface implements beverage interface ?So the answer to this question is that although it is decorator but it can have all the basic functionalities that a beverage can have (such as description , price , name etc) , we have made decorator interface in order to distinguish decorators from beverage . Then we have our different decorator classes that implements Beverage Decorator interface .

Now the question is why do we even need this Decorator interface ?

Suppose you have designed the coffee shop and made all the required interfaces and classes . Now you have got a request for let’s say Espresso with Caramel now what you are going to do here is you will make one more class named “Espresso with Caramel” which implements beverage inetrface . Now again you got a request for Espresso + Caramel + Caramel so again you will make a class for it and this way many classes can be formed of all possible combination . Forming numerous class is time consuming , memory consuming and tedious task it makes our code inefficient . So what we have came with is making a decorator interface which is implemented by each and every decorator .

Code -:

Beverage Interface -:

abstract public class Beverage
{
public String description;
abstract public String getDescription();
abstract public double getPrice();

}

We have our Beverage interface with all the basic properties that a bevrage can have .

Espresso Class -:

public class Espresso extends Beverage
{

public Espresso() {
this.description = "Espresso";
}

@Override
public double getPrice()
{
return 100.00;
}

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

Similarly we can make Americano , Cappuccino or any other Coffee class .

Beverage Decorator Interface -:

abstract public class BeverageDecorator extends Beverage
{

}

Now this is funny this interface contains nothing . The reason to this is that it implements Beverage interface .

Caramel (Decorator) Class -:

public class Caremel extends BeverageDecorator
{
private static final int CaremelAddonPrice = 60;
Beverage beverage;

public Caremel(Beverage beverage)
{
this.beverage = beverage;
}
@Override
public double getPrice()
{
return this.beverage.getPrice() + CaremelAddonPrice;
}

@Override
public String getDescription()
{
return this.beverage.getDescription() + " " + "extra Caremel";
}
}

Similary we can make other Decorator class . Now here we have made a Beverage object so this clearly specifies that in which type of coffee you want to add carmel and according to that the price and description is shown .

Coffee Shop Class -:

public class CoffeeShop
{
public static void main(String[] args)
{

Beverage amricano = new Amricano();
amricano = new Milk(amricano);
amricano = new Milk(amricano);
amricano = new Milk(amricano);
amricano = new Milk(amricano);
amricano = new Caremel(amricano);


// System.out.println(amricano.getDescription());
// System.out.println(amricano.getPrice());

Beverage dalgona = new Dalgona();
dalgona = new Milk(dalgona);
dalgona = new Caremel(dalgona);
dalgona = new ChoclateSyrup(dalgona);

System.out.println(dalgona.getDescription());
System.out.println(dalgona.getPrice());


}
}

So here we have our client class which shows the price and description of particular coffee type with or without add on . To add new coffee type or decorator we can simply make a new class for that similar to above ones .

So this was a very simple design of Coffee shop . Hope you understood it well .

You can find complete code here -: https://github.com/akshatsh0610/Coffee-Shop-Design-Low-Level-Design-

If I can add something more to it please let me know in comments .

Please leave some claps for it as it motivates me to write more such blogs.

Thank you 😄

--

--