1

In Java I have an abstract base class, let's say WrapX, which contains a property of a type, say X (think Decorator DP). This class presents a method to call a specific method on the encapsulated X:

public abstract class WrapX{

  protected X instance;

  public WrapX(X x){
     this.instance = x;
  }

  public void foo(){
       instance.foo();
  }

}

public class X {

   public void foo(){
       System.out.println("foo");
   }
}

There is then a class called Y that extends from X and provides an additional method:

public class Y extends X {

  public void bar(){
     System.out.println("bar");
  }
}

Then naturally I have created WrapY that can be used as a decorated type over the type Y:

public class WrapY extends WrapX{

  ...

  public void bar(){
    instance.bar();
  }

}

So herein lies the issue. WrapY has inherited the instance property of type X from its parent WrapX. As far as Eclipse is concerned, instance is of type X and so will complain that it contains no method .bar().

Fair enough of course, but how then in this subclass can we implicitly cast the instance to an instance of Y (a valid subclass of the initial type X)... WITHOUT the need for explicit cast ascriptions littering the code, or variable shadowing?

If I just had this in the constructor:

public WrapY(Y y){
 this.instance = y;
}

Eclipse still complains that .bar() is not a method of type X because I guess it cannot infer for certain that WrapY(Y y) will be used prior to construct the WrapY instance:

public void bar(){
        instance.bar(); // ERROR
      }

Here is the current approach I have, littered with casts:

public WrapY(Y y){
 (Y)instance = y;
}

public void bar(){
  ((Y)instance).bar();
}

I haven't come across this particular type of architectural problem in my experience before, file it under 'Decorator-Based-Inheritance-Type-Casting'(!)... Please enlighten me as to how I can model this in a better way.

Another issue is that, if in future someone extends WrapY, the type of instance their class inherits will be the natural (uncasted) type of X, when they may reasonably assume it should be of type Y.

Thanks

4

1 回答 1

2

您可以使您的 Wrap 类通用,例如:

public abstract class Wrap<T extends X>{

    protected T instance;

    public Wrap(T x){
        this.instance = x;
    }

    public void foo(){
       instance.foo();
    }
}



public final class WrapY extends Wrap<Y> {

    public WrapY(Y y) {
        super(y);
    }

    public void bar(){
        instance.bar();
    }
}

那么对于 的实例WrapYinstance将是一个Y


更新:如果您也想从 WrapY 继承(并解决最合适的包装类型的最后一个问题),请执行以下操作:

public class WrapY<U extends Y> extends Wrap<U> {

    public WrapY(U y) {
        super(y);
    }

    public void bar(){
        instance.bar();
    }
}
于 2012-10-06T10:12:45.793 回答