I have some theoretical knowledge related to Design Patterns and now I have some issues to make these info and another ones in action.
Here are the 2 methods in a ServiceImpl
class:
@Override
public MultipartFile exportMethodA() throws IOException {
// repeated lines
workbook = new XSSFWorkbook();
sheet = workbook.createSheet(...);
//
// other lines special to this method
// repeated lines ======================================================
outputFile = File.createTempFile();
try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
workbook.write(outputStream);
} catch (IOException e) {
LoggingUtils.error("Writing failed ", e);
}
final FileInputStream input = new FileInputStream(outputFile);
final String fileName = TextBundleUtil.read(...);
return new MockMultipartFile(fileName,
fileName, CONTENT_TYPE, IOUtils.toByteArray(input));
//
}
@Override
public MultipartFile exportMethodB() throws IOException {
// repeated lines
workbook = new XSSFWorkbook();
sheet = workbook.createSheet(...);
//
// other lines special to this method
// repeated lines ======================================================
outputFile = File.createTempFile(...);
try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
workbook.write(outputStream);
} catch (IOException e) {
LoggingUtils.error("Writing failed ", e);
}
final FileInputStream input = new FileInputStream(outputFile);
final String fileName = TextBundleUtil.read(...);
return new MockMultipartFile(fileName,
fileName, CONTENT_TYPE, IOUtils.toByteArray(input));
//
}
As it is shown, there are repeated parts in all of these methods. So, the first option I think, to create an interface etc, but I have really no idea how to split and refactor them. So, could you pls. inform me on how to refactor these two methods and use a single code for the repeated parts?
Aucun commentaire:
Enregistrer un commentaire