// Application layer: defines the calculator interface and the calculation logic
interface Calculator {
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
int divide(int a, int b);
}
class CalculatorImpl implements Calculator {
// Implement the calculation logic
}
// Port: defines the input port for the calculation request
interface CalculationRequestPort {
void requestCalculation(int a, int b, String operation);
}
// Port: defines the output port for the calculation response
interface CalculationResponsePort {
void respondCalculation(int result);
}
// Adapter: defines the driver adapter for the console input
class ConsoleInputAdapter implements CalculationRequestPort {
private Calculator calculator; // Inject the calculator dependency
private CalculationResponsePort calculationResponsePort; // Inject the output port dependency
public ConsoleInputAdapter(Calculator calculator, CalculationResponsePort calculationResponsePort) {
this.calculator = calculator;
this.calculationResponsePort = calculationResponsePort;
}
public void readInput() {
// Read the input from the console and request the calculation
}
public void requestCalculation(int a, int b, String operation) {
// Perform the calculation and respond with the result
}
}
// Adapter: defines the driven adapter for the console output
class ConsoleOutputAdapter implements CalculationResponsePort {
public void respondCalculation(int result) {
// Write the result to the console
}
}