Java Generics Class可以使用工厂生成的产品

时间:2016-04-20 08:20:19

标签: java generics

对于有线标题感到抱歉,但归结为......

问题可以这样描述:

我有一个Worker<P>类,可配置为生成“某些”产品。这个Worker必须能够使用自己的产品(并且只有自己的产品,这是练习的全部内容):

interface Worker<P> {
    public P produce();
    public void consume( P p );
}

(无法更改)

到目前为止这么容易,但现在可以配置产品本身:

interface ConfiguredProduct<T> {
    public T value();
}

(无法更改)

现在工作人员是由工厂生成的(现实生活本身是使用配置文件创建的)

static class Hatchery {

    public <X extends CharSequence, Y extends ConfiguredProduct<X>> Worker<Y> breed() {

        return new StringWorker(); // Type mismatch: cannot convert from Test.StringWorker to Test.Worker<Y>
    }
}

(可能会更改)

我喜欢以这样的方式使用它:

public static <perhapse some wired arguments> void main( String [] args ) throws Exception {

    Hatchery hatchery = new Hatchery(); // Cannot be parametrized here

    Worker<T> worker = hatchery.breed();

    ConfiguredProduct<wired arguments> product = worker.produce();

    worker.consume( product );
}

工人的例子可能是:

static class StringWorker implements Worker<ConfiguredProduct<String>> {

    @Override
    public ConfiguredProduct<String> produce() {
        return () -> "Hello";
    }

    @Override
    public void consume( ConfiguredProduct<String> p ) {
        System.out.println( "Hello " + p.value() );
    }
}

static class StringBufferWorker implements Worker<ConfiguredProduct<StringBuffer>> {

    @Override
    public ConfiguredProduct<StringBuffer> produce() {
        return () -> new StringBuffer( "Hello" );
    }

    @Override
    public void consume( ConfiguredProduct<StringBuffer> p ) {
        System.out.println( "Hello " + p.value().toString() );
    }
}

这是否有可能在没有疯狂施展的情况下?我在任何我能想到的地方都尝试了泛型参数的每一种组合,但它确实在某一点产生了错误。

编辑:添加了注释,哪些部分无法更改。添加了Hatchery的使用和备注。

2 个答案:

答案 0 :(得分:2)

如果你有不同的工厂可以返回不同的工人,那么你会有类似的东西:

static interface Hatchery {
    public <X extends CharSequence, Y extends ConfiguredProduct<X>> Worker<Y> breed();
}

static class WorkerHatchery implements Hatchery{
    public   Worker<ConfiguredProduct<String>> breed(){
        return new StringWorker();
    }
}

然后这段代码没有任何问题

public static void main(String[] args) throws Exception {
    Hatchery hatchery = new WorkerHatchery();
    Worker<ConfiguredProduct<String>> worker = hatchery.breed();
    ConfiguredProduct<String> product = worker.produce();
    worker.consume(product);
}

修改 如果您不想发出任何警告,还有另一种选择,您可以在课堂上移动泛型值,您将拥有:

 interface Hatchery<X extends CharSequence, Y extends ConfiguredProduct<X>>{ 
       Worker<Y> breed();
    }

class WorkerHatchery implements Hatchery<String,ConfiguredProduct<String>> { 
        public Worker<ConfiguredProduct<String>> breed() {  
        return new  StringWorker();     
    }   
}

答案 1 :(得分:0)

这个怎么样:

public class Test {

    private interface Product<X> {
        public X getValue();
    }

    private static class StringProduct implements Product<String> {

        @Override
        public String getValue() {
            return "Hello World!";
        }
    }

    private interface Worker<X> {
        public Product<X> produce();

        public void consume(Product<? extends X> p);
    }

    private static class StringWorker implements Worker<String> {

        @Override
        public StringProduct produce() {
            return new StringProduct();
        }

        @Override
        public void consume(Product<? extends String> p) {
            System.out.println(p.getValue());
        }
    }

    private interface WorkerFactory<T extends Worker<?>> {
        public T breed();
    }

    private static class StringWorkerFactory implements WorkerFactory<StringWorker> {

        @Override
        public StringWorker breed() {
            return new StringWorker();
        }
    }

    public static void main(String[] args) {
        WorkerFactory<StringWorker> factory = new StringWorkerFactory();
        Worker<String> worker = factory.breed();

        Product<String> product = worker.produce();

        worker.consume(product);
    }
}