Hi All,
As per the wikipedia, the decorator
pattern is defined as
"The decorator pattern can be used to extend (decorate) the functionality of a certain object statically, or in some cases at run-time, independently of other instances of the same class, provided some groundwork is done at design time. This is achieved by designing a new decorator class that wraps the original class. This wrapping could be achieved by the following sequence of steps:
1.Subclass the original "Component" class into a "Decorator" class (see UML diagram);
2.In the Decorator class, add a Component pointer as a field;
3.Pass a Component to the Decorator constructor to initialize the Component pointer;
4.In the Decorator class, redirect all "Component" methods to the "Component" pointer; and
5.In the ConcreteDecorator class, override any Component method(s) whose behavior needs to be modified.
"
It means the Decorator should actually extend the component that needs to be decorated.
My question is, is this requirement always rigid to be called as a Decorator pattern.
The reason I'm asking this question is, I have a bean class with lot of setters and getters and I do not have access to change the code. However, I wanted to print the bean's data in a particular format.
My approach is as follows
So basically this bean does nothing (no business functionality exists). We can assume that this is just a container of some data and also has some basic toString() method which prints the data. However, I want to have the data printed in a very particular way based on some business rules.
Now I'm thinking of implementing something like this
Can the above approach be considered as Decorator pattern, as this also supplying runtime behavior to Bean class ? If this is not a decorator, is this any other pattern ?
Thanks