How to impliment the TableDecorator in my project.Can anyone provide me the steps or code for that.
-
1Refer to BalusC's excellent [GoF Design Patterns answer](http://stackoverflow.com/questions/1673841/examples-of-gof-design-patterns/2707195#2707195) for usage of the Decorator pattern in the Java Library – Sean Patrick Floyd Apr 06 '11 at 09:22
-
1Changing topic and content **after** receiving answers is not very nice - note, that my answer is valid for the *original* version of this question... – Andreas Dolk Apr 06 '11 at 09:25
-
sorry for the inconvenience,previously topic is mistakenly written by – Anshul Apr 06 '11 at 09:30
3 Answers
"Decorator" is not a class but a design pattern. You'll find a lot of implementations of this pattern in the java.io
package (look at the streams, for example)
A prominent example, that explains a lot, is the BufferedInputStream
. This class decorates any InputStream
by adding some buffering.
Simple example:
public interface GreetProvider { public String greet(); }
public class HelloProvider implements GreetProvider {
public String greet() {
return "Hello";
}
}
public class ByeProvider implements GreetProvider {
public String greet() {
return "Good Bye";
}
}
public class SmilyDecorator implements GreetProvider {
private GreetProvider provider;
public SmilyDecorator(GreetProvider provider) {this.provider = provider;}
public String greet() {
return provider.greet() + " :-)";
}
}
// somwhere in some method
GreetProvider hello = new HelloProvider();
GreetProvider bye = new ByeProvider();
GreetProvider helloAndSmiley = new SmileyDecorator(hello);
GreetProvider helloAndTwoSmileys = new SmileyDecorator(helloAndSmiley);
System.out.printf("%s%n%s%n%s%n%s%n",
hello.greet(), bye.greet(),
helloAndSmiley.greet(), helloAndTwoSmileys.greet());

- 113,398
- 19
- 180
- 268
-
Changing topic and content after receiving answers is not very nice - note, that my answer is valid for the original version of this question... – Andreas Dolk Apr 06 '11 at 09:29
Following classes uses decorator pattern:
All subclasses of
java.io.InputStream, OutputStream, Reader and Writer
have a constructor taking an instance of same type.Almost all implementations of
java.util.List
, Set and Map have a constructor taking an instance of same type.java.util.Collections
, thecheckedXXX(), synchronizedXXX() and unmodifiableXXX()
methods.javax.servlet.http.HttpServletRequestWrapper and HttpServletResponseWrapper

- 13,436
- 25
- 87
- 129
OK, apparently we are talking about this TableDecorator
class.
Although it is abstract, it has no abstract methods, so I guess the abstract
modifier is just to stop you from instantiating the super class, use one of the subclasses instead:
Direct Known Subclasses:
MultilevelTotalTableDecorator
,TotalTableDecorator
(I don't think you are meant to implement your own TableDecorator
class)

- 292,901
- 67
- 465
- 588