-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
singleton.cpp
58 lines (46 loc) · 1.25 KB
/
singleton.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
#include <iostream>
using namespace std;
// In software engineering, the Application pattern is a software design pattern
// that restricts the instantiation of a class to one "single" instance.
// This is useful when exactly one object is needed to coordinate actions across the system.
// https://refactoring.guru/ru/design-patterns/Singleton
// Another Singleton LOGGER EXAMPLE: https://git.io/vo9CN
class Application {
static Application* instance;
int window_width;
int window_height;
string localization;
// private:
Application() {
window_width = 800;
window_height = 600;
localization = "uk";
}
public:
static Application* getInstance() {
if (instance == nullptr)
instance = new Application;
return instance;
}
int getWidth() const {
return window_width;
}
int getHeight() const {
return window_height;
}
void setWidth(int width) {
window_width = width;
}
void setHeight(int height) {
window_height = height;
}
};
Application* Application::instance = nullptr;
int main() {
Application* app = Application::getInstance();
// Application object; // ERROR
// Application* dynamic_object = new Application; // ERROR
cout << app->getWidth() << "\n"; // 800
app->setWidth(1024);
cout << app->getWidth() << "\n"; // 1024
}