Singletonパターンはクラスのインスタンスが一つしかないことを保証するパターンです。
Singletonパターンのメリットは
です。
まずは次のクラス図をご覧ください

このクラス図で重要なのは
- getInstance()メソッド(static Singleton& getInstance())
- privateなコンストラクタ(Singleton)
の二つですこれらによりsingletonを実現します。
つぎにソースを見てみましょう、用意するのは次のファイルです。
- main.cpp
- Singleton.h
- Singleton.cpp
ますはSingleton.hです。
- #ifndef _SINGLETON_H_
- #define _SINGLETON_H_
-
- class Singleton
- {
- public:
-
-
- static Singleton& getInstance();
-
-
- const char*const toString();
-
-
- ~Singleton();
-
- private:
-
- Singleton();
- Singleton(const Singleton& _singleton);
-
-
- const bool init();
-
-
- Singleton& operator=(const Singleton& _singleton);
-
- };
- #endif
#ifndef _SINGLETON_H_
#define _SINGLETON_H_
class Singleton
{
public:
// get instance
static Singleton& getInstance(); // ①
// to string
const char*const toString(); // ②
// destructor
~Singleton(); // ③
private:
// constructor
Singleton(); // ④
Singleton(const Singleton& _singleton); // ⑤
// initialize
const bool init(); // ⑥
// operator(=)
Singleton& operator=(const Singleton& _singleton); // ⑦
};
#endif
①唯一のインスタンスを取得するためのインタフェースです。
②文字列化のインタフェースです。Singletonパターンとは関係有りません。
③デストラクタです。
④⑤インスタンスの生成を禁止するためにコンストラクタをprivateで宣言しています。
⑥初期化メソッドですSingletonパターンとは関係ありません。
⑦operator=をprivateで宣言することによりインスタンスのコピーを禁止しています。Singletonパターンとは関係有りません。
次にSingleton.cppファイルを見てみます。
- #include "Singleton.h"
-
- Singleton& Singleton::getInstance()
- {
- static Singleton singleton;
-
- static bool isAlreadyInit = false;
- if (isAlreadyInit) {
-
- }
- else {
- isAlreadyInit = singleton.init();
- }
- return singleton;
- }
-
-
- const char*const Singleton::toString()
- {
- static char* str = "singleton";
- return str;
- }
-
-
- Singleton::Singleton()
- {
-
- }
-
-
- Singleton::~Singleton()
- {
-
- }
-
-
- const bool Singleton::init()
- {
-
- return true;
- }
#include "Singleton.h"
Singleton& Singleton::getInstance()
{
static Singleton singleton; // ①
static bool isAlreadyInit = false; // ②
if (isAlreadyInit) {
// do nothing...
}
else {
isAlreadyInit = singleton.init();
}
return singleton;
}
// to string
const char*const Singleton::toString()
{
static char* str = "singleton";
return str;
}
// constructor
Singleton::Singleton()
{
// do nothing...
}
// destructor
Singleton::~Singleton()
{
// do nothing...
}
// initialize
const bool Singleton::init()
{
// do nothing...
return true;
}
①唯一のインスタンスをstaticで定義しています。
②初期化処理を最初の1回だけ行うために初期化済みフラグを定義して初期化したらtrueを代入します。
注:この実装はスレッドセーフではありません。
最後にmain.cppです。
- #include "Singleton.h"
- #include <stdio.h>
-
- void main (void)
- {
- Singleton& apple = Singleton::getInstance();
- printf("%s\n", apple.toString());
- printf("hit any key...\n");
- while(getchar() == EOF);
- }
- </stdio.h>
#include "Singleton.h"
#include
void main (void)
{
Singleton& apple = Singleton::getInstance(); // ①
printf("%s\n", apple.toString()); // ②
printf("hit any key...\n"); // ③
while(getchar() == EOF); // ④
}
①getInstance()メソッドでインスタンスを取得しています。
②取得したインスタンスのtoString()メソッドを呼び出しています。
③"hit any key..."を標準出力に表示しています。
④いずれかのkeyが押されるまでループしています。
以上で
Singletonパターンの説明は終わりです。ソースコードはコピペで動かせるハズですので是非試してみてください。