1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

デザインパターン学習メモ:「Singleton」

Posted at

このパターンの目的

GoF本より引用する。

 あるクラスに対してインスタンスが1つしか存在しないことを保証し、それにアクセスするためのグローバルな方法を提供する。

実装例

『アジャイルソフトウェア開発の奥義』(P.231)より引用する。

Singleton.java
public class Singleton
{
	private static Singleton theInstance = null;
	private Singleton() {}
	public static Singleton Instance()
	{
		if (theInstance = null)
			theInstance = new Singleton();
		return theInstance;
	}
}

利点

  • あるクラスに対して、インスタンスが1つしか存在しないことを保証する
    • システム内の一貫性を保つ
    • 無駄なインスタンス生成を行わない

参考文献

  • エリック ガンマ、ラルフ ジョンソン、リチャード ヘルム、ジョン プリシディース(1999)『オブジェクト指向における再利用のためのデザインパターン 改訂版』本位田 真一、吉田 和樹 監訳、SBクリエイティブ
  • ロバート・C・マーチン(2004)『アジャイルソフトウェア開発の奥義 第2版 オブジェクト指向開発の神髄と匠の技』瀬谷啓介訳、SBクリエイティブ
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?