-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-single-responsibility-principle.java
More file actions
73 lines (58 loc) · 2.01 KB
/
Copy path01-single-responsibility-principle.java
File metadata and controls
73 lines (58 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Noncompliant Example: Violates Single Responsibility Principle
class Employee {
private String name;
private String email;
public Employee(String name, String email) {
this.name = name;
this.email = email;
}
public void saveToDatabase() {
// Code to save employee to database
}
public void sendEmail() {
// Code to send email to employee
}
}
// Compliant Example: Follows Single Responsibility Principle
class EmployeeData {
private String name;
private String email;
public EmployeeData(String name, String email) {
this.name = name;
this.email = email;
}
// Getters and setters
}
class EmployeeRepository {
public void save(EmployeeData employee) {
// Code to save employee to database
}
}
class EmailService {
public void sendEmail(EmployeeData employee) {
// Code to send email to employee
}
}
/*
Key Points of Single Responsibility Principle (SRP):
1. DEFINITION: A class should have only one reason to change
- Each class should have only one job or responsibility
2. BAD EXAMPLE ANALYSIS:
- Employee class handles data, database operations, and email operations
- Has multiple reasons to change (data structure, database logic, email logic)
3. GOOD EXAMPLE ANALYSIS:
- EmployeeData: Only responsible for holding employee data
- EmployeeRepository: Only responsible for database operations
- EmailService: Only responsible for email operations
4. Benefits of following SRP:
- Easier to maintain, test, and modify code
- Changes to one responsibility don't affect others
- Better code organization and reusability
5. How to achieve SRP:
- Break down large classes into smaller, focused classes
- Identify different responsibilities within a class
- Use composition to combine different responsibilities
- Follow the "Do one thing and do it well" philosophy
This principle helps create clean, maintainable code where each class
has a single, well-defined purpose.
*/