Repository navigation
Expand file tree
/
Copy pathexpno 9.cpp
More file actions
137 lines (128 loc) · 3 KB
/
Copy pathexpno 9.cpp
File metadata and controls
137 lines (128 loc) · 3 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
#include <iostream>
#include <vector>
#include <memory>
#include <string>
using namespace std;
/**
* @brief Observer interface.
*/
class Observer
{
public:
virtual void notify(const string &message) = 0;
virtual ~Observer() {}
};
/**
* @brief Email notification observer.
*/
class EmailNotification : public Observer
{
private:
string email;
public:
EmailNotification(string e)
{
email = e;
}
void notify(const string &message) override
{
cout << "Email sent to " << email
<< ": " << message << endl;
}
};
/**
* @brief SMS notification observer.
*/
class SMSNotification : public Observer
{
private:
string phoneNumber;
public:
SMSNotification(string number)
{
phoneNumber = number;
}
void notify(const string &message) override
{
cout << "SMS sent to " << phoneNumber
<< ": " << message << endl;
}
};
/**
* @brief Factory class for creating notifications.
*/
class NotificationFactory
{
public:
static shared_ptr<Observer> createNotification(
const string &type,
const string &receiver)
{
if (type == "EMAIL")
{
return make_shared<EmailNotification>(receiver);
}
else if (type == "SMS")
{
return make_shared<SMSNotification>(receiver);
}
return nullptr;
}
};
/**
* @brief Singleton Event Manager.
*/
class EventManager
{
private:
vector<shared_ptr<Observer>> observers;
// Private constructor prevents external object creation
EventManager()
{
}
public:
// Delete copy constructor and assignment operator
EventManager(const EventManager &) = delete;
EventManager &operator=(const EventManager &) = delete;
// Singleton access method
static EventManager &getInstance()
{
static EventManager instance;
return instance;
}
void subscribe(shared_ptr<Observer> observer)
{
observers.push_back(observer);
}
void triggerEvent(const string &message)
{
cout << "\nEvent Triggered: "
<< message << endl;
cout << "Sending Notifications...\n";
for (auto observer : observers)
{
observer->notify(message);
}
}
};
int main()
{
// Access Singleton EventManager
EventManager &manager = EventManager::getInstance();
// Create observers using Factory Pattern
shared_ptr<Observer> emailUser =
NotificationFactory::createNotification(
"EMAIL",
"student@gmail.com");
shared_ptr<Observer> smsUser =
NotificationFactory::createNotification(
"SMS",
"9876543210");
// Subscribe observers
manager.subscribe(emailUser);
manager.subscribe(smsUser);
// Trigger event
manager.triggerEvent(
"University examination starts tomorrow!");
return 0;
}