-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18.DesignAGenericNotificationSystem.java
More file actions
170 lines (146 loc) · 6.19 KB
/
18.DesignAGenericNotificationSystem.java
File metadata and controls
170 lines (146 loc) · 6.19 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* ----------------------------------------------------------------------------
LLD - Generic Notification System
(For YouTube Explanation by Bharadwaj)
-------------------------------------------------------------------------------
1) a) Functional Requirements
----------------------------
- System should send notifications via multiple channels (Email, SMS, Push).
- Each channel should follow a common interface for extensibility.
- Allow adding new channels easily (Open/Closed principle).
- Should support sending messages to multiple users.
b) Non-Functional Requirements
------------------------------
- Extensible design (easy to add new channels)
- Maintainable and loosely coupled
- Lightweight (no external DB)
- Thread-safe for concurrent sends
-------------------------------------------------------------------------------
2) Algorithm Choice Discussion
------------------------------
- Core algorithm: Observer Pattern
(Each notification channel observes and reacts to a common event.)
- Strategy Pattern applies since each channel implements its own send strategy.
- Time complexity: O(n) per notification dispatch.
- Space complexity: O(n) for channel registry.
-------------------------------------------------------------------------------
3) Concurrency and Data Model Discussion
----------------------------------------
- Uses in-memory list (no database dependency).
- Thread-safe using synchronized blocks.
- Each send operation runs concurrently using ExecutorService.
-------------------------------------------------------------------------------
4) UML Diagram (ASCII Representation)
-------------------------------------
+----------------------+
| NotificationManager |
+----------------------+
| - channels: List |
+----------+-----------+
|
+---------+----------+
| |
+---------------+ +---------------+
| EmailNotifier | | SMSNotifier |
+---------------+ +---------------+
| +send(msg) | | +send(msg) |
+---------------+ +---------------+
| |
+---------+----------+
|
+--------------------+
| PushNotifier |
+--------------------+
| +send(msg) |
+--------------------+
-------------------------------------------------------------------------------
5) Correct Solution in Java (Runnable in VS Code)
-------------------------------------------------------------------------------
*/
import java.util.*;
import java.util.concurrent.*;
// 1️⃣ Common Interface for all Notifiers
interface Notifier {
void send(String message);
}
// 2️⃣ Concrete Implementations
class EmailNotifier implements Notifier {
@Override
public void send(String message) {
try { Thread.sleep(500); } catch (InterruptedException e) {}
System.out.println("📧 Email Sent: " + message);
}
}
class SMSNotifier implements Notifier {
@Override
public void send(String message) {
try { Thread.sleep(300); } catch (InterruptedException e) {}
System.out.println("📱 SMS Sent: " + message);
}
}
class PushNotifier implements Notifier {
@Override
public void send(String message) {
try { Thread.sleep(200); } catch (InterruptedException e) {}
System.out.println("🔔 Push Notification Sent: " + message);
}
}
// 3️⃣ Notification Manager - Controller class
class NotificationManager {
private final List<Notifier> channels = new ArrayList<>();
// Thread-safe channel registration
public synchronized void registerChannel(Notifier notifier) {
channels.add(notifier);
}
// Concurrently send notifications via ExecutorService
public void sendNotification(String message) {
ExecutorService executor = Executors.newFixedThreadPool(channels.size());
for (Notifier ch : channels) {
executor.submit(() -> ch.send(message));
}
executor.shutdown();
try {
executor.awaitTermination(2, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// 4️⃣ Demo Execution (Main Class)
public class NotificationSystemDemo {
public static void main(String[] args) {
NotificationManager manager = new NotificationManager();
manager.registerChannel(new EmailNotifier());
manager.registerChannel(new SMSNotifier());
manager.registerChannel(new PushNotifier());
System.out.println("🚀 Sending Notifications...\n");
manager.sendNotification("Hello, Bharadwaj Subscribers! 🎥🔥");
System.out.println("\n✅ Notification dispatch complete!");
}
}
/*
-------------------------------------------------------------------------------
Expected Output:
-------------------------------------------------------------------------------
🚀 Sending Notifications...
📧 Email Sent: Hello, Bharadwaj Subscribers! 🎥🔥
📱 SMS Sent: Hello, Bharadwaj Subscribers! 🎥🔥
🔔 Push Notification Sent: Hello, Bharadwaj Subscribers! 🎥🔥
✅ Notification dispatch complete!
-------------------------------------------------------------------------------
6) Limitations of Current Code
------------------------------
- No retry mechanism for failed sends.
- No message queue or scheduling.
- In-memory only (no persistence layer).
- Simple text message handling (no templates or attachments).
-------------------------------------------------------------------------------
7) Alternative Algorithms and Trade Off's (Future Discussions)
--------------------------------------------------------------
- Event-driven architecture using Pub/Sub (Highly scalable, but adds infra complexity)
- Use Kafka/RabbitMQ for async notifications (Reliable but heavy setup)
- Template engine + user preferences (Customizable but more complex)
- Persistent storage for logs (Adds reliability but more overhead)
-------------------------------------------------------------------------------
END OF FILE
-------------------------------------------------------------------------------
*/