欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用C++编写的单例模式Singleton

程序员文章站 2022-03-07 18:06:06
...

单例模式Singleton代码如下:

/*
*/
#ifdef Implementation1
/*
*/
class Singleton {
public:
    static Singleton* Instance();
protected:
    Singleton();
private:
    static Singleton* _instance;
};
/*
*/
Singleton* Singleton::_instance = 0;

Singleton* Singleton::Instance () {
    if (_instance == 0) {
        _instance = new Singleton;
    }
    return _instance;
}
/*
*/
#endif
/*
*/
#ifdef Implementation2
#include "List.H"
#include "stdlib.h"
class NameSingletonPair;

/*
*/
class Singleton {
public:
    static void Register(char* name, Singleton*);
    static Singleton* Instance();
protected:
    static Singleton* Lookup(const char* name);
private:
    static Singleton* _instance;
    static List<NameSingletonPair>* _registry;
};
/*
*/
Singleton* Singleton::Instance () {
    if (_instance == 0) {
        const char* singletonName = getenv("SINGLETON");
        // user or environment supplies this at startup

        _instance = Lookup(singletonName);
        // Lookup returns 0 if there's no such singleton
    }
    return _instance;
}
/*
*/
class MySingleton : public Singleton {
public:
    MySingleton();
};
/*
*/
MySingleton::MySingleton() {
    // ...
    Singleton::Register("MySingleton", this);
}
/*
*/
static MySingleton theSingleton;
/*
*/
#endif
/*
*/
#ifdef Singleton
/*
*/
class MazeFactory {
public:
    static MazeFactory* Instance();

    // existing interface goes here
protected:
    MazeFactory();
private:
    static MazeFactory* _instance;
};
/*
*/
MazeFactory* MazeFactory::_instance = 0;

MazeFactory* MazeFactory::Instance () {
    if (_instance == 0) {
        _instance = new MazeFactory;
    }
    return _instance;
}
/*
*/
#else
//MazeFactory* MazeFactory::_instance = 0;
#include "C++/MazeFactories.H"
#include "stdlib.h"
#include "strings.h"
/*
*/
MazeFactory* MazeFactory::Instance () {
    if (_instance == 0) {
        const char* mazeStyle = getenv("MAZESTYLE");
/*
*/
        if (strcmp(mazeStyle, "bombed") == 0) {
            _instance = new BombedMazeFactory;
/*
*/
        } else if (strcmp(mazeStyle, "enchanted") == 0) {
            _instance = new EnchantedMazeFactory;
/*
*/
        // ... other possible subclasses
/*
*/
        } else {        // default
            _instance = new MazeFactory;
        }
    }
    return _instance;
}
/*
*/
#endif
/*
*/


相关标签: singleton