Junior — Middle
Can you explain what a low-coupling code between components is?
sobes.tech AI
Answer from AI
Low coupling code means that system components depend on each other minimally. This increases flexibility, makes testing and maintenance easier.
Example:
- Components communicate through well-defined interfaces rather than directly with each other's internal details.
- Changes in one component minimally affect others.
For example, if you have a class that depends on an interface rather than a specific implementation, you can easily replace the implementation without changing the code that uses this interface.
interface PaymentProcessor {
void processPayment(double amount);
}
class PaypalProcessor implements PaymentProcessor {
public void processPayment(double amount) {
// implementation via PayPal
}
}
class OrderService {
private PaymentProcessor paymentProcessor;
public OrderService(PaymentProcessor paymentProcessor) {
this.paymentProcessor = paymentProcessor;
}
public void checkout(double amount) {
paymentProcessor.processPayment(amount);
}
}
Here, OrderService does not depend on a specific payment method, only on the interface, which reduces coupling.