Are you ready to dive into an inspiring story about Uncle Bob?
Uncle Bob, a true legend in the world of Clean Code, believed that code should be as clean and beautiful as a well-crafted masterpiece.
In 2000, he published the article "Design Principles and Design Patterns," highlighting that software must be maintainable, flexible, and ready for change.
From these insights, Uncle Bob crafted a golden formula:
He called it SOLID.
It's not just a word; it's a philosophy — an acronym for five design principles that help you write robust, scalable code:
- "A class should have only one reason to change."
- A class should do only one job.
- "Software entities should be open for extension but closed for modification."
- Add new functionality without changing existing code.
- "Subtypes must be substitutable for their base types."
- A child class should be usable in place of a parent class.
- "No client should be forced to depend on methods it does not use."
- Prefer smaller, more specific interfaces.
- "High-level modules should not depend on low-level modules. Both should depend on abstractions."
- Decouple modules to improve flexibility.
In the provided code examples, each SOLID principle was demonstrated practically, with simple unit tests verifying the concepts.
When writing unit tests, always follow 3 steps:
- Arrange → Prepare objects and inputs.
- Act → Call the method you want to test.
- Assert → Verify the result using assertions like
assertEquals(expected, actual, delta).
✅ Keep tests clean, simple, and focused on one functionality.
Example:
@Test
public void testRectangleArea() {
// Arrange
LiskovSSubstitutionPrincipleUpdate.Rectangle rectangle = new LiskovSSubstitutionPrincipleUpdate.Rectangle(5, 10);
// Act
double actualArea = rectangle.calculateArea();
// Assert
assertEquals(50.0, actualArea, 0.0001);
}Polymorphism allows calling a child class through its parent reference, determined at:
- Runtime → Dynamic method overriding (needs inheritance).
- Compile-time → Static method overloading.
abstract class Animal {
public abstract void sound();
}
class Dog extends Animal {
@Override
public void sound() {
System.out.println("Bark");
}
}
class Cat extends Animal {
@Override
public void sound() {
System.out.println("Meow");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.sound(); // Output: Bark
}
}public class Calculator {
public static int add(int number1, int number2) {
return number1 + number2;
}
public static int add(int number1, int number2, int number3) {
return number1 + number2 + number3;
}
public static void main(String[] args) {
System.out.println(add(1, 2)); // Output: 3
System.out.println(add(1, 2, 3)); // Output: 6
}
}- Belongs to the class itself.
- Cannot be overridden.
- Belongs to an object.
- Can be overridden.
- Only nested classes in Java can be static.
- Regular class, creates multiple instances.
public class Example {
static class StaticClass {
public static void staticMethod() {
System.out.println("Static method called.");
}
}
public void instanceMethod() {
System.out.println("Instance method called.");
}
public static void main(String[] args) {
StaticClass.staticMethod();
Example example = new Example();
example.instanceMethod();
}
}- JavaTechOnline - MCQ on SOLID Principles
- Skilr - SOLID Principles Practice Exam
- QuizGecko - SOLID Principles Quiz
- GeeksforGeeks - SOLID Principle in Programming
✨ I hope this clean structure helps you present your project even more professionally! ✨