Question

I have the following structure

public interface I{...}

public class A implements I{
  ....

  public static class Inside{
    ...
    //data from here are used in A. Therefore tight coupling.
  }
}

public class B implements I{
  ....

  public static class Inside{
    ...
    //data from here are used in B.  Therefore tight coupling.
  }
}

Note that the implementation of A.Inside is somewhat different from B.Inside. Then I have another class that accesses Inside as

public class AW extends X{
  ...
}

public class BW extends X{
  ...
}

public class X{
  ...
  public Obj doSomeWork(Inside inside){
    mInside = inside;
    ....//some work
  }
}


//UsageB is similar in it's implementation of the B stack
public class UsageA{
  A.AW inst = new AW();
  Inside in = A.Inside();
  inst.doSomeWork(in);
}

question: What should be the type of mInside? I don't want to have two mInside variables.

AW is tailored for A and BW is tailored for B

Was it helpful?

Solution

The problem is that your two Inside types are currently unrelated - there's no way for X.doSomeWork() to do refer to "it's this type or that type" when they're entirely different types. The fact that they're both nested classes called Inside doesn't make any difference.

If you extract the common behaviour (that X wants to call) into an interface and make both Inside classes implement that interface, then you can use that commonality. Basically do exactly what you'd do if they weren't nested classes and didn't have the same name, because X doesn't care about either of those aspects.

OTHER TIPS

If I understand you, It depends on your code. I mean, if your code in X.java is:

import tests.A.Inside;

public class X {
    public static void doSomeWork (Inside inside ){
    }
}

Then Inside is the class implemented in A.java, however you can do it a bit more explicit, using:

import tests.A;

public class X {
    public static void doSomeWork (A.Inside inside ){
    }
}

So the method parameters is self-explanatory.

I doubt that you need this solution, but:

interface I{
IInside getInside();
}
interface IInside{
    Object doWork();
}

class X {
    private I state;
}

simpler, where A\B can delegate actual execution to inner class:

interface I{

    Object doWork();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top