mardi 24 février 2015

Invoke a function if it has a defined Decorator Pattern

I don't know if I'm using the wrong pattern or what. I'm have a standard class and 3 decorators:



MyClass, Dec1, Dec2, Dec3.


everyone implements MyClassInterface { getDescription(), addDescription(String t) }. But Dec2 has also the function { specialFunction() }.


I create an instance:



MyClassInterface instance = new Dec1(new Dec2(new Dec3(new MyClass())));


then I would call the specialFunction, but I can't! If I would have done this with Dec1 that extends Dec2, that extends Dec3, that extends MyClass, would have been possible.


It would be nice having a function like



decoratorInstance = instance.hasDecorator(Dec2)
decoratorInstance.specialFunction()


But I don't know if it is possible without making a huge pattern mistake.


Aucun commentaire:

Enregistrer un commentaire