Four rules of simple design - A design is "simple" if it follows these rules:
void scaleToOneDimension(...){ if (Math.abs(desiredDimension - imageDimension) < errorThreshold) return; float scalingFactor = ...; ... RenderedOp newImage = ImageUtilities.getScaledImage(image, scalingFactor, scalingFactor); image.dispose(); System.gc(); image = newImage; } void rotate(int degrees) { RenderedOp newImage = ImageUtilities.getRotatedImage(image, degrees); image.dispose(); System.gc(); image = newImage(); }Could be changed to
void scaleToOneDimension(...) { if (Math.abs(desiredDimension - imageDimension) < errorThreshold) return; float scalingFactor = ...; ... replaceImage(ImageUtilities.getScaledImage(image, scalingFactor, scalingFactor)); } void rotate(int degrees) { replaceImage(ImageUtilities.getRotatedImage(image, degrees)); } void replaceImage(RenderedOp newImage) { image.dispose(); System.gc(); image = newImage(); }Additionally, the fact that we need replaceImage shows we violate the single responsibility principle; so the replaceImage method might be extracted into its own class.
ex:
class VacationPolicy { public void accrueVacation() { calculateBaseVacationHours(); alterForLegalMinimums(); applyToPayroll(); } void calculateBaseVacationHours() {...} abstract void alterForLegalMinimums(); // to be implemented void applyToPayroll() {...} } class USVacationPolicy extends VacationPolicy { @Override void alterForLegalMinimums() { ... } } class EUVacationPolicy extends VacationPolicy { @Override void alterForLegalMinimums() { ... } }
bj 2019-09-22