I am trying to implement decorator pattern, my problem is I have different generated sources which will be passed to decorated masker, I do not have common parent which exposes the methods I need for doing the various masking in each masker so to solve this issue I thought about passing the type to the masker, and each masker will know the setter/getter he will be using, now what I want to do is pass the objects which have a common parent (without methods) and cast these objects using the passed type, now my problem is calling the getter, for example for NumbrMasker I know the getter/setter will be getNumber/setNumber but I am not sure how to call the methods for the passed type.
class NumberDecoratedMasker extends Masker {
private Class classType;
public DecoratedMasker (Masker masker, Class classType) {
this.classType = classType;
}
//for Number masker
public void mask(ParentModel parent) {
//cast parent using classtype
//call getNumber/setNumber for masking number
if(masker != null)
makser.mask(parent); // this is the decorated call for the other masker in the decorator structure
}
}
class ParentModel {
//has nothing
}
class Elementa extends ParentModel {
//has setters and getters for number
}
Now I initialize the Decorated with the classType of the object I want to mask
Masker masker = new NumberDecoratedMakser(new SecondNumberDecoratedMasker(ElementA.class), ElementA.class);
ParentModel model = new ElementA();
masker.mask(moel);
I know reflection will be a solution in this case but I cannot go with it, is there a way to do this with java8?