SOLID Principles — Backbone of System Design

Abhishek Ranjan
5 min readApr 7, 2023

--

SOLID is an acronym representing a set of five design principles that help developers write maintainable, scalable, and flexible code. These principles, coined by Robert C. Martin, are essential for creating robust and easy-to-understand software. In this article, we will explore each SOLID principle with practical Java code examples and tips on how to ensure your code adheres to these principles.

1. Single Responsibility Principle (SRP)

The SRP states that a class should have only one reason to change, meaning it should have only one responsibility. This principle helps in achieving separation of concerns in your code.

Example

Consider an application that manages employees and their salaries.

// Violating SRP
class Employee {
private String name;
private double salary;
// ...
public void save() {
// Code to save the employee to a database
}
public double calculateTotalSalary() {
// Code to calculate salary with deductions and bonuses
}
}

In the above example, the Employee class is responsible for both employee data management and salary calculations. This violates the SRP.

A better approach:

class Employee {
private String name;
private double salary;
// ...
}
class EmployeeRepository {
public void save(Employee employee) {
// Code to save the employee to a database
}
}
class SalaryCalculator {
public double calculateTotalSalary(Employee employee) {
// Code to calculate salary with deductions and bonuses
}
}

Here, we separate the concerns into three classes, each with a single responsibility.

2. Open/Closed Principle (OCP)

The OCP states that software entities should be open for extension but closed for modification. This means that new functionality should be added by extending the existing codebase without modifying it.

Example

Imagine a simple application that calculates the area of different shapes.

class Rectangle {
public double width;
public double height;
}

class Circle {
public double radius;
}
class AreaCalculator {
public double calculateArea(Object shape) {
if (shape instanceof Rectangle) {
Rectangle rectangle = (Rectangle) shape;
return rectangle.width * rectangle.height;
} else if (shape instanceof Circle) {
Circle circle = (Circle) shape;
return Math.PI * circle.radius * circle.radius;
}
throw new IllegalArgumentException("Invalid shape");
}
}

If we need to add a new shape, we must modify the AreaCalculator class, which violates the OCP. Instead, we can use inheritance and polymorphism to achieve OCP.

interface Shape {
double calculateArea();
}

class Rectangle implements Shape {
public double width;
public double height;
@Override
public double calculateArea() {
return width * height;
}
}
class Circle implements Shape {
public double radius;
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
}
class AreaCalculator {
public double calculateArea(Shape shape) {
return shape.calculateArea();
}
}

Now, adding a new shape requires extending the Shape interface without modifying the AreaCalculator class.

3. Liskov Substitution Principle (LSP)

The LSP states that objects of a derived class should be able to replace objects of the base class without affecting the correctness of the program. This principle ensures that inheritance is used correctly.

Example

Consider a simple example of a Bird class.

class Bird {
public void fly() {
// ...
}
}
class Penguin extends Bird {
@Override
public void fly() {
throw new UnsupportedOperationException("Penguins can't fly");
}
}
class BirdController {
public void letBirdFly(Bird bird) {
bird.fly();
}
}

n this example, the Penguin class violates the LSP because a Penguin cannot replace a Bird without altering the correctness of the program. A better approach is to use interfaces for each behavior.

interface Flyable {
void fly();
}

class Bird {
// ...
}
class FlyingBird extends Bird implements Flyable {
@Override
public void fly() {
// ...
}
}
class Penguin extends Bird {
// ...
}
class BirdController {
public void letBirdFly(Flyable bird) {
bird.fly();
}
}

Now, the LSP is maintained, and we can use the BirdController for any bird implementing the Flyable interface.

4. Interface Segregation Principle (ISP)

The ISP states that clients should not be forced to implement interfaces they do not use. Instead, interfaces should be split into smaller, more specific ones so that clients only need to implement the methods they require.

Example

Consider an AllInOnePrinter class that has multiple functionalities.

interface AllInOnePrinter {
void print();
void scan();
void fax();
}

class Printer implements AllInOnePrinter {
@Override
public void print() {
// ...
}
@Override
public void scan() {
// ...
}
@Override
public void fax() {
// ...
}
}

If a new printer only needs the print functionality, it will still be forced to implement the unused methods. A better solution is to separate the interface into smaller, more specific interfaces.

interface Printer {
void print();
}

interface Scanner {
void scan();
}
interface Fax {
void fax();
}
class AllInOnePrinter implements Printer, Scanner, Fax {
@Override
public void print() {
// ...
}
@Override
public void scan() {
// ...
}
@Override
public void fax() {
// ...
}
}
class BasicPrinter implements Printer {
@Override
public void print() {
// ...
}
}

Now, clients only need to implement the interfaces they require.

5. Dependency Inversion Principle (DIP)

The DIP states that high-level modules should not depend on low-level modules; both should depend on abstractions. Additionally, abstractions should not depend on details; details should depend on abstractions. This principle promotes loose coupling between modules.

Example

Consider a simple application that reads and processes text data from different sources.

class FileReader {
String read() {
// Read data from a file
}
}
class DataProcessor {
private final FileReader fileReader;
DataProcessor(FileReader fileReader) {
this.fileReader = fileReader;
}
void processData() {
String data = fileReader.read();
// Process data
}
}

In this example, the DataProcessor class is tightly coupled to the FileReader class. To achieve the DIP, we can introduce an interface for data readers.

interface DataReader {
String read();
}
class FileReader implements DataReader {
@Override
public String read() {
// Read data from a file
}
}
class DatabaseReader implements DataReader {
@Override
public String read() {
// Read data from a
// Read data from a database
}
}
class DataProcessor {
private final DataReader dataReader;
DataProcessor(DataReader dataReader) {
this.dataReader = dataReader;
}
void processData() {
String data = dataReader.read();
// Process data
}
}

Now, the DataProcessor class depends on the abstraction DataReader rather than the concrete FileReader class. This allows for easy integration of new data readers without modifying the DataProcessor.

Tips for Ensuring SOLID Principles in Your Code

  1. Refactor regularly: Periodically review your code and refactor it to adhere to the SOLID principles. Regular refactoring keeps your code clean, maintainable, and scalable.
  2. Embrace test-driven development (TDD): Writing tests before implementing functionality helps ensure that your code follows the SOLID principles, as TDD encourages modular design and separation of concerns.
  3. Utilize code reviews: Have your code reviewed by peers or use automated code review tools to spot potential violations of SOLID principles.
  4. Design with interfaces: Whenever possible, design your classes and modules using interfaces. This promotes loose coupling, making it easier to adhere to SOLID principles.
  5. Stay informed: Keep up-to-date with best practices in software design, and regularly assess your codebase to ensure it adheres to the SOLID principles.

By following these tips and understanding the SOLID principles, you will be better equipped to design and maintain robust, scalable, and flexible Java applications.

--

--