dimanche 17 décembre 2017

Duplicated code dependency injection JavaEE

I've got interface 'FileFactory' with 'produce' method. This interface is implemented by two classes: 'SingleFileFactory' and 'MultipleFileFactory'. Both of these classes have the same code structure but returning different injected object. IntelliJ prints infirmation, that code in both classes is duplicated. I can't paste original code, so it's an example

Example:

public class SingleFileFactory {

@Inject
private FiirstSingleFile firstSingleFile;

@Inject
private SecondSingleFile secondSingleFile;

public File produce() {
if(something)
return firstSingleFile;
else
return secondSingleFile;
}
}

Class 'MultipleFileFactory' has the same logical but different injected objects.

I wanted to separate code into multiple classes, because in my opinion each can return specific object in the result.

Do you know how to avoid such as duplication - should I stretch it and pack into one class with a lot of injects and if expressions? Maybe you have better solution for this problem.

Aucun commentaire:

Enregistrer un commentaire