Add subdirectory for practicing design patterns in C++

+ Add example for singleton
This commit is contained in:
Shaun Reed 2021-05-11 11:55:21 -04:00
parent 8646cd51ea
commit 248e48d5c9
6 changed files with 94 additions and 0 deletions

View File

@ -20,4 +20,5 @@ project(
add_subdirectory(algorithms) add_subdirectory(algorithms)
add_subdirectory(cmake) add_subdirectory(cmake)
add_subdirectory(datastructs) add_subdirectory(datastructs)
add_subdirectory(patterns)
add_subdirectory(sdl-cmake) add_subdirectory(sdl-cmake)

View File

@ -0,0 +1,18 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: A project for practicing C++ design patterns ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
#
cmake_minimum_required(VERSION 3.15)
project(
#[[NAME]] DesignPatterns
VERSION 1.0
DESCRIPTION "A project for practicing various design patterns in C++"
LANGUAGES CXX
)
add_subdirectory(singleton)

View File

@ -0,0 +1,20 @@
###############################################################################
## Author: Shaun Reed ##
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
## About: A project for practicing the singleton C++ design pattern ##
## ##
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
##############################################################################
#
cmake_minimum_required(VERSION 3.15)
project(
#[[NAME]] Singleton
VERSION 1.0
DESCRIPTION "An example of the singleton design pattern in C++"
LANGUAGES CXX
)
add_library(singleton "singleton.cpp")
add_executable(singleton-test "main.cpp")
target_link_libraries(singleton-test singleton)

View File

@ -0,0 +1,15 @@
#include "singleton.h"
int main(const int argc, const char *argv[])
{
// 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();
}

View File

@ -0,0 +1,15 @@
#include "singleton.h"
Singleton::~Singleton()
{
// Delete any allocated data, close any opened files, etc..
}
Singleton &Singleton::getInstance()
{
// Construct a new singleton if it doesnt exist, return it
// + If a static Singleton exists already, just return it
static Singleton l;
return l;
}

View File

@ -0,0 +1,25 @@
#ifndef SINGLETON_H
#define SINGLETON_H
#include <iostream>
#include <string>
class Singleton {
public:
static Singleton &getInstance();
~Singleton();
inline void showMessage() const { std::cout << message;}
inline void updateMessage(const std::string &m) { message = m;}
private:
std::string message;
// Don't allow copying of this class
Singleton() { message = "New singleton\n";}
Singleton(const Singleton &) {}
Singleton &operator=(const Singleton &) {}
};
#endif // SINGLETON_H