This repository has been archived on 2022-11-09. You can view files and clone it, but cannot push or open issues or pull requests.
4 changed files with
71 additions and
0 deletions
|
|
|
@ -0,0 +1 @@
|
|
|
|
|
Паттерн "Одиночка" гарантирует, что класс имеет только один экземпляр, и предоставляет глобальную точку доступа к этому экземпляру.
|
|
|
|
@ -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;
|
|
|
|
|
}
|
|
|
|
@ -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();
|
|
|
|
|
};
|
|
|
|
@ -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;
|
|
|
|
|
}
|