This commit is contained in:
Alexander Zhirov 2021-11-04 01:35:16 +03:00
parent 3b15979dfe
commit 1807704940
4 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1 @@
Паттерн "Одиночка" гарантирует, что класс имеет только один экземпляр, и предоставляет глобальную точку доступа к этому экземпляру.

32
lesson_5/Singleton.cpp Normal file
View File

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

22
lesson_5/Singleton.hpp Normal file
View File

@ -0,0 +1,22 @@
/*
* Singleton.hpp
*
* Created on: 4 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include <string>
class Singleton
{
private:
static Singleton *_instance;
protected:
Singleton();
~Singleton();
public:
static Singleton* Instance();
std::string getDiscription();
};

16
lesson_5/main.cpp Normal file
View File

@ -0,0 +1,16 @@
/*
* main.cpp
*
* Created on: 4 нояб. 2021 г.
* Author: alexander
*/
#include "Singleton.hpp"
#include <iostream>
int main()
{
std::cout << Singleton::Instance()->getDiscription() << std::endl;
return 0;
}