August 12, 2024August 19, 2024 Single Responsibility Principle (SRP) A class should have only one reason to change. Example: Consider a class that handles both user data and its formatting. This violates SRP. // Violation of SRP public class User { private String name; private String email; public User(String name, String email) { this.name = name; this.email = email; } // Responsibility 1: Managing user data public String getName() { return name; } public String getEmail() { return email; } // Responsibility 2: Formatting user data public String getUserDetails() { return "Name: " + name + ", Email: " + email; } } // Following SRP public class User { private String name; private String email; public User(String name, String email) { this.name = name; this.email = email; } public String getName() { return name; } public String getEmail() { return email; } } public class UserFormatter { public String formatUserDetails(User user) { return "Name: " + user.getName() + ", Email: " + user.getEmail(); } } Here, the User class handles only user data, while UserFormatter handles formatting. This separation ensures each class has only one reason to change. Next: Open/Closed Principle (OCP) SOLID Solid Design Principle Open/Closed Principle (OCP)Single Responsibility Principle (SRP)