In software engineering, keeping all logic in a single class creates "God Objects" that are hard to maintain. This project demonstrates the Refactoring of a monolithic Calculator into a modular architecture based on the Single Responsibility Principle (SRP). We have decoupled the system into three distinct roles:
- CalculatorData: An immutable record for holding the state.
- CalculationService: A dedicated class for mathematical operations.
- ResultPrinter: A specialized component for UI/Console output.
- Separation of Concerns: Each class has exactly one reason to change.
- Immutability: Used a Java Record for data transfer to ensure results are not accidentally modified.
- Encapsulation: Hidden calculation details within the service layer.
- Clean Architecture: Followed the package-by-feature structure for better organization.
- Java 16+ (Records, OOP Design Patterns)
- CalculatorData: Data carrier.
- CalculationService: Logic provider.
- ResultPrinter: Output handler.
The final result of calculation is: 55
Project Structure:
JavaBasics_Task_407_V0.2/
├── src/
│ └── com/yurii/pavlenko/
│ ├── app/
│ │ └── CalculatorApp.java
│ └── math/
│ ├── models/
│ │ └── CalculatorData.java
│ ├── services/
│ │ └── CalculationService.java
│ └── ui/
│ └── ResultPrinter.java
├── LICENSE
├── TASK.md
├── THEORY.md
└── README.md
Code
package com.yurii.pavlenko.app;
import com.yurii.pavlenko.math.models.CalculatorData;
import com.yurii.pavlenko.math.services.CalculationService;
import com.yurii.pavlenko.math.ui.ResultPrinter;
public class CalculatorApp {
public static void main(String[] args) {
CalculationService service = new CalculationService();
ResultPrinter printer = new ResultPrinter();
CalculatorData data = service.calculate(10, 5);
printer.print(data);
}
}package com.yurii.pavlenko.math.models;
public record CalculatorData(int result) {
}package com.yurii.pavlenko.math.services;
import com.yurii.pavlenko.math.models.CalculatorData;
public class CalculationService {
public CalculatorData calculate(int a, int b) {
// Calculation: a * b + (a - b)
int calculatedValue = a * b + (a - b);
return new CalculatorData(calculatedValue);
}
}package com.yurii.pavlenko.math.ui;
import com.yurii.pavlenko.math.models.CalculatorData;
public class ResultPrinter {
public void print(CalculatorData data) {
System.out.println("The final result of calculation is: " + data.result());
}
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT