images
Techligious

SOLID Principles

  • Home
  • SOLID Principles
images
images
09 Apr

SOLID Principles

SOLID principles first introduced by the famous Computer Scientist Robert J. Martin (a.k.a Uncle Bob) in 2000. Further, acronyms given by Michael Feathers.

Single Responsibility Principle [SRP]

  • Class should implement only one functionality
  • Tightly coupled
  • A class should have one and only reason to change
  • Easy to test and maintain

#sample code in C language

// Function for calculating the area of a rectangle

float calculateRectangleArea(float width, float height) {

    return width * height;

}

// Function for printing the area of a rectangle

void printRectangleArea(float area) {

    printf(“The area of the rectangle is: %.2f\n”, area);

}

#sample code in python

class Rectangle:

    def __init__(self, width, height):

        self.width = width

        self.height = height

    def calculate_area(self):

        return self.width * self.height

class RectanglePrinter:

    def print_area(self, area):

        print(f”The area of the rectangle is: {area}”)

Open / Closed Principle

  • Open for extension and Close for modification
  • Create interface – create alternative implementation of the class by implementing the same interface
  • Create abstract class
  • Create inheritance – create a child class of existing class and override required methods that needs modifications
  • To allow for new functionality to be added without modifying existing code.

#sample code in C language

// Shape interface

typedef struct {

    float (*calculateArea)();

} Shape;

// Rectangle implementation

float calculateRectangleArea() {

    // Calculate rectangle area

}

Shape* createRectangle() {

    Shape* rectangle = malloc(sizeof(Shape));

    rectangle->calculateArea = &calculateRectangleArea;

    return rectangle;

}

#sample code in python

from abc import ABC, abstractmethod

# Shape interface

class Shape(ABC):

    @abstractmethod

    def calculate_area(self):

        pass

# Rectangle implementation

class Rectangle(Shape):

    def calculate_area(self):

        # Calculate rectangle area

def create_rectangle():

    return Rectangle()

Liskov Substitution Principle

  • If overriding the method => it should not violet the functionality of original method
  • Solution – create interface
  • Ensures that inheritance hierarchies are designed correctly and that polymorphism can be effectively utilized

#sample code in C language

// Base shape struct

typedef struct {

    float (*calculateArea)();

} Shape;

// Rectangle implementation

float calculateRectangleArea() {

    // Calculate rectangle area

}

Shape* createRectangle() {

    Shape* rectangle = malloc(sizeof(Shape));

    rectangle->calculateArea = &calculateRectangleArea;

    return rectangle;

}

// Function using shapes

void printArea(Shape* shape) {

    float area = shape->calculateArea();

    printf(“The area is: %.2f\n”, area);

}

#sample code in python

# Base shape class

class Shape:

    def calculate_area(self):

        pass

# Rectangle implementation

class Rectangle(Shape):

    def calculate_area(self):

        # Calculate rectangle area

def print_area(shape):

    area = shape.calculate_area()

    print(f”The area is: {area}”)

Interface segregation Principle

  • No client class should be forced to depend on methods it doesn’t use
  • Prefer to make many smaller interfaces rather than one single big interface

Dependency Inversion Principle

  • Higher level modules should not depend on lower level modules
  • Both should depend on abstraction
  • Loose coupling and dependency injection
NEWSLETTER

Subscribe to our newsletter