mercredi 29 mars 2017

Java 8: Automatically composite default methods of multiple interfaces

I have class implements multiple interfaces which have a same default default method. I am wondering how can I composite the default method from all the interfaces. For example:

interface IA { 
    default void process() { 
        // do something 
    }
}

interface IB { 
    default void process() { 
        // do something 
    }
}

interface IC { 
    default void process() { 
        // do something 
    }
}

// other similar interfaces
....    

class MyClass implements IA, IB, IC, ... {
    public void process() {
       // question: how to avoid iterate all the interfaces? 
       IA.super.process();       
       IB.super.process();
       IC.super.process();
       ...
    }
}

class AnotherClass implements IA, ID, IF, IH, ... {
    public void process() {
        IA.super.process();
        ID.super.process();
        IF.super.process();
        IH.super.process();
        ...
    }
}

I have to call IA.super, IB.super, IC.super explicitly. If the interface list is long it's painfully to write all of them. Also I may have different classes to implement different combination of interfaces. Is there other syntax sugar/design pattern/library that allows me to do it automatically?

Aucun commentaire:

Enregistrer un commentaire