My solution required considerably more lines of code than the code at ICT Cool. For a problem as simple as this, using the Decorator Pattern is probably overkill. However, the advantage of this pattern is that multiple decorators can be stacked up at run time in any order that we choose. Also, if we wanted to add more operations later, we simply add a new class, which is cleanly separate from the other operations.
I’ve written the program using NetBeans, which creates quite a few files other than the source files, which are in
src. To run the program,
cd to the
dist directory and type
java -jar StringDecoratorDemo.jar.