只有一个实例的类,singlton

None.gif content
None.gif
" // Singleton
None.gif

None.gif
//  Intent: ""Ensure a class only has one instance, and provide a global
None.gif
//  point of access to it"". 
None.gif
None.gif
//  For further information, read ""Design Patterns"", p127, Gamma et al.,
None.gif
//  Addison-Wesley, ISBN:0-201-63361-2
None.gif

ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Notes:
InBlock.gif * If it makes sense to have only a single instance of a class (a so-called
InBlock.gif * singleton), then it makes sense to enforce this (to elimintate potential 
InBlock.gif * errors, etc). 
InBlock.gif * 
InBlock.gif * A class based on the singleton design pattern protects its constructor, 
InBlock.gif * so that only the class itself (e.g. in a static method) may instantiate itself. 
InBlock.gif * It exposes an Instance method which allows client code to retrieve the 
InBlock.gif * current instance, and if it does not exist to instantiate it.  
ExpandedBlockEnd.gif 
*/

None.gif 
None.gif
namespace  Singleton_DesignPattern
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
using System;
InBlock.gif
InBlock.gif    
class Singleton 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private static Singleton _instance;
InBlock.gif        
InBlock.gif        
public static Singleton Instance()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (_instance == null)
InBlock.gif                _instance 
= new Singleton();
InBlock.gif            
return _instance;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
protected Singleton()dot.gif{}
InBlock.gif
InBlock.gif        
// Just to prove only a single instance exists
InBlock.gif
        private int x = 0;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public void SetX(int newVal) dot.gif{x = newVal;}
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public int GetX()dot.gif{return x;}        
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
///    Summary description for Client.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Client
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static int Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int val;
InBlock.gif            
// can't call new, because constructor is protected
InBlock.gif
            Singleton FirstSingleton = Singleton.Instance(); 
InBlock.gif            Singleton SecondSingleton 
= Singleton.Instance();
InBlock.gif
InBlock.gif            
// Now we have two variables, but both should refer to the same object
InBlock.gif            
// Let's prove this, by setting a value using one variable, and 
InBlock.gif            
// (hopefully!) retrieving the same value using the second variable
InBlock.gif
            FirstSingleton.SetX(4);
InBlock.gif            Console.WriteLine(
""Using first variable for singleton, set x to 4"");        
InBlock.gif
InBlock.gif            val 
= SecondSingleton.GetX();
ExpandedSubBlockStart.gifContractedSubBlock.gif            Console.WriteLine(
""Using second variable for singleton, value retrieved = dot.gif{0}"", val);        
InBlock.gif            
return 0;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif注意:因为.net回收的不确定性,所以在类的Dispose或析构函数中要加入一句_insta=
null "

转载于:https://www.cnblogs.com/tingfeng/articles/488514.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值