From 1807704940e1d365b7aaf6bb5c9e76fc64a0a9c6 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Thu, 4 Nov 2021 01:35:16 +0300 Subject: [PATCH] lesson_4 --- README.md | 1 + lesson_5/Singleton.cpp | 32 ++++++++++++++++++++++++++++++++ lesson_5/Singleton.hpp | 22 ++++++++++++++++++++++ lesson_5/main.cpp | 16 ++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 lesson_5/Singleton.cpp create mode 100644 lesson_5/Singleton.hpp create mode 100644 lesson_5/main.cpp diff --git a/README.md b/README.md index e69de29..26a2706 100644 --- a/README.md +++ b/README.md @@ -0,0 +1 @@ +Паттерн "Одиночка" гарантирует, что класс имеет только один экземпляр, и предоставляет глобальную точку доступа к этому экземпляру. \ No newline at end of file diff --git a/lesson_5/Singleton.cpp b/lesson_5/Singleton.cpp new file mode 100644 index 0000000..d64ea3d --- /dev/null +++ b/lesson_5/Singleton.cpp @@ -0,0 +1,32 @@ +/* + * Singleton.cpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#include "Singleton.hpp" + +Singleton *Singleton::_instance = nullptr; + +Singleton::Singleton() {} + +Singleton* Singleton::Instance() +{ + if (_instance) + { + _instance = new Singleton; + } + + return _instance; +} + +std::string Singleton::getDiscription() +{ + return "I'm a statically initialized Singleton!"; +} + +Singleton::~Singleton() +{ + delete _instance; +} diff --git a/lesson_5/Singleton.hpp b/lesson_5/Singleton.hpp new file mode 100644 index 0000000..3479a97 --- /dev/null +++ b/lesson_5/Singleton.hpp @@ -0,0 +1,22 @@ +/* + * Singleton.hpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include + +class Singleton +{ +private: + static Singleton *_instance; +protected: + Singleton(); + ~Singleton(); +public: + static Singleton* Instance(); + std::string getDiscription(); +}; diff --git a/lesson_5/main.cpp b/lesson_5/main.cpp new file mode 100644 index 0000000..b0ffdb0 --- /dev/null +++ b/lesson_5/main.cpp @@ -0,0 +1,16 @@ +/* + * main.cpp + * + * Created on: 4 нояб. 2021 г. + * Author: alexander + */ + +#include "Singleton.hpp" +#include + +int main() +{ + std::cout << Singleton::Instance()->getDiscription() << std::endl; + + return 0; +}