Advertisement
Kiosani

Ejemplo Singleton

Jul 6th, 2021
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. /**
  2. * The Singleton class defines the `GetInstance` method that serves as an
  3. * alternative to constructor and lets clients access the same instance of this
  4. * class over and over.
  5. */
  6. class Singleton
  7. {
  8.  
  9. /**
  10. * The Singleton's constructor should always be private to prevent direct
  11. * construction calls with the `new` operator.
  12. */
  13.  
  14. protected:
  15. Singleton(const std::string value): value_(value)
  16. {
  17. }
  18.  
  19. static Singleton* singleton_;
  20.  
  21. std::string value_;
  22.  
  23. public:
  24.  
  25. /**
  26. * Singletons should not be cloneable.
  27. */
  28. Singleton(Singleton &other) = delete;
  29. /**
  30. * Singletons should not be assignable.
  31. */
  32. void operator=(const Singleton &) = delete;
  33. /**
  34. * This is the static method that controls the access to the singleton
  35. * instance. On the first run, it creates a singleton object and places it
  36. * into the static field. On subsequent runs, it returns the client existing
  37. * object stored in the static field.
  38. */
  39.  
  40. static Singleton *GetInstance(const std::string& value);
  41. /**
  42. * Finally, any singleton should define some business logic, which can be
  43. * executed on its instance.
  44. */
  45. void SomeBusinessLogic()
  46. {
  47. // ...
  48. }
  49.  
  50. std::string value() const{
  51. return value_;
  52. }
  53. };
  54.  
  55. Singleton* Singleton::singleton_= nullptr;;
  56.  
  57. /**
  58. * Static methods should be defined outside the class.
  59. */
  60. Singleton *Singleton::GetInstance(const std::string& value)
  61. {
  62. /**
  63. * This is a safer way to create an instance. instance = new Singleton is
  64. * dangeruous in case two instance threads wants to access at the same time
  65. */
  66. if(singleton_==nullptr){
  67. singleton_ = new Singleton(value);
  68. }
  69. return singleton_;
  70. }
  71.  
  72. void ThreadFoo(){
  73. // Following code emulates slow initialization.
  74. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  75. Singleton* singleton = Singleton::GetInstance("FOO");
  76. std::cout << singleton->value() << "\n";
  77. }
  78.  
  79. void ThreadBar(){
  80. // Following code emulates slow initialization.
  81. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  82. Singleton* singleton = Singleton::GetInstance("BAR");
  83. std::cout << singleton->value() << "\n";
  84. }
  85.  
  86.  
  87. int main()
  88. {
  89. std::cout <<"If you see the same value, then singleton was reused (yay!\n" <<
  90. "If you see different values, then 2 singletons were created (booo!!)\n\n" <<
  91. "RESULT:\n";
  92. std::thread t1(ThreadFoo);
  93. std::thread t2(ThreadBar);
  94. t1.join();
  95. t2.join();
  96.  
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement