-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiton.cpp
More file actions
101 lines (85 loc) · 2.88 KB
/
Multiton.cpp
File metadata and controls
101 lines (85 loc) · 2.88 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
/**
* @cite Multiton Pattern is a variation of Singleton Pattern where multiple singleton objects of a class are created that are uniquely identifiable.
*
* @brief Multiton Pattern can be exemplified by an EventLogger class that stores Events in different files based on their severity i.e. High, Medium, Low etc.
*/
#include <iostream>
#include <map>
#include <string>
/**
* @brief Stores the Severity of Events on the basis of which Event Logger Singleton Objects are created.
*/
enum EventSeverity
{
Critical,
Warning,
Normal
};
/**
* @brief Multiple Singleton Objects of EventLogger are created.
*/
class EventLogger
{
static int id;
// Contructor is private so that only friend class EventLoggerMultiton can create the objects of Event Logger.
EventLogger() {
std::cout << ++id << " Loggers created so far...\n";
}
friend class EventLoggerMultiton;
// Stores event to a EventLogger.
void add_event(std::string event)
{
std::cout << "Event \"" << event << "\" Logged ";
}
};
int EventLogger::id = 0;
/**
* @brief Stores Multiple Singleton Objects of EventLogger based on the EventSeverity.
*/
class EventLoggerMultiton
{
// Stores the Singleton Objects of EventLogger.
static std::map<EventSeverity, EventLogger *> loggers;
public:
// Adds event to appropriate EventLogger Singleton Object.
static void add_event(std::string event_desc, EventSeverity severity = EventSeverity::Normal)
{
if (loggers.find(severity) == loggers.end())
{
loggers[severity] = new EventLogger();
}
loggers[severity]->add_event(event_desc);
std::cout << " to " << get_severity(severity) << " Logger.\n";
}
// Converts EventSeverity enum values to strings.
static std::string get_severity(EventSeverity severity) {
std::string severity_str = "";
switch (severity)
{
case EventSeverity::Critical:
severity_str = "Critical";
break;
case EventSeverity::Warning:
severity_str = "Warning";
break;
case EventSeverity::Normal:
severity_str = "Normal";
break;
default:
break;
}
return severity_str;
}
};
// Initialization of static logers' list
std::map<EventSeverity, EventLogger *> EventLoggerMultiton::loggers;
int main()
{
// Replicates the Event Logging in Real-World Systems.
EventLoggerMultiton::add_event("System Booted", EventSeverity::Normal);
EventLoggerMultiton::add_event("Data Copy couldn't complete", EventSeverity::Warning);
EventLoggerMultiton::add_event("USB device not recognized", EventSeverity::Warning);
EventLoggerMultiton::add_event("System not responding", EventSeverity::Critical);
EventLoggerMultiton::add_event("System Crashed", EventSeverity::Critical);
return 0;
}