A Beginner Guide To Decorator Pattern

Which is your favourite Gun in PUBG?

Mine is this one in the picture below. Who will name this? SCAR-L! That’s right. It needs 5.56 mm bullet and is pretty handy. More about SCAR-L here.

But! let’s ask our friends in Pochinki? Has someone got 4x zoom?

Why? Because you know that the assault rifle works best with 4x zoom and while we are on it, we would also need compensator and suppressor. It will reduce recoil and reduce firing sound as well as echo. Eh! Pochinki can be heartbreaking, no wants to die soon.

Good for us! But Bad for PUBG developers!

Manager announced in the meeting that we will need to implement this feature where we will allow players to buy their favourite gun and their accessories. Players can buy the gun or some of the accessory or all of the accessory and they would be charged accordingly.

And one of the developers came up with the class diagram as shown below:

Meeting room was filled with questions:

"What if they want to add handle grip? Will we make six more classes?"

"And what about other guns? AKM would need 10 more classes? How many classes will we need? Such pain!"

"Can't we use Decorator Pattern here?, Looks like the perfect case for the decorator pattern here"

Well, the guy went back to his study table to learn decorator pattern and let’s see what he studied!

Woah! One chapter! And change in perspective of our developer.

"What If I just start with a gun, say SCAR-L and add the accessories as the player buy it?"

Let’s take it step by step. Suppose someone wants to buy SCAR-L with all three accessories mentioned above.

  1. Take an object of SCAR-L
  2. Decorate (or add) the SCAR-L with 4x zoom object
  3. Decorate the SCAR-L with suppressor object
  4. Decorate the SCAR-L with compressor object
  5. Call the cost method and let each object delegate to add on the cost using cost method of accessories.

Meeting called to discuss new class diagram using decorator pattern

Decorator Pattern at work

Looks Good. Can we implement this?

Usual story, project wants to ship this feature asap. How would the implementation look like?

public abstract class Gun {
private Double cost;
public Double getCost() {
return cost;
view raw Gun.java hosted with ❤ by GitHub

This class would be extended by all the guns. We will see that in a while. Let’s make an abstract class for gun accessory.

public abstract class GunAccessories extends Gun {
public class Scarl extends Gun {
public Scarl() {
cost = 100;
view raw Scarl.java hosted with ❤ by GitHub
public class AKM extends Gun {
public AKM() {
cost = 200;
view raw AKM.java hosted with ❤ by GitHub

We are all set with example of AKM and Scar-L. Can we have some accessories please?

public class Comepensator extends GunAccessories {
Gun gun;
public Comepensator(Gun gun) {
cost = 10;
this.gun = gun;
public double getCost(){
return cost + gun.getCost();
public class Suppressor extends GunAccessories {
Gun gun;
public Suppressor(Gun gun) {
cost = 5;
this.gun = gun;
public double getCost(){
return cost + gun.getCost();
view raw Suppressor.java hosted with ❤ by GitHub
public class 4xZoom extends GunAccessories {
Gun gun;
public 4xZoom(Gun gun) {
cost = 5;
this.gun = gun;
public double getCost(){
return cost + gun.getCost();
view raw 4x hosted with ❤ by GitHub

Time for a main Method

public class GunShop{
public static void main(String args[]){
Gun scarl = new Scarl();
scarl = new Compensator(scarl);
scarl = new Supressor(scarl);
scarl = new 4xZoom(scarl);
System.out.println("Price is "+scarl.getCost());
view raw GunShop.java hosted with ❤ by GitHub


Now that we have seen an example of decorator pattern. Let’s define it briefly.

Decorator pattern attaches additional responsibilities to an object. Decorator pattern involves a set of decorator classes(like 4xZoom, Suppressor, Compensator) that are used to wrap concrete components(like AKM, Scarl). Decorator classes have the same type as components they decorate(Gun in our case).


  • Decorators can result in many small objects
  • Any number of decorators can be wrapped over the concrete component
  • Decorators allow us to extend the behaviour without need to modify the existing code. For example, a plane gun can be extended to one with zoom
  • Decorators are invisible to client


Head First Design Pattern – The Decorator Pattern

SO – Example of design pattern

If you liked this article and would like one such blog to land in your inbox every week, consider subscribing to our newsletter: https://skillcaptain.substack.com

One thought on “A Beginner Guide To Decorator Pattern

Add yours

Leave a Reply

Up ↑

%d bloggers like this: