vendredi 25 octobre 2019

Tail recursion happening through the Class Hierarchy

Hey you people out there,

I'm asking myself the following question. This should be done in Java, but since I don't know how to do it anyway just a concept would help too: I have the following code:

public abstract class A {

   protected enum AType implements AInterface {
     A_VALUE
   }

   public AInterface[] possibleRequests() {
      AInterface types = AType.values();
      return ArrayUtils.concat(types, possibleFurtherRequests());
   }

   public abstract AInterface[] possibleFurtherRequests();

}

public class B extends A {

   protected enum BType implements BInterface {
     B_VALUE
   }

   @Override
   protected AInterface[] possibleFurtherRequests() {
      //Here is my problem.
      return BType.values();
   }
}

public interface AInterface {
}

public interface BInterface extends AInterface {
}

What I want to do is have these possibleRequest-methods in indefinite depth. They should only be accessible via A, I do not and should not know which class the object of type A belongs to.

What I mean by indefinite depth is, that say this concept was extended with C extends B. I now want to access all values from A, B and C. How do I enforce, that whenever a new subclass is added the programmer is forced to define these AInterface-enumerations and how do I force him to implement a method that is then called recursively up the class Hierarchy. I don't need help defining an abstract method, or overriding one. What I want to do is NOT override the existing one and NOT add an abstract method to each inheriting class that gets called.

I honestly don't know how to ask this question but I hope someone out there understands what I mean. If not leave a comment.





Aucun commentaire:

Enregistrer un commentaire