A simple example of the decorator design pattern in Java.
This is very popular example (and very illustrative) of the Pizza Shop.
Create a Pizza Interface.
We will create a concrete PlainPizza in a second, by implementing this interface.
1 | interface Pizza { |
Concrete PlainPizza class.
We are giving some default values to our Plain Pizza which will be dressed with the toppings.
1 | class PlainPizza implements Pizza { |
Create a Topping abstract class.
It implements Pizza as well, and HAS-A pizza; This will enable Toppings to be wrapped around each other.
This is our Decorator.
1 | abstract class Topping implements Pizza { |
Create a CheeseTopping class
Our first topping type. We are using pizza instance we defined in Decorator, and adding extra values.
1 | class CheeseTopping extends Topping { |
Create a PepperoniTopping class
Our second topping type. Obviously, we can add as many as we like.
1 | class PepperoniTopping extends Topping { |
Create your client
An actual user of your pizza Decorator. You can see how Decorators are wrapping Pizzas and other decorators.
1 | class Main { |
Run it on repl.it:
https://repl.it/@NenadP/Decorator-pattern-example#Main.java
UML Diagram for our Pizza Decorator pattern example:

Github example:
https://github.com/NenadP/super-simple-coding/tree/master/src/designpatterns/decorator
Wikipedia:
https://en.wikipedia.org/wiki/Decorator_pattern#cite_note-1