fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. /// singleton class
  6. class ConfigurationManager {
  7. private:
  8. string configuration_path;
  9. vector<string> servers_ips;
  10. string aws_service_url;
  11. bool is_loaded = false;
  12. static ConfigurationManager* obj;
  13.  
  14. ConfigurationManager() {}
  15.  
  16. void load() {
  17. if (is_loaded) return;
  18. cout << "Lazy Loading\n";
  19. servers_ips.push_back("10.20.30.40");
  20. servers_ips.push_back("10.20.30.41");
  21. servers_ips.push_back("10.20.30.42");
  22. aws_service_url = "https://d...content-available-to-author-only...s.com";
  23. is_loaded = true;
  24. }
  25.  
  26. public:
  27. ConfigurationManager(const ConfigurationManager& other) = delete;
  28. ConfigurationManager& operator=(const ConfigurationManager& other) = delete;
  29.  
  30. static ConfigurationManager* GetInstance() {
  31. if (!obj) {
  32. obj = new ConfigurationManager();
  33. }
  34. return obj;
  35. }
  36.  
  37. string GetAwsServiceUrl() {
  38. load();
  39. return aws_service_url;
  40. }
  41. };
  42.  
  43. // Initialize static member
  44. ConfigurationManager* ConfigurationManager::obj = nullptr;
  45.  
  46. void f1() {
  47. ConfigurationManager* mgr = ConfigurationManager::GetInstance();
  48. cout << "Instance address in f1: " << mgr << '\n';
  49. cout << mgr->GetAwsServiceUrl() << '\n';
  50. cout <<"-------------------------------------------\n";
  51. }
  52.  
  53. void f2() {
  54. ConfigurationManager* mgr = ConfigurationManager::GetInstance();
  55. cout << "Instance address in f1: " << mgr << '\n';
  56. cout << mgr->GetAwsServiceUrl() << '\n';
  57. cout <<"-------------------------------------------\n";
  58. }
  59.  
  60. int main() {
  61. f1();
  62. f2();
  63. return 0;
  64. }
  65.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Instance address in f1: 0x55cd91f42e70
Lazy Loading
https://d...content-available-to-author-only...s.com
-------------------------------------------
Instance address in f1: 0x55cd91f42e70
https://d...content-available-to-author-only...s.com
-------------------------------------------