From 248e48d5c9312b1378d810b6942a1eb32a7c0bcd Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Tue, 11 May 2021 11:55:21 -0400 Subject: [PATCH] Add subdirectory for practicing design patterns in C++ + Add example for singleton --- cpp/CMakeLists.txt | 1 + cpp/patterns/CMakeLists.txt | 18 ++++++++++++++++++ cpp/patterns/singleton/CMakeLists.txt | 20 ++++++++++++++++++++ cpp/patterns/singleton/main.cpp | 15 +++++++++++++++ cpp/patterns/singleton/singleton.cpp | 15 +++++++++++++++ cpp/patterns/singleton/singleton.h | 25 +++++++++++++++++++++++++ 6 files changed, 94 insertions(+) create mode 100644 cpp/patterns/CMakeLists.txt create mode 100644 cpp/patterns/singleton/CMakeLists.txt create mode 100644 cpp/patterns/singleton/main.cpp create mode 100644 cpp/patterns/singleton/singleton.cpp create mode 100644 cpp/patterns/singleton/singleton.h diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index e65379d..3f8aada 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -20,4 +20,5 @@ project( add_subdirectory(algorithms) add_subdirectory(cmake) add_subdirectory(datastructs) +add_subdirectory(patterns) add_subdirectory(sdl-cmake) diff --git a/cpp/patterns/CMakeLists.txt b/cpp/patterns/CMakeLists.txt new file mode 100644 index 0000000..f0d98e8 --- /dev/null +++ b/cpp/patterns/CMakeLists.txt @@ -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) diff --git a/cpp/patterns/singleton/CMakeLists.txt b/cpp/patterns/singleton/CMakeLists.txt new file mode 100644 index 0000000..2807f0c --- /dev/null +++ b/cpp/patterns/singleton/CMakeLists.txt @@ -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) diff --git a/cpp/patterns/singleton/main.cpp b/cpp/patterns/singleton/main.cpp new file mode 100644 index 0000000..51d273b --- /dev/null +++ b/cpp/patterns/singleton/main.cpp @@ -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(); +} diff --git a/cpp/patterns/singleton/singleton.cpp b/cpp/patterns/singleton/singleton.cpp new file mode 100644 index 0000000..4bdad2b --- /dev/null +++ b/cpp/patterns/singleton/singleton.cpp @@ -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; +} diff --git a/cpp/patterns/singleton/singleton.h b/cpp/patterns/singleton/singleton.h new file mode 100644 index 0000000..383febc --- /dev/null +++ b/cpp/patterns/singleton/singleton.h @@ -0,0 +1,25 @@ + +#ifndef SINGLETON_H +#define SINGLETON_H + +#include +#include + +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