One idea
SOLID is not a rulebook to impress seniors. It is a set of checks that helps your feature stay easy to change without breaking everything else.
Use it after you have working code. First ship the feature. Then clean the design where change is likely.
The five checks
S means Single Responsibility. A class should have one main reason to change. For example, Order should not also send WhatsApp messages and write invoices.
O means Open/Closed. Add a new behaviour by adding a new class when possible, not by editing one big if-else block every time.
The next three
L means Liskov Substitution. If code expects a PaymentMethod, any UpiPayment or CardPayment should work without surprise errors.
I means Interface Segregation. Do not force a class to implement methods it does not need. D means Dependency Inversion. High-level code should depend on interfaces, not hard-coded concrete classes.
Small payment example
Here is a simple Dependency Inversion and Open/Closed example. Checkout uses the PaymentMethod interface. Adding NetBankingPayment later will not require changing Checkout.
interface PaymentMethod {
boolean pay(int amountInRupees);
}
class UpiPayment implements PaymentMethod {
public boolean pay(int amountInRupees) {
System.out.println("Paid ₹" + amountInRupees + " using UPI");
return true;
}
}
class Checkout {
private final PaymentMethod paymentMethod;
Checkout(PaymentMethod paymentMethod) {
this.paymentMethod = paymentMethod;
}
boolean placeOrder(int amountInRupees) {
return paymentMethod.pay(amountInRupees);
}
}
class Main {
public static void main(String[] args) {
Checkout checkout = new Checkout(new UpiPayment());
checkout.placeOrder(499);
}
}Do not over-SOLID
Beginners sometimes create ten interfaces for a two-class feature. That makes the repo harder to read, not better.
Use SOLID to remove pain. If there is only one implementation and no likely change, a direct class may be fine.
Use AI carefully
Ask an AI tool to review your object model using SOLID. Then check the advice yourself. AI often suggests extra abstractions you do not need.
A useful prompt: Review these classes for SOLID issues. Suggest only changes that make the feature easier to test or extend. Avoid adding patterns unless needed.
SOLID review pass
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done
Do this now
Do each one yourself, then tap it to tick it off. The ticks are only a checklist for you: they are not marked or scored.
0 of 5 done