Skip to content

HananHIssa/Solid-Principles

Repository files navigation

🌟 SOLID Principles and More

🌈 Once Upon a Time in the World of Code...

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.

📚 What is SOLID?

It's not just a word; it's a philosophy — an acronym for five design principles that help you write robust, scalable code:

S - Single Responsibility Principle (SRP)

  • "A class should have only one reason to change."
  • A class should do only one job.

O - Open/Closed Principle (OCP)

  • "Software entities should be open for extension but closed for modification."
  • Add new functionality without changing existing code.

L - Liskov Substitution Principle (LSP)

  • "Subtypes must be substitutable for their base types."
  • A child class should be usable in place of a parent class.

I - Interface Segregation Principle (ISP)

  • "No client should be forced to depend on methods it does not use."
  • Prefer smaller, more specific interfaces.

D - Dependency Inversion Principle (DIP)

  • "High-level modules should not depend on low-level modules. Both should depend on abstractions."
  • Decouple modules to improve flexibility.

📄 Examples and Unit Testing

In the provided code examples, each SOLID principle was demonstrated practically, with simple unit tests verifying the concepts.


🧪 Quick Note About Unit Testing

When writing unit tests, always follow 3 steps:

  1. Arrange → Prepare objects and inputs.
  2. Act → Call the method you want to test.
  3. 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);
}

🌟 Bonus: Other Things I've Learned

🔹 Polymorphism in OOP

Polymorphism allows calling a child class through its parent reference, determined at:

  • Runtime → Dynamic method overriding (needs inheritance).
  • Compile-time → Static method overloading.

✨ Dynamic Method Overriding Example

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
    }
}

✨ Static Method Overloading Example

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
    }
}

🔹 Static Methods, Static Classes vs. Instance Methods, Instance Classes

🔍 Static Method

  • Belongs to the class itself.
  • Cannot be overridden.

🔍 Instance Method

  • Belongs to an object.
  • Can be overridden.

🔍 Static Class

  • Only nested classes in Java can be static.

🔍 Instance Class

  • Regular class, creates multiple instances.

✔️ Quick Example

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();
    }
}

📚 Sources:


I hope this clean structure helps you present your project even more professionally!


About

The SOLID principles are a set of five key guidelines designed to improve software architecture, making code cleaner, more maintainable, and highly scalable.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages