Fundamentals 9 min read

Understanding the Single Responsibility Principle (SRP) with UML Diagrams and Java Code Examples

This article explains the Single Responsibility Principle, its core idea, why it should be followed, its three main benefits, and provides UML class diagrams and Java code examples that demonstrate how to split responsibilities into dedicated classes for clearer, more maintainable software.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Understanding the Single Responsibility Principle (SRP) with UML Diagrams and Java Code Examples

The Single Responsibility Principle (SRP), also known as the Single Function Principle, states that a class should have only one reason to change, meaning it should perform a single, well‑defined task.

What is the Single Responsibility Principle

SRP requires each class to focus on one specific responsibility, which improves understandability, maintainability, and extensibility of code.

Why Follow SRP

When each class has a clear, single responsibility, code complexity is reduced, maintenance becomes easier, and changes affecting one responsibility do not unintentionally impact others.

Changes to one responsibility do not affect other responsibilities.

Clients only need the functionality they require, avoiding unnecessary dependencies.

UML Class Diagram for SRP

The diagram shows how responsibilities are split into separate classes such as FinancialApartment and HRApartment instead of being combined in a single Employee class.

Concrete Example: Hospital System

Consider a Doctor class that currently handles doctor information, patient diagnosis, prescription, and surgery scheduling. This violates SRP because it mixes four distinct responsibilities.

class Doctor {
    private String name;
    private String specialization;

    public Doctor(String name, String specialization) {
        this.name = name;
        this.specialization = specialization;
    }

    public void diagnosePatient(Patient patient) {
        // diagnosis logic
    }

    public void prescribeMedicine(Patient patient, String medicine) {
        // prescription logic
    }

    public void scheduleSurgery(Patient patient, Date date) {
        // surgery scheduling logic
    }

    // other doctor‑related methods and fields
}

To comply with SRP, the responsibilities are split into three classes:

class Doctor {
    private String name;
    private String specialization;
    // only doctor information management
}

class Patient {
    // patient information management
}

class MedicalStaff {
    public void diagnosePatient(Doctor doctor, Patient patient) { /* ... */ }
    public void prescribeMedicine(Doctor doctor, Patient patient, String medicine) { /* ... */ }
    public void scheduleSurgery(Doctor doctor, Patient patient, Date date) { /* ... */ }
}

Three Major Benefits of SRP

Reduces class complexity.

Improves code readability and maintainability.

Lowers the risk associated with changes.

Implementation Example: E‑commerce Order Processing

In an order system, separate the responsibilities of order management and shipping:

// Order management class
class OrderManager {
    public void createOrder(Order order) { /* ... */ }
    public void updateOrder(Order order) { /* ... */ }
    public Order getOrder(int orderId) { return null; }
}

// Shipping processing class
class ShippingProcessor {
    public void shipOrder(Order order) { /* ... */ }
}

// Order entity
class Order {
    private int orderId;
    private String customerName;
    private String shippingAddress;
    // other fields and methods
    public Order(int orderId, String customerName, String shippingAddress) {
        this.orderId = orderId;
        this.customerName = customerName;
        this.shippingAddress = shippingAddress;
    }
}

public class Main {
    public static void main(String[] args) {
        OrderManager orderManager = new OrderManager();
        ShippingProcessor shippingProcessor = new ShippingProcessor();
        Order newOrder = new Order(1, "John Doe", "123 Main St");
        orderManager.createOrder(newOrder);
        Order retrieved = orderManager.getOrder(1);
        shippingProcessor.shipOrder(retrieved);
    }
}

Each class now has a single, well‑defined responsibility, reducing coupling and making the system easier to evolve.

Conclusion

The core of SRP is to let each class focus on one clear responsibility, which enhances maintainability and readability. Determining when to split responsibilities depends on how changes affect the code: if a change impacts only one responsibility, split it; otherwise, keep it together.

Understanding and applying SRP is essential for Java interviews and real‑world software design.

Javasoftware designUMLCode Exampleobject-orientedsingle responsibility principleSRP
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.