-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day-120.cpp
59 lines (44 loc) · 1.32 KB
/
Day-120.cpp
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
//
// Created by Amit Kumar on 12/07/23.
//
#include "iostream"
#include "string"
using namespace std;
enum State {
NotInitialized, Odd, Even
};
class ModifiedSingleton {
string instanceName;
ModifiedSingleton() = default;
explicit ModifiedSingleton(string name) : instanceName(std::move(name)) {}
public:
ModifiedSingleton(ModifiedSingleton &ms) = delete;
static ModifiedSingleton &getInstance() {
static ModifiedSingleton instanceOne;
static ModifiedSingleton instanceTwo;
static State currentState = State::NotInitialized;
if (currentState == State::NotInitialized) {
currentState = State::Odd;
instanceOne = ModifiedSingleton("First Instance");
instanceTwo = ModifiedSingleton("Second Instance");
}
if (currentState == State::Odd) {
currentState = State::Even;
return instanceTwo;
} else {
currentState = State::Odd;
return instanceOne;
}
}
friend ostream &operator<<(ostream &out, const ModifiedSingleton &ms) {
out << ms.instanceName;
return out;
}
};
int main() {
for (int i = 1; i < 10; ++i) {
cout << ((i&1) ? "Odd Call: " : "Even Call: ");
cout << ModifiedSingleton::getInstance() << endl;
}
return 0;
}