I recently learned about the decorator-pattern to dynamically extend existing behaviour. So I have this code:
IMyInterface b = new A();
if(someCondition)
b = new B(b);
if(secondCondition)
b = new C(b);
and so on, where all classes A
, B
and C
implement IMyInterface
and B
and C
are the decorators. So the final request for a method might be handled by up to three instances in the above example.
However I now want to inject a decorator into my code, because I have some plugin-mechanism which allows me to seperate product-code from project-specific code. This is why I don´t even know anything of B
and C
. So I want a way that returns a decorated instance of my interface, in a way that I can chain multiple plugins and thus multiple decorators:
IMyInterface b = new A();
/* collect the decorators from all plugins and chain them to get the final instance */
Has anyone a good idea on how to create the instances?