Singleton (单件模式)
2007-08-05 18:24
190 查看
意图: 保证一个类仅有一个实例,并提供一个访问它的全局访问点。
使用性:
当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。
当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。
代码:
namespace Singleton_DesignPattern
{
using System;
class Singleton
{
private static Singleton _instance;
public static Singleton Instance()
{
if (_instance == null)
_instance = new Singleton();
return _instance;
}
protected Singleton(){}
// Just to prove only a single instance exists
private int x = 0;
public void SetX(int newVal) {x = newVal;}
public int GetX(){return x;}
}
public class Client
{
public static int Main(string[] args)
{
int val;
// can't call new, because constructor is protected
Singleton FirstSingleton = Singleton.Instance();
Singleton SecondSingleton = Singleton.Instance();
// Now we have two variables, but both should refer to the same object
// Let's prove this, by setting a value using one variable, and
// (hopefully!) retrieving the same value using the second variable
FirstSingleton.SetX(4);
Console.WriteLine("Using first variable for singleton, set x to 4");
val = SecondSingleton.GetX();
Console.WriteLine("Using second variable for singleton, value retrieved = {0}", val);
return 0;
}
}
}
//Sys.WebForms.PageRequestManager._initialize('AjaxHolder$scriptmanager1', document.getElementById('Form1'));
Sys.WebForms.PageRequestManager.getInstance()._updateControls(['tAjaxHolder$UpdatePanel1'], [], [], 90);
//]]>
td { font-size: 12px }
.commentTextBox
{
font-family : Verdana;
font-size: 13px;
}
.userData { BEHAVIOR: url(#default#userdata) }
使用性:
当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。
当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。
代码:
namespace Singleton_DesignPattern
{
using System;
class Singleton
{
private static Singleton _instance;
public static Singleton Instance()
{
if (_instance == null)
_instance = new Singleton();
return _instance;
}
protected Singleton(){}
// Just to prove only a single instance exists
private int x = 0;
public void SetX(int newVal) {x = newVal;}
public int GetX(){return x;}
}
public class Client
{
public static int Main(string[] args)
{
int val;
// can't call new, because constructor is protected
Singleton FirstSingleton = Singleton.Instance();
Singleton SecondSingleton = Singleton.Instance();
// Now we have two variables, but both should refer to the same object
// Let's prove this, by setting a value using one variable, and
// (hopefully!) retrieving the same value using the second variable
FirstSingleton.SetX(4);
Console.WriteLine("Using first variable for singleton, set x to 4");
val = SecondSingleton.GetX();
Console.WriteLine("Using second variable for singleton, value retrieved = {0}", val);
return 0;
}
}
}
//Sys.WebForms.PageRequestManager._initialize('AjaxHolder$scriptmanager1', document.getElementById('Form1'));
Sys.WebForms.PageRequestManager.getInstance()._updateControls(['tAjaxHolder$UpdatePanel1'], [], [], 90);
//]]>
td { font-size: 12px }
.commentTextBox
{
font-family : Verdana;
font-size: 13px;
}
.userData { BEHAVIOR: url(#default#userdata) }
相关文章推荐
- 单件模式(Singleton)和锁定(lock)
- 单件模式在C#中的实现--Implementing the Singleton Pattern in C#
- 设计模式笔记--创建型模式之五--单件 Singleton
- 1、Singleton 单件(创建模式)
- Singleton(单件)模式与类静态函数(实用类)的区别
- 单件模式 Singleton
- Singleton 单件模式
- 单件模式[Singleton]
- C#面向对象设计模式学习笔记(1) - Singleton 单件模式(创建型模式)
- Singleton(单件)模式的两种实现方法
- 单实例模式的实现(singleton)----单件
- 关于Singleton 单件(创建型模式)
- 设计模式入门(1)------单件(Singleton)
- 五种单件模式之Singleton的实现方法详解
- 单件模式(Singleton)
- 学习笔记[1]——Singleton(单件)模式
- (创建型模式)Singleton 单件模式
- [整理]C#面向对象设计模式纵横谈(2):Singleton单件(创建型模式)
- singleton modle.(单件模式 / 单态…
- PHP 实现单件(singleton)模式