Give an example of SRP violation.

Answer

A classic SRP violation is a User class that handles authentication, profile management, email sending, and database persistence all in one. Consider: class User { login() {}, logout() {}, updateProfile() {}, sendWelcomeEmail() {}, saveToDatabase() {} }. This class has five distinct reasons to change: changes to auth logic, profile structure, email templates, or database schema all require modifying the same class. A SOLID refactoring splits this into AuthService, UserProfileService, EmailService, and UserRepository. Each class now has a single, clearly defined responsibility and can be modified, tested, or replaced independently.