diff --git a/cpp/patterns/singleton/CMakeLists.txt b/cpp/patterns/singleton/CMakeLists.txt index 2807f0c..15a66d2 100644 --- a/cpp/patterns/singleton/CMakeLists.txt +++ b/cpp/patterns/singleton/CMakeLists.txt @@ -16,5 +16,6 @@ project( ) add_library(singleton "singleton.cpp") +add_library(singleton-pointer "singleton-pointer.cpp") add_executable(singleton-test "main.cpp") -target_link_libraries(singleton-test singleton) +target_link_libraries(singleton-test singleton singleton-pointer) diff --git a/cpp/patterns/singleton/main.cpp b/cpp/patterns/singleton/main.cpp index 51d273b..d9fb928 100644 --- a/cpp/patterns/singleton/main.cpp +++ b/cpp/patterns/singleton/main.cpp @@ -1,8 +1,13 @@ -#include "singleton.h" +#include "singleton.hpp" +#include "singleton-pointer.hpp" int main(const int argc, const char *argv[]) { + + /****************************************************************************/ + // Meyers Singleton - static getInstance + // Creates a singleton, initializes message in ctor Singleton &s = Singleton::getInstance(); s.showMessage(); @@ -12,4 +17,20 @@ int main(const int argc, const char *argv[]) // Update already existing Singleton message, show it Singleton::getInstance().updateMessage("Second update\n"); s.showMessage(); + + /****************************************************************************/ + // Classic Singleton - pointer getInstance + + + std::cout << "\n##### ClassicSingleton #####\n\n"; + + // Creates a classic singleton, initializes message in ctor + ClassicSingleton *classicSingleton = ClassicSingleton::getInstance(); + classicSingleton->showMessage(); + // Update already existing ClassicSingleton message, show it + classicSingleton->updateMessage("First update\n"); + ClassicSingleton::getInstance()->showMessage(); + // Update already existing ClassicSingleton message, show it + ClassicSingleton::getInstance()->updateMessage("Second update\n"); + classicSingleton->showMessage(); } diff --git a/cpp/patterns/singleton/singleton-pointer.cpp b/cpp/patterns/singleton/singleton-pointer.cpp new file mode 100644 index 0000000..ca1886f --- /dev/null +++ b/cpp/patterns/singleton/singleton-pointer.cpp @@ -0,0 +1,11 @@ + +#include "singleton-pointer.hpp" + +// Initialize pointer to instance to null; Allocate on first call to getInstance +ClassicSingleton* ClassicSingleton::instance = nullptr; + +ClassicSingleton* ClassicSingleton::getInstance(){ + // If the instance is null, return a new one; Otherwise return instance + if(instance == nullptr) instance = new ClassicSingleton; + return instance; +} diff --git a/cpp/patterns/singleton/singleton-pointer.hpp b/cpp/patterns/singleton/singleton-pointer.hpp new file mode 100644 index 0000000..1bdf682 --- /dev/null +++ b/cpp/patterns/singleton/singleton-pointer.hpp @@ -0,0 +1,29 @@ + +#ifndef SINGLETON_POINTER_HPP +#define SINGLETON_POINTER_HPP + +#include +#include + +class ClassicSingleton{ +public: + // returns pointer to single getInstance + static ClassicSingleton* getInstance(); + + // example functions manipulating singleton object + inline void showMessage() const { std::cout << message;} + void updateMessage(const std::string &m) { message=m;} + +private: + // Do not allow construction of this object; Use getInstance() instead + ClassicSingleton(){ message = "New ClassicSingleton\n";} + // Do not allow copying of this object + ClassicSingleton(const ClassicSingleton&){} + ClassicSingleton& operator=(const ClassicSingleton&){} + + // Static pointer to instance of this singleton + static ClassicSingleton* instance; + std::string message; +}; + +#endif // SINGLETON_POINTER_HPP diff --git a/cpp/patterns/singleton/singleton.cpp b/cpp/patterns/singleton/singleton.cpp index 4bdad2b..f45ef04 100644 --- a/cpp/patterns/singleton/singleton.cpp +++ b/cpp/patterns/singleton/singleton.cpp @@ -1,5 +1,5 @@ -#include "singleton.h" +#include "singleton.hpp" Singleton::~Singleton() { diff --git a/cpp/patterns/singleton/singleton.h b/cpp/patterns/singleton/singleton.hpp similarity index 100% rename from cpp/patterns/singleton/singleton.h rename to cpp/patterns/singleton/singleton.hpp