-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmessage.js
More file actions
81 lines (57 loc) · 2.09 KB
/
message.js
File metadata and controls
81 lines (57 loc) · 2.09 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
// Task: Implement a newsletter system using observer pattern.
// The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
// Dont suggest code
// Do nothing
// Tasks: Implement 3 classes:
// 1. Subscriber Class - Properties (name, emailadress,age)
// 2. Message class (content, min-age(optional))
// 3. NewsletterSystem class (sendEmail) - Handle the newsletter system using observer pattern
class Subscriber{
constructor(name,emailadress,age){
this.name=name
this.age=age
this.emailadress=emailadress
}
}
class Message{
constructor(content,minAge=null){
this.content=content
this.minAge=minAge
}
}
class NewsletterSystem{
constructor(sendEmail){
this.sendEmail=sendEmail
this.subscribers=[]
}
// Create an instanc of message class
sendNewsLetter(messageInstance){
// Iterate through the subscriber class and call the send email function for every subscriber
const sub=this.subscribers;
for(let i=0; i<=sub.length;i++){
if(subscribers[i].age>=messageInstance.minAge){
sendEmail(subscribersi.emailadress,messageInstance.content);
}
}
}
subscribe(subscriber){
if(subscriber.age>=13 && !this.subscribers.includes(subscriber)){
// Add a subscriber
this.subscribers.push(subscriber)
}
}
unsubscribe(subscriber){
this.subscribers.pop(subscriber)
}
}
function sendEmail(emailadress,messageContent){
this.subscribers.forEach((subscriber)=>{
let mes='Hello' + subscriber.name + messageContent + emailadress
})
}
// Instantiate
const subscriber1=new Subscriber('John',10, 'john@gmail.com')
const Message1=new Message('Hello World',13)
const newsletterSystem=new NewsletterSystem(sendEmail)
newsletterSystem.subscribe(subscriber1)
newsletterSystem.sendNewsLetter(Message1)