Add example for classic singleton

This commit is contained in:
Shaun Reed 2021-05-11 12:21:03 -04:00
parent 248e48d5c9
commit 9bb2f9867d
6 changed files with 65 additions and 3 deletions

View File

@ -16,5 +16,6 @@ project(
) )
add_library(singleton "singleton.cpp") add_library(singleton "singleton.cpp")
add_library(singleton-pointer "singleton-pointer.cpp")
add_executable(singleton-test "main.cpp") add_executable(singleton-test "main.cpp")
target_link_libraries(singleton-test singleton) target_link_libraries(singleton-test singleton singleton-pointer)

View File

@ -1,8 +1,13 @@
#include "singleton.h" #include "singleton.hpp"
#include "singleton-pointer.hpp"
int main(const int argc, const char *argv[]) int main(const int argc, const char *argv[])
{ {
/****************************************************************************/
// Meyers Singleton - static getInstance
// Creates a singleton, initializes message in ctor // Creates a singleton, initializes message in ctor
Singleton &s = Singleton::getInstance(); Singleton &s = Singleton::getInstance();
s.showMessage(); s.showMessage();
@ -12,4 +17,20 @@ int main(const int argc, const char *argv[])
// Update already existing Singleton message, show it // Update already existing Singleton message, show it
Singleton::getInstance().updateMessage("Second update\n"); Singleton::getInstance().updateMessage("Second update\n");
s.showMessage(); 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();
} }

View File

@ -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;
}

View File

@ -0,0 +1,29 @@
#ifndef SINGLETON_POINTER_HPP
#define SINGLETON_POINTER_HPP
#include <iostream>
#include <string>
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

View File

@ -1,5 +1,5 @@
#include "singleton.h" #include "singleton.hpp"
Singleton::~Singleton() Singleton::~Singleton()
{ {