Skip to content

Commit c889547

Browse files
committed
design patterns learning continued
1 parent b3ceb48 commit c889547

File tree

18 files changed

+234
-1
lines changed

18 files changed

+234
-1
lines changed

D/design_patterns/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
🧩 Types of Design Patterns:
2+
Creational — Object kaise banaye (e.g., Singleton, Factory)
3+
4+
Structural — Objects ko efficiently kaise connect karein (e.g., Adapter, Decorator)
5+
6+
Behavioral — Object ka behavior kaise manage karein (e.g., Observer, Strategy)
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Behavioural Design Paterns
2+
3+
Yeh patterns batate hain ki objects aapas mein kaise interact karte hain — like communication rules.
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Observer Patern (pub - sub model)
2+
3+
Ek object mein jab koi change aaye, toh automatically dusre dependent objects ko notify karna.
4+
5+
“Observer pattern ek publish-subscribe (pub-sub) mechanism hai jisme ek subject (observable) apne observers (subscribers) ko notify karta hai jab bhi koi change hota hai.”
6+
7+
8+
- **Subject** (Obesrvable) isko observe kiya jata hai / isko observer krte hai
9+
10+
- **Observer** (Subscriber) ye log observe krte hai
11+
12+
13+
Example : Notification delivery on new video update on Youtube
14+
15+
16+
## STEPS to Create Observer
17+
//1. Observwble classbanaao
18+
//2. constructor mai ek observer space/array bnaao.
19+
// 3. subscribe , unsubscribe, notify y 3 method do
20+
// 4. subscribe matlab jo bhi fumction mila usko observer mai push kr
21+
// 5. unsubscribe matlab observr mai ye vo fun pop
22+
// 6. notify matlab observer k andar k sare fun ko line se execute kr
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// STEPS to Create Observer
2+
//1. Observwble classbanaao
3+
//2. constructor mai ek observer space/array bnaao.
4+
// 3. subscribe , unsubscribe, notify y 3 method do
5+
// 4. subscribe matlab jo bhi fumction mila usko observer mai push kr
6+
// 5. unsubscribe matlab observr mai ye vo fun pop
7+
// 6. notify matlab observer k andar k sare fun ko line se execute kr
8+
9+
class Observable {
10+
constructor() {
11+
this.observers = [];
12+
}
13+
14+
subscribe(fn) {
15+
this.observers.push(fn);
16+
}
17+
18+
unsubscribe(fn) {
19+
this.observers = this.observers.filter((observer) => observer !== fn);
20+
}
21+
22+
notify(data) {
23+
this.observers.forEach((observer) => observer(data));
24+
}
25+
}
26+
27+
// USAGE
28+
const newsAgency = new Observable();
29+
30+
function subscriber1(news) {
31+
console.log("Subscriber 1 received:", news);
32+
}
33+
34+
function subscriber2(news) {
35+
console.log("Subscriber 2 received:", news);
36+
}
37+
38+
newsAgency.subscribe(subscriber1);
39+
newsAgency.subscribe(subscriber2);
40+
41+
newsAgency.notify("🔥 New Article on Design Patterns!");

D/design_patterns/builder/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Builder / Creational
2+
3+
Yeh patterns kaam aate hain jab object creation ko flexible aur controlled banana ho.
4+

D/design_patterns/builder/factory/README.js

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Car {
2+
constructor(wheels, isEv, topSpeed) {
3+
this.wheels = wheels;
4+
this.isEv = isEv;
5+
this.topSpeed = topSpeed;
6+
}
7+
}
8+
9+
class Bike {
10+
constructor(wheels, isEv, topSpeed) {
11+
this.wheels = wheels;
12+
this.isEv = isEv;
13+
this.topSpeed = topSpeed;
14+
}
15+
}
16+
17+
class VehicleFactory {
18+
static createVehicle(type, config = {}) {
19+
switch (type) {
20+
case "car":
21+
return new Car(config.wheels ?? 4, true, 250);
22+
case "bike":
23+
return new Bike(2, false, 180);
24+
default:
25+
new Error("type not found");
26+
}
27+
}
28+
}
29+
30+
let car1 = VehicleFactory.createVehicle("car", {"wheels":8});
31+
32+
console.log(car1);

D/design_patterns/builder/singleton/README.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Singleton {
2+
constructor() {
3+
// already instance hai to vahi return kar do
4+
if (Singleton._instance) {
5+
return Singleton._instance;
6+
}
7+
// common prop across all the objects created
8+
this.count = 0;
9+
// instance nhi hai to instance k value current object set krdo .
10+
Singleton._instance = this;
11+
12+
// agar constructor mai return statemant nhi likha hai to vo current object(Singleton) return kr dega
13+
}
14+
15+
increaseCount() {
16+
this.count++;
17+
console.log("Count:", this.count);
18+
}
19+
}
20+
21+
let p1 = new Singleton();
22+
let p2 = new Singleton();
23+
24+
25+
p1.increaseCount();
26+
p2.increaseCount();
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Structural
2+
3+
Inka kaam hota hai objects ke beech ka structure manage karna ya simplify karna.
4+

F/for/for-loop.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
//? 1. Normal break statement
3+
4+
for (let i = 0; i < 10; i++) {
5+
if (i === 5) break;
6+
console.log(i);
7+
}
8+
9+
10+
//? 2. Using Return statement in loop
11+
12+
for (let item of arr) {
13+
if (item === val) return "Found!";
14+
}
15+
16+
//? 3. Using labels
17+
18+
outerLoop: for (let i = 0; i < 3; i++) {
19+
for (let j = 0; j < 3; j++) {
20+
if (i === 1 && j === 1) break outerLoop;
21+
console.log(`i=${i}, j=${j}`);
22+
}
23+
}
24+
25+
// ? 4. throw Exception
26+
27+
try {
28+
for (let i = 0; i < 10; i++) {
29+
if (i === 5) throw new Error("Abort loop");
30+
console.log(i);
31+
}
32+
} catch (e) {
33+
console.log("Loop aborted");
34+
}

F/formater/formater.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let amount = 23482719.98;
2+
3+
const formater = new Intl.NumberFormat("hin-IN", {
4+
style: "currency",
5+
currency: "INR",
6+
});
7+
8+
9+
let formatedAmount = formater.format(amount);
10+
11+
console.log(formatedAmount);

S/server-sent-events/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Server Sent Events
2+
3+
4+
Socho ek radio station hai (server), aur kai listeners hain (clients/browsers).
5+
Listeners apna radio on karte hain aur station se awaaz (updates) sunte rehte hain.
6+
Radio station baar-baar poocha nahi jaata, ki "kuch naya hai kya?"
7+
Bas jab kuch naya aata hai, radio khud bol deta hai — aur listeners sun lete hain.
8+
9+
Yeh hi concept hai SSE ka.

S/server-sent-events/sse_backend.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
app.get("/endpoint", (req, res)=>{
2+
3+
// this 3 header are most important to keep the stream alive
4+
res.header = {'Content-Type': 'text/event-stream'}
5+
res.header = {'Cache-Control': 'no-cache'}
6+
res.header = {'Connection': 'keep-alive'}
7+
8+
9+
// now we will send data at regular interval
10+
11+
let intervalId = setInterval(() => {
12+
res.send("updated data sent every 5 seconds");
13+
}, 5000);
14+
15+
// frontend se close call hone pr ye call hoga
16+
req.on("close", () => {
17+
clearInterval(intervalId);
18+
});
19+
20+
21+
});

S/server-sent-events/sse_frontend.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let source = new EventSource("https://sse.dev/test?interval=1");
2+
3+
4+
source.onmessage = ( message)=>{
5+
6+
console.log(message.data);
7+
8+
}
9+
10+
// this will close the server connection
11+
setTimeout(() => {
12+
source.close();
13+
}, 10000);

S/settimeout/settimeout.js

+5
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ console.log(...args);
55

66

77
setTimeout(callback, 5000, "new");
8+
9+
10+
setTimeout(() => {
11+
console.log("Hello");
12+
}, Infinity);

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<div id="demo"></div>
1313

14-
<script src="./A/array/array.js"> </script>
14+
<script src="./D/design_patterns/behavioural/observer/observer.js"> </script>
1515
</body>
1616

1717
</html>

0 commit comments

Comments
 (0)