Skip to content

Commit 1beb831

Browse files
committed
Update for singleton
1 parent fb694bb commit 1beb831

1 file changed

Lines changed: 27 additions & 46 deletions

File tree

src/dp/creational/Singleton.cpp

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,59 @@
11
// cppcheck-suppress-file [functionStatic]
22

3-
// Singleton is a creational design pattern that lets you ensure that a class
4-
// has only one instance, while providing a global access point to this
5-
// instance. Appicability:
6-
// (*) when a class in your program should have just a single instance
7-
// available to all clients; for example, a single database object shared by
8-
// different parts of the program.
9-
// (**) when you need stricter control over global variables.
10-
11-
// UML: docs/uml/patterns_creational_singleton.drawio.svg
12-
133
#include <string>
4+
#include "ExampleRegistry.h"
145
#include "Logger.h"
156

167
namespace {
178
namespace singleton_pattern {
189

19-
/**
20-
* The Singleton class defines the `GetInstance` method that serves as an
21-
* alternative to constructor and lets clients access the same instance of this
22-
* class over and over.
23-
*/
24-
class Singleton {
25-
private:
26-
static inline Singleton* instance_ = nullptr;
27-
static inline int id_ = 0;
28-
int dummy_{};
29-
/**
30-
* The Singleton's constructor should always be private to prevent direct
31-
* construction calls with the `new` operator.
32-
*/
33-
Singleton() = default;
10+
/// @class Singleton class
11+
/// @brief defines the `GetInstance` method that serves as an alternative to constructor
12+
class SingletonConfig {
3413

3514
public:
3615
// 1. Should not be cloneable.
37-
Singleton(const Singleton& other) = delete;
16+
SingletonConfig(const SingletonConfig& other) = delete;
3817

3918
// 2. Should not be assignable
40-
Singleton& operator=(const Singleton& other) = delete;
19+
SingletonConfig& operator=(const SingletonConfig& other) = delete;
4120

42-
static Singleton* get_instance() {
43-
if (instance_ == nullptr) {
44-
instance_ = new Singleton();
45-
id_++;
46-
}
47-
LOG("id: " + std::to_string(id_));
48-
return instance_;
21+
static SingletonConfig& get_instance() {
22+
static SingletonConfig instance;
23+
return instance;
4924
}
5025

51-
void operation() {
52-
LOG("id: " + std::to_string(id_));
53-
dummy_++;
26+
void init(const std::string& input) {
27+
LOG(input);
28+
value_ = input;
5429
}
30+
31+
const std::string& get_value() const {
32+
LOG("");
33+
return value_;
34+
};
35+
36+
private:
37+
/// @brief Default constructor should always be private
38+
SingletonConfig() = default;
39+
std::string value_;
5540
};
5641

5742
void run() {
58-
auto client_code = [](Singleton* s) {
59-
s->operation();
43+
auto client_code = []() {
44+
LOG(SingletonConfig::get_instance().get_value());
6045
};
6146

62-
Singleton* s1 = Singleton::get_instance();
63-
client_code(s1);
64-
65-
Singleton* s2 = Singleton::get_instance();
66-
client_code(s2);
47+
SingletonConfig& s1 = SingletonConfig::get_instance();
48+
s1.init("0x001");
49+
client_code();
6750

6851
// Singleton* s3 = new Singleton(); // ERROR
6952
}
7053

7154
} // namespace singleton_pattern
7255
} // namespace
7356

74-
#include "ExampleRegistry.h"
75-
7657
class SingletonExample : public IExample {
7758
public:
7859
std::string group() const override { return "dp/creational"; }

0 commit comments

Comments
 (0)