The Decorator Design Pattern

Share

The official definition of the Decorator pattern from the GoF book (Design Patterns: Elements of Reusable Object-Oriented Software, 1995, Pearson Education, Inc. Publishing as Pearson Addison Wesley) says you can, “Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.”

This pattern, also known as Wrapper, allows you to extend the core object by wrapping it in various decorator wrappers, avoiding modification of the core code. Each successive wrapper called the getDescription method of the object it wrapped and added something to it.

class ChristmasTree {
	public ChristmasTree() {
	}
 
	public String getDescription() {
		return "Christmas tree";
	}
}
 
abstract class ChristmasTreeDecorator extends ChristmasTree {
	public abstract String getDescription();
}
 
class ChristmasLight extends ChristmasTreeDecorator {
	private ChristmasTree christmasTree;
 
	public ChristmasLight(ChristmasTree ct) {
		christmasTree = ct;
	}
 
	public String getDescription() {
		return christmasTree.getDescription() + " with lights";
	}
}
 
class ChristmasOrnament extends ChristmasTreeDecorator {
	private ChristmasTree christmasTree;
 
	public ChristmasOrnament(ChristmasTree ct) {
		christmasTree = ct;
	}
 
	public String getDescription() {
		return christmasTree.getDescription() + " and ornaments.";
	}
}
 
public class DecoratorTest {
	public static void main(String args[]) {
		ChristmasTree christmasTree = new ChristmasTree();
 
		christmasTree = new ChristmasLight(christmasTree);
		christmasTree = new ChristmasOrnament(christmasTree);
 
		System.out.println("This is a " + christmasTree.getDescription());
	}
}

The decorator design pattern is also used in the java.io classes. These classes are designed to be “chained” or “wrapped“. You can “wrap” a BufferedReader around a FileReader or a BufferedWriter around a FileWriter, to get access to higher-level (more convenient) methods.

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close