Master java skills

Decorator Design Pattern

The Decorator Design Pattern is a structural design pattern. It is used to dynamically add new behaviour or functionality to objects without changing their structure or making its subclasses. The pattern is advantageous when you want to enhance the functionality of an object in a flexible and reusable way.

In this example, we will have a Room interface. There would be a class of it as BasicRoom. Then there would decorated classes: DecoratedRoom and FurnishedRoom. The decorator class name is RoomDecorator

In this example, BasicRoom is the class whose objects will be decorated. Whereas DecoratedRoom and FurnishedRoom are the decorator classes which will give the decorated objects.

Interface Room

package com.sks.decorator;

public interface Room {

	void getDescription();
	
}

BasicRoom class

package com.sks.decorator;

public class BasicRoom implements Room {

	protected boolean isPainted = true;
	protected boolean isWalpaperApplied;
	protected boolean isLightingDone;
	protected boolean isFurnished;
	
	@Override
	public void getDescription() {
		System.out.println(this);
	}
	
	@Override
	public String toString() {
		return "Room [isPainted=" + isPainted + ", isWalpaperApplied=" + isWalpaperApplied + ", isLightingDone="
				+ isLightingDone + ", isFurnished=" + isFurnished + "]";
	}

	//constructors, getters, setters
}

DecoratedRoom

package com.sks.decorator;

public class DecoratedRoom extends RoomDecorator {
	
	public DecoratedRoom(Room room) {
		super(room);
	}

	@Override
	public void getDescription() {
		if(this.room instanceof BasicRoom) {
			BasicRoom room = (BasicRoom)this.room;
			room.setLightingDone(true);
			room.setWalpaperApplied(true);
		}
		System.out.println(this.room);
	}

	@Override
	public String toString() {
		return "DecoratedRoom [room=" + room + "]";
	}
	
}

FurnishedRoom

package com.sks.decorator;

public class FurnishedRoom extends RoomDecorator {
	
	public FurnishedRoom(Room room) {
		super(room);
	}

	@Override
	public void getDescription() {
		if(this.room instanceof BasicRoom) {
			BasicRoom room = (BasicRoom)this.room;
			room.setLightingDone(true);
			room.setWalpaperApplied(true);
			room.setFurnished(true);
		}
		System.out.println(this.room);
	}

	@Override
	public String toString() {
		return "FurnishedRoom [room=" + room + "]";
	}
}

Decorator class

package com.sks.decorator;

public class RoomDecorator implements Room {
	
	protected Room room;
	
	public RoomDecorator(Room room) {
		super();
		this.room = room;
	}

	@Override
	public void getDescription() {
		this.room.getDescription();
	}

	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}

}

DecoratorTest class

package com.sks.decorator;

public class DecoratorTest {

	public static void main(String[] args) {

		Room basicRoom = new BasicRoom();

		basicRoom.getDescription();

		Room decoratedRoom = new DecoratedRoom(basicRoom);

		decoratedRoom.getDescription();

		Room furnishedRoom = new FurnishedRoom(basicRoom);

		furnishedRoom.getDescription();

	}
}

Output: