klips/cpp/patterns/singleton/main.cpp

37 lines
1.3 KiB
C++
Raw Permalink Normal View History

2021-05-11 16:21:03 +00:00
#include "singleton.hpp"
#include "singleton-pointer.hpp"
int main(const int argc, const char *argv[])
{
2021-05-11 16:21:03 +00:00
/****************************************************************************/
// Meyers Singleton - static getInstance
// Creates a singleton, initializes message in ctor
Singleton &s = Singleton::getInstance();
s.showMessage();
// Update already existing Singleton message, show it
s.updateMessage("First update\n");
Singleton::getInstance().showMessage();
// Update already existing Singleton message, show it
Singleton::getInstance().updateMessage("Second update\n");
s.showMessage();
2021-05-11 16:21:03 +00:00
/****************************************************************************/
// 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();
}