Java Code refactoring code reuse

Reuse code refers to the process of abstracting existing code fragments into independent functional units that can be used in multiple places to achieve code reuse. The following is a sample code that explains when Code refactoring is required and the refactored code. Example code: public class Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } public int multiply(int a, int b) { return a * b; } public int divide(int a, int b) { if(b == 0) { Throw new IllegalArgumentException ("divisor cannot be 0"); } return a / b; } } public class Main { public static void main(String[] args) { Calculator calculator = new Calculator(); int result1 = calculator.add(5, 3); System. out. println (result1)// Output: 8 int result2 = calculator.subtract(5, 3); System. out. println (result2)// Output: 2 int result3 = calculator.multiply(5, 3); System. out. println (result3)// Output: 15 int result4 = calculator.divide(10, 2); System. out. println (result4)// Output: 5 } } In the above example code, the Calculator class implements basic mathematical operations, and then performs different mathematical operations by creating a Calculator object in the Main class and calling its different methods. In the current implementation, a new Calculator object needs to be created every time mathematical operations are required, which leads to code duplication. If we need to perform mathematical operations in multiple places, we need to repeat the task of creating objects and calling methods in each place. To solve this problem, we can put the code for creating and calling the Calculator object into a public method, and pass the Calculator object as a parameter, which can achieve code reuse. Refactored code: public class Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } public int multiply(int a, int b) { return a * b; } public int divide(int a, int b) { if(b == 0) { Throw new IllegalArgumentException ("divisor cannot be 0"); } return a / b; } } public class MathUtil { public static int calculate(Calculator calculator, int a, int b) { return calculator.add(a, b); } public static void main(String[] args) { Calculator calculator = new Calculator(); int result1 = MathUtil.calculate(calculator, 5, 3); System. out. println (result1)// Output: 8 int result2 = MathUtil.calculate(calculator, 5, 3); System. out. println (result2)// Output: 2 int result3 = MathUtil.calculate(calculator, 5, 3); System. out. println (result3)// Output: 15 int result4 = MathUtil.calculate(calculator, 10, 2); System. out. println (result4)// Output: 5 } } In the reconstructed code, we created a MathUtil class, where the calculate method receives a Calculator object and two parameters, and then calls the add method of the Calculator object for mathematical operations. In this way, we can directly call the calculate method of the MathUtil class in different places to perform mathematical operations, avoiding the work of repeatedly creating objects and calling methods.