Java处理许多混凝土工厂

Java处理许多混凝土工厂,java,generics,intellij-idea,abstract-factory,Java,Generics,Intellij Idea,Abstract Factory,我想为许多(~40-50)类似实体概括一段重复的Java代码(在我的例子中,这段代码是用这些实体对文件进行索引) 我试图用泛型方法重构它,但结果是,我得到了一个泛型类的构造函数,这在Java中显然是禁止的。为了避免这种情况,我实现了抽象工厂模式,下面是我得到的结果 public <E extends CMObject, F extends IndexedFile<E>> F indexFile(CMFactory<E, F> factory) { F

我想为许多(~40-50)类似实体概括一段重复的Java代码(在我的例子中,这段代码是用这些实体对文件进行索引)

我试图用泛型方法重构它,但结果是,我得到了一个泛型类的构造函数,这在Java中显然是禁止的。为了避免这种情况,我实现了抽象工厂模式,下面是我得到的结果

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
public F indexFile(CMFactory){
F项目;
ByteBuffer[]浅黄色;
//…正在填充缓冲区。。。
items=factory.makeFile(buff);//因为我不能做items=new F(buff)
退货项目;
}
公共城市文件getCities(){
返回indexFile(new CityFactory());
}
公共文件getContinents(){
返回索引文件(新工厂());
}
//还有很多
这解决了创建泛型类实例的问题。然而,我现在面临的任务是为每一个实体创建一个混凝土工厂,这似乎是很多单调的工作,因为它们看起来都很像

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);
    }
}
公共抽象类CMFactory{
公共摘要F makeFile(ByteBuffer[]buff);
}
公共类CityFactory扩展了CMFactory{
@凌驾
公共城市文件生成文件(ByteBuffer[]buff){
返回新的城市文件(buff);
}
}
公共类工厂扩展了CMFactory{
@凌驾
公共文件makeFile(ByteBuffer[]buff){
返回新文件(buff);
}
}
问题是:有没有办法自动化创建这样的工厂?或者也许有另一种模式至少可以让这种创造变得不那么痛苦


我尝试使用IntelliJ IDEA的替换构造函数和工厂方法重构,但没有帮助我

由于您的
CMFactory
几乎是一个函数接口,您可以使用构造函数句柄,而不是为每个具体类实现
CMFactory

使
CMFactory
成为一个界面:

public interface CMFactory<E extends CMObject, F extends IndexedFile<E>> {
    public abstract F makeFile(ByteBuffer[] buff);
}
您甚至可以放弃
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);
}
public F indexFile(函数工厂){
ByteBuffer[]浅黄色;
//…正在填充缓冲区。。。
返回工厂。应用(buff);
}

由于您的
CMFactory
几乎是一个函数接口,您可以使用构造函数句柄,而不是为每个具体类实现
CMFactory

使
CMFactory
成为一个界面:

public interface CMFactory<E extends CMObject, F extends IndexedFile<E>> {
    public abstract F makeFile(ByteBuffer[] buff);
}
您甚至可以放弃
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);
}
public F indexFile(函数工厂){
ByteBuffer[]浅黄色;
//…正在填充缓冲区。。。
返回工厂。应用(buff);
}