简体   繁体   English

Java处理许多具体工厂

[英]Java dealing with a lot of concrete factories

I want to generalize a repetitive piece of Java code for a lot of (~40-50) similar entities (in my case, this piece is indexing of files with these entities). 我想为很多(〜40-50)个相似的实体概括一个重复的Java代码(在我的情况下,这是用这些实体对文件进行索引)。

I tried to refactor it with generic method, but, as a result, I get a constructor of generic class that is apparently prohibited in Java. 我试图使用泛型方法对其进行重构,但是结果,我得到了Java中显然禁止的泛型类的构造函数。 To avoid this, I implemented abstract factory pattern and here's what I've get. 为避免这种情况,我实现了抽象工厂模式,这就是我所得到的。

public <E extends CMObject, F extends IndexedFile<E>> F indexFile(CMFactory<E, F> factory) {
    F items;
    ByteBuffer[] buffs;

    // ...filling buffers...

    items = factory.makeFile(buffs); // as I cannot do items = new F(buffs)

    return items;
}

public CityFile getCities() {
    return indexFile(new CityFactory());
}

public ContinentFile getContinents() {
    return indexFile(new ContinentFactory());
}
// a lot of more

This solves an issue of creating an instance of generic class. 这解决了创建通用类实例的问题。 However, I now face a task of creating a concrete factory for each single entity that seems to be a lot of monotonous work as they all look like each other. 但是,我现在面临着为每个实体创建一个具体工厂的任务,这似乎是很多单调的工作,因为它们看上去彼此相似。

public abstract class CMFactory<E extends CMObject, F extends IndexedFile<E>> {
    public abstract F makeFile(ByteBuffer[] buff);
}

public class CityFactory extends CMFactory<City, CityFile> {
    @Override
    public CityFile makeFile(ByteBuffer[] buff) {
        return new CityFile(buff);
    }
}
public class ContinentFactory extends CMFactory<Continent, ContinentFile> {
    @Override
    public ContinentFile makeFile(ByteBuffer[] buffs) {
        return new ContinentFile(buffs);
    }
}

The question is: is there any way to automatize creation of such factories? 问题是:是否有任何方法可以自动创建此类工厂? Or maybe is there another pattern that can at least make such creation less painful? 也许还有另一种模式至少可以减轻这种创作的痛苦?

I tried to use IntelliJ IDEA's Replace Constructor with Factory Method refactor, but it didn't help me. 我试图将IntelliJ IDEA的Replace Constructor与Factory Method重构一起使用,但是它没有帮助。

Since your CMFactory is almost a functional interface you can use constructor handles instead of implementing CMFactory for each concrete class: 由于CMFactory几乎是一个功能接口,因此可以使用构造函数句柄,而不是为每个具体类实现CMFactory

Make CMFactory an interface: 使CMFactory接口:

public interface CMFactory<E extends CMObject, F extends IndexedFile<E>> {
    public abstract F makeFile(ByteBuffer[] buff);
}

and then write 然后写

public CityFile getCities() {
    return indexFile(CityFile::new);
}

You can even discard CMFactory and use java.util.Function : 您甚至可以丢弃CMFactory并使用java.util.Function

public <E extends CMObject, F extends IndexedFile<E>> F indexFile(Function<ByteBuffer[],F> factory) {
    ByteBuffer[] buffs;
    // ...filling buffers...
    return factory.apply(buffs);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM