lundi 13 juillet 2020

How to invoke a method of class from interface return type object using java reflection

i am new to reflection and learning a problem statement of my work project.

interface performable{
 public void perform();
}

class searchOne implements performable{

    public void perform(){
        Sysout("from searchOne)
    }

    public static Performable from(){
        return new searchOne();
    }

}
class searchTwo implements performable{
    
    public void perform(){
        Sysout("from searchtwo)
    }

    public static Performable from(){
        return new searchOne();
    }

}
class mainClass{
    public static void main(String args[]){
        new mainClass().triesTo(searchOne.from,searchTwo.from)
    }
    
    triesTo(Performable...perform){
        //parameters inside tries two return two performable object
        for(Performable per:perform){
        }
    }

}

i understand that triesTo returns performable var arg type object. My question here is with the type of performable object how to iterate and invoke appropriate implementation class method using java reflection. To also learn further under which concept or design pattern these type of scenarios come under. Thanks buddies for your suggestion in advance





Aucun commentaire:

Enregistrer un commentaire