I Help You
Programming

Decorator Design Pattern

The Decorator pattern is a structural design pattern that adds new behaviour to an object dynamically by wrapping it in another object of the same interface, instead of creating many subclasses.

Decorators can be stacked, each adding one feature, such as adding milk and sugar to a coffee order, or buffering and compression to a stream. Java's I/O classes, such as BufferedReader wrapping a FileReader, use this pattern.

Example
interface Coffee { double cost(); }
class Plain implements Coffee { public double cost() { return 50; } }
class WithMilk implements Coffee {
	private final Coffee base;
	WithMilk(Coffee base) { this.base = base; }
	public double cost() { return base.cost() + 15; }
}

Coffee order = new WithMilk(new Plain()); // 65