Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/524824760891744257
edited tags
Link
deleted 134 characters in body
Source Link

I initially wanted to put the concrete classes as generic types in the implementation of TransformStrategy to avoid the cast, but that does not work, because the implementation cannot be inserted into the constructor of the transformer. This problem is shortly described with this example (using the classes from above):

abstract class AnswerConstraint { }
class LengthConstraint extends AnswerConstraint { }

interface TransformStrategy<T, S> { /* some method */ }
class LengthConstraintTransformStrategy implements TransformStrategy<LengthConstraint, LengthConstraintResource> { /* implementation */}

class Main {
    public static void main(String[] args) {

        List<TransformStrategy<AnswerConstraint, AnswerConstraintResource>> list = new ArrayList<>();

        // this fails although AnswerConstraint is the supertype of LengthConstraint and AnswerConstraintResource the supertype of LengthConstraintResource
        list.add(new LengthConstraintTransformStrategy());
    }
}

I initially wanted to put the concrete classes as generic types in the implementation of TransformStrategy to avoid the cast, but that does not work, because the implementation cannot be inserted into the constructor of the transformer. This problem is shortly described with this example:

abstract class AnswerConstraint { }
class LengthConstraint extends AnswerConstraint { }

interface TransformStrategy<T, S> { /* some method */ }
class LengthConstraintTransformStrategy implements TransformStrategy<LengthConstraint, LengthConstraintResource> { /* implementation */}

class Main {
    public static void main(String[] args) {

        List<TransformStrategy<AnswerConstraint, AnswerConstraintResource>> list = new ArrayList<>();

        // this fails although AnswerConstraint is the supertype of LengthConstraint and AnswerConstraintResource the supertype of LengthConstraintResource
        list.add(new LengthConstraintTransformStrategy());
    }
}

I initially wanted to put the concrete classes as generic types in the implementation of TransformStrategy to avoid the cast, but that does not work, because the implementation cannot be inserted into the constructor of the transformer. This problem is shortly described with this example (using the classes from above):

class LengthConstraintTransformStrategy implements TransformStrategy<LengthConstraint, LengthConstraintResource> { /* implementation */}

class Main {
    public static void main(String[] args) {

        List<TransformStrategy<AnswerConstraint, AnswerConstraintResource>> list = new ArrayList<>();

        // this fails although AnswerConstraint is the supertype of LengthConstraint and AnswerConstraintResource the supertype of LengthConstraintResource
        list.add(new LengthConstraintTransformStrategy());
    }
}
Post Reopened by rolfl
also replaced dummy class names in last example with real ones
Source Link

I initially wanted to put the concrete classes as generic types in the implementation of TransformStrategy to avoid the cast, but that does not work, because the implementation cannot be inserted into the constructor of the transformer. This problem is shortly described with this example:

abstract class BaseAnswerConstraint { }
class ConcreteLengthConstraint extends BaseAnswerConstraint { }

interface Something<T>TransformStrategy<T, S> { /* some method */ }
class ImplementationLengthConstraintTransformStrategy implements Something<Concrete>TransformStrategy<LengthConstraint, LengthConstraintResource> { /* implementation */}

class Main {
    public static void main(String[] args) {

        List<Something<Base>>List<TransformStrategy<AnswerConstraint, AnswerConstraintResource>> list = new ArrayList<>();

        // this fails although BaseAnswerConstraint is the supertype of ConcreteLengthConstraint and AnswerConstraintResource the supertype of LengthConstraintResource
        list.add(new ImplementationLengthConstraintTransformStrategy());
    }
}

I initially wanted to put the concrete classes as generic types in the implementation of TransformStrategy, but that does not work, because the implementation cannot be inserted into the constructor of the transformer. This problem is shortly described with this example:

abstract class Base { }
class Concrete extends Base { }

interface Something<T> { /* some method */ }
class Implementation implements Something<Concrete> { /* implementation */}

class Main {
    public static void main(String[] args) {

        List<Something<Base>> list = new ArrayList<>();

        // this fails although Base is the supertype of Concrete
        list.add(new Implementation());
    }
}

I initially wanted to put the concrete classes as generic types in the implementation of TransformStrategy to avoid the cast, but that does not work, because the implementation cannot be inserted into the constructor of the transformer. This problem is shortly described with this example:

abstract class AnswerConstraint { }
class LengthConstraint extends AnswerConstraint { }

interface TransformStrategy<T, S> { /* some method */ }
class LengthConstraintTransformStrategy implements TransformStrategy<LengthConstraint, LengthConstraintResource> { /* implementation */}

class Main {
    public static void main(String[] args) {

        List<TransformStrategy<AnswerConstraint, AnswerConstraintResource>> list = new ArrayList<>();

        // this fails although AnswerConstraint is the supertype of LengthConstraint and AnswerConstraintResource the supertype of LengthConstraintResource
        list.add(new LengthConstraintTransformStrategy());
    }
}
replaced dummy class names with real ones
Source Link
Loading
Post Closed as "Not suitable for this site" by rolfl
Source Link
Loading