Decorator Pattern in Java

Decorator Pattern is a Structural Pattern in which, functionality can be added to an existing object without changing its structure.

I. OVERVIEW

Decorator Pattern defines one or many Classes that wrap original Class. Everytime we decorate original Class with a Decorator Class, a functionality is added.
decorator-pattern-overview
There is an abstract Decorator Class which implements Component interface to get its original operation() function, then each concrete Decorator override operation() method to add its own functionality decorate() using instance of original Class Component.
decorator-pattern-structure
So, when Client wants to add more functionality, it just calls appropriate constructor method corresponding to specific Decorator.

II. PRACTICE

1. Project Overview

At first, a person could buy a new House. We give him an option that he can pay more money to add a Garden around his house, and then, if he think he should have a Pool, we can create it for him.

So we need 2 Concrete Decorators: Garden and Pool extending abstract Class HouseDecorator.
decorator-pattern-demo

2. Step by Step

2.1- Create IHouse interface and its implementation – SimpleHouse:

IHouse.java


package com.javasampleapproach.decorator.pattern;

public interface IHouse {

	public String description();
}	

SimpleHouse.java


package com.javasampleapproach.decorator.pattern;

public class SimpleHouse implements IHouse {

	public SimpleHouse() {
		System.out.println("buy a house");
	}

	@Override
	public String description() {
		return "House";
	}

}	

2.2- Create HouseDecorator abstract class:


package com.javasampleapproach.decorator.pattern;

public abstract class HouseDecorator implements IHouse {

	protected IHouse house;

	public HouseDecorator(IHouse house) {
		this.house = house;
	}

	public String description() {
		return this.house.description();
	}
}	

2.3- Create 2 Concrete Decorators: Garden class and Pool class extending HouseDecorator:
Garden.java


package com.javasampleapproach.decorator.pattern;

public class Garden extends HouseDecorator {

	public Garden(IHouse house) {
		super(house);
	}

	@Override
	public String description() {
		return house.description() + upgrade();
	}

	private String upgrade() {
		return "[+garden]";
	}
}

Pool.java


package com.javasampleapproach.decorator.pattern;

public class Pool extends HouseDecorator {
	
	public Pool(IHouse house) {
		super(house);
	}

	@Override
	public String description() {
		return house.description() + upgrade();
	}

	private String upgrade() {
		return "[+pool]";
	}
}	

2.4- Create Client Test Class:


package com.javasampleapproach.decorator;

import com.javasampleapproach.decorator.pattern.SimpleHouse;
import com.javasampleapproach.decorator.pattern.Garden;
import com.javasampleapproach.decorator.pattern.IHouse;
import com.javasampleapproach.decorator.pattern.Pool;

public class MainApp {

	public static void main(String[] args) {
		
		IHouse house = new SimpleHouse();
		
		System.out.println("--- pay more ---");
		
		house = new Garden(house);
		System.out.println(house.description());
		
		System.out.println("--- pay more ---");
		
		house = new Pool(house);
		System.out.println(house.description());
		
		System.out.println("=== Another Person ===");
		
		IHouse newHouse = new SimpleHouse();
		System.out.println("--- pay more ---");
		
		newHouse = new Pool(newHouse);
		System.out.println(newHouse.description());
	}
}

2.5- Run the code, the console window shows:


buy a house
--- pay more ---
House[+garden]
--- pay more ---
House[+garden][+pool]
=== Another Person ===
buy a house
--- pay more ---
House[+pool]

III. SOURCE CODE

DecoratorPattern

One thought on “Decorator Pattern in Java”

  1. Link exchange is nothing else but it is only placing
    the other person’s website link on your page at suitable place and other person will also do similar in favor of you.

Leave a Reply

Your email address will not be published. Required fields are marked *