Skip to content

Commit 57f5dc7

Browse files
committed
docs: sample of using god service
Signed-off-by: Otavio Santana <otaviopolianasantana@gmail.com>
1 parent 86f24b3 commit 57f5dc7

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
= ☕ Refactoring the God Service: Improving Coupling and Cohesion
2+
Otávio Santana
3+
:icons: font
4+
:source-highlighter: rouge
5+
:toc:
6+
7+
== Before: The God Service Class
8+
9+
A single class doing too much — low cohesion, high coupling.
10+
11+
[source,java]
12+
----
13+
class UserService {
14+
void registerUser(User user) { /* DB logic */ }
15+
16+
void sendWelcomeEmail(User user) { /* email SMTP logic */ }
17+
18+
void generatePdfReport(User user) { /* PDF rendering */ }
19+
20+
void scheduleUserSync(User user) { /* scheduler logic */ }
21+
22+
void logUserHistory(User user) { /* audit log writing */ }
23+
}
24+
----
25+
26+
== ✅ After: Focused Service Classes
27+
28+
Each class has a clear purpose (high cohesion), and dependencies are reduced (low coupling):
29+
30+
[source,java]
31+
----
32+
class UserRegistrationService {
33+
void register(User user) { /* only registration logic */ }
34+
}
35+
36+
class EmailNotificationService {
37+
void sendWelcome(User user) { /* SMTP client */ }
38+
}
39+
40+
class ReportService {
41+
void generateUserPdf(User user) { /* PDF generation */ }
42+
}
43+
44+
class UserSyncScheduler {
45+
void schedule(User user) { /* async sync logic */ }
46+
}
47+
48+
class AuditLogger {
49+
void log(User user) { /* persistence of logs */ }
50+
}
51+
----
52+
53+
== 🎨 Diagram: Separation of Concerns
54+
55+
[source, mermaid]
56+
----
57+
classDiagram
58+
class UserRegistrationService {
59+
+register(User)
60+
}
61+
62+
class EmailNotificationService {
63+
+sendWelcome(User)
64+
}
65+
66+
class ReportService {
67+
+generateUserPdf(User)
68+
}
69+
70+
class UserSyncScheduler {
71+
+schedule(User)
72+
}
73+
74+
class AuditLogger {
75+
+log(User)
76+
}
77+
78+
class User
79+
80+
UserRegistrationService --> User
81+
EmailNotificationService --> User
82+
ReportService --> User
83+
UserSyncScheduler --> User
84+
AuditLogger --> User
85+
----
86+
87+
== 💡 Summary
88+
89+
- Focused classes increase **maintainability**
90+
- Reduces side effects when changing behavior
91+
- Easier to test and debug each responsibility independently

0 commit comments

Comments
 (0)