Skip to content

Commit 0588381

Browse files
Types of design patterns
1 parent a2de9c6 commit 0588381

7 files changed

+303
-0
lines changed

factory-pattern.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// factory pattern is a creational design pattern that uses factory methods to create objects
2+
// factory functions simply creates an object and returns it.
3+
4+
class Devloper {
5+
name = "";
6+
type = "Devloper"
7+
8+
constructor(name) {
9+
this.name = name;
10+
}
11+
12+
say() {
13+
console.log(`Hi i'm ${this.name} & i'm ${this.type}`)
14+
}
15+
16+
}
17+
18+
class Tester {
19+
name = "";
20+
type = "Tester"
21+
22+
constructor(name) {
23+
this.name = name;
24+
}
25+
26+
say() {
27+
console.log(`Hi i'm ${this.name} & i'm ${this.type}`)
28+
}
29+
30+
}
31+
32+
class Analyst {
33+
name = "";
34+
type = "Analyst"
35+
36+
constructor(name) {
37+
this.name = name;
38+
}
39+
40+
say() {
41+
console.log(`Hi i'm ${this.name} & i'm ${this.type}`)
42+
}
43+
44+
}
45+
46+
class EmployeeFactory {
47+
create(name, type) {
48+
switch (type) {
49+
case 1:
50+
return new Devloper(name);
51+
case 2:
52+
return new Tester(name);
53+
case 3:
54+
return new Analyst(name);
55+
default:
56+
break;
57+
}
58+
}
59+
}
60+
61+
const employeeFactory = new EmployeeFactory();
62+
const employees = [];
63+
employees.push(employeeFactory.create("Ajith", 1));
64+
employees.push(employeeFactory.create("Vijay", 2));
65+
66+
console.log(employees)

index.html

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Design Patterns</title>
8+
</head>
9+
10+
<body>
11+
12+
</body>
13+
<!-- creational Patterns-->
14+
<!-- creational design patterns are design patterns that deal with object creation mechanisms -->
15+
<!-- factory pattern -->
16+
<!-- Singleton Pattern -->
17+
<!-- Prototype Pattern -->
18+
<!-- Builder Pattern -->
19+
20+
<!-- structural Patterns-->
21+
<!-- Structural Design Patterns are Design Patterns that ease the design by identifying a simple way to realize relationships between entities -->
22+
<!-- Decorator Pattern -->
23+
<!-- Proxy Pattern -->
24+
25+
26+
<!-- behaviors Patterns-->
27+
<!-- behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns -->
28+
<!-- Strategy Pattern -->
29+
<!-- Observer Pattern -->
30+
31+
<!-- <script src="/factory-pattern.js"></script> -->
32+
<!-- <script src="/singleton-pattern.js"></script> -->
33+
<!-- <script src="/strategy-pattern.js"></script> -->
34+
<!-- <script src="/observer-pattern.js"></script> -->
35+
<!-- <script src="/module-pattern.js"></script> -->
36+
<script src="/prototype-pattern.js"></script>
37+
38+
</html>

module-pattern.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* The Module Pattern is a design pattern in JavaScript that
2+
provides a way to encapsulate and organize code by creating self-contained modules */
3+
4+
const calculateWeight = (function () {
5+
const POUND = 0.220462;
6+
const TONS = 0.000110231
7+
const METRIC_TONS = 0.0001
8+
let userWeight = 0
9+
10+
function convert(weight, type) {
11+
userWeight = weight;
12+
switch (type) {
13+
case 1:
14+
return weight * POUND
15+
case 2:
16+
return weight * TONS
17+
case 3:
18+
return weight * METRIC_TONS
19+
default:
20+
return weight;
21+
22+
}
23+
}
24+
25+
return {
26+
weight: userWeight,
27+
converWeight: function (weight, type) {
28+
return convert(weight, type)
29+
}
30+
}
31+
32+
})();
33+
34+
console.log(calculateWeight.weight)
35+
console.log(calculateWeight.userWeight)
36+
console.log(calculateWeight.converWeight(10, 1))

observer-pattern.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* The Observer Pattern is a behavioral design pattern that defines a dependency between objects so that when one object
2+
(the subject) changes state, all its dependents (observers) are notified and updated automatically */
3+
4+
class SMSNotification {
5+
from = ""
6+
#to = "+919000000000"
7+
8+
constructor(from) {
9+
this.from = from;
10+
}
11+
12+
send(msg) {
13+
console.log(msg, this.#to, this.from)
14+
console.log(`SMS Notification send successfully`)
15+
}
16+
}
17+
18+
class EmailNotification {
19+
from = ""
20+
21+
22+
constructor(from) {
23+
this.from = from;
24+
}
25+
26+
send(msg) {
27+
console.log(msg, this.#to, this.from)
28+
console.log(`Email Notification send successfully`)
29+
}
30+
}
31+
32+
class PushNotification {
33+
#publishers = []
34+
35+
addPublisher(publisher) {
36+
this.#publishers.push(publisher);
37+
}
38+
39+
removePublisher(publisher) {
40+
this.#publishers = this.#publishers.filter((e) => e != publisher);
41+
}
42+
43+
sendNotification(msg) {
44+
this.#publishers.forEach((publisher) => {
45+
publisher.send(msg);
46+
})
47+
}
48+
}
49+
50+
const pushNotification = new PushNotification();
51+
const sms = new SMSNotification("+9191111111111");
52+
const email = new EmailNotification("[email protected]")
53+
pushNotification.addPublisher(sms);
54+
pushNotification.addPublisher(email);
55+
56+
// pushNotification.removePublisher(email);
57+
58+
pushNotification.sendNotification("haiii test")

prototype-pattern.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* The Prototype Pattern is a creational design pattern that allows you to create objects based on a prototype or template object.
2+
It's used to create new instances by copying the properties and methods from an existing object */
3+
4+
function Developer(name) {
5+
this.name = name;
6+
this.role = "Devloper";
7+
8+
this.getRole = function () {
9+
return this.role
10+
}
11+
}
12+
13+
Developer.prototype.employeeInfo = function () {
14+
return {
15+
name: this.name,
16+
role: this.role
17+
}
18+
}
19+
20+
function Tester(name) {
21+
this.name = name;
22+
this.role = "Tester";
23+
24+
Developer.call(this, name);
25+
26+
}
27+
28+
Tester.prototype = Object.create(Developer.prototype);
29+
Tester.prototype.constructor = Tester;
30+
31+
32+
33+
const developer = new Developer("Dhanush");
34+
const tester = new Tester("Ajith");
35+
36+
console.log(developer.getRole())
37+
console.log(developer.employeeInfo())
38+
39+
console.log(tester.getRole())
40+
console.log(tester.employeeInfo())
41+

singleton-pattern.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// It has only one instance and provides a global point of access to that instance.
2+
3+
class Singleton {
4+
static instance;
5+
data = [];
6+
7+
static getInstance() {
8+
if (!Singleton.instance) Singleton.instance = new Singleton();
9+
return Singleton.instance;
10+
}
11+
12+
addData(data) {
13+
this.data.push(data);
14+
}
15+
16+
getData(data) {
17+
return this.data;
18+
}
19+
}
20+
21+
const singleton1=Singleton.getInstance().addData("Dhanush");
22+
const singleton2=Singleton.getInstance().addData("Ajith");
23+
24+
console.log(Singleton.getInstance().getData())
25+
26+
console.log(singleton1 === singleton2)

strategy-pattern.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* The Strategy pattern is a behavioral design pattern that enables you to define a group (or family) of closely-related algorithms (known as strategies).
2+
The strategy pattern allows you to swap strategies in and out for each other as needed at runtime. */
3+
4+
class Flipkart {
5+
cost = 100;
6+
shippingCost(amount) {
7+
return amount * this.cost;
8+
}
9+
}
10+
11+
class Amazon {
12+
cost = 250;
13+
shippingCost(amount) {
14+
return amount * this.cost;
15+
}
16+
}
17+
18+
class Meesho {
19+
cost = 30;
20+
shippingCost(amount) {
21+
return amount * this.cost;
22+
}
23+
}
24+
25+
class Shipping {
26+
shippingCompany = ''
27+
28+
constructor(company) {
29+
this.shippingCompany = company ? company : null;
30+
}
31+
32+
calculate(amount) {
33+
return this.shippingCompany ? this.shippingCompany.shippingCost(amount) : 0;
34+
}
35+
}
36+
37+
const flipkartShipping = new Shipping(new Flipkart);
38+
console.log(flipkartShipping.calculate(10))

0 commit comments

Comments
 (0)