mirror of
https://github.com/JakubVojvoda/design-patterns-cpp.git
synced 2025-12-17 04:44:36 +03:00
code refactoring: fix memory leaks, code style, etc.
This commit is contained in:
@@ -15,16 +15,28 @@
|
||||
* has private static variable to hold one instance of the class
|
||||
* and method which gives us a way to instantiate the class
|
||||
*/
|
||||
class Singleton {
|
||||
class Singleton
|
||||
{
|
||||
public:
|
||||
static Singleton *get() {
|
||||
if (instance == NULL) {
|
||||
static Singleton* get()
|
||||
{
|
||||
if ( !instance )
|
||||
{
|
||||
instance = new Singleton();
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
void tell() {
|
||||
|
||||
static void restart()
|
||||
{
|
||||
if ( instance )
|
||||
{
|
||||
delete instance;
|
||||
}
|
||||
}
|
||||
|
||||
void tell()
|
||||
{
|
||||
std::cout << "This is Singleton." << std::endl;
|
||||
// ...
|
||||
}
|
||||
@@ -42,5 +54,7 @@ Singleton* Singleton::instance = nullptr;
|
||||
int main()
|
||||
{
|
||||
Singleton::get()->tell();
|
||||
Singleton::restart();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user