Reflection for Creating Decorators
in Java
Prof. David Bernstein
James Madison University
Computer Science Department
bernstdh@jmu.edu
Review
Reflection
:
An object looking at its internal details at runtime (a.k.a., introspection)
The Decorator Pattern
:
A decorator delegates much of its functionality to the object it decorates
An Inconvenient Aspect of the Decorator Pattern
In Some Cases:
The interface that is realized by the decorated and the decorator contains many methods
The Inconvenience:
Every method must be implemented in the decorator
Most methods delegate to the decorated but they all have different signatures
Overcoming this Inconvience
At Run Time:
Nothing can be done
At Compile Time:
It is possible to use reflection to create much of the code in the decorator
Using Reflection to Create the Source for a Decorator
javaexamples/decorator/DecoratorMaker.java
There's Always More to Learn