单件模式(singleton)

保证一个类只有一个实例的机制.

站在类的设计者的角度,强制使一个类只能有一个实例,而不是站在类的使用者角度。

要点:
1.singleton不要实现ICloneable,避免出现多个实例,与singleton冲突
2.singleton不要支持序列化,如上同理。
3.不要实现在多现程环境中.

单件模形初形
None.gif   public   class  Singleton
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif                
private static Singleton instance = null;
ExpandedSubBlockStart.gifContractedSubBlock.gif               
private Singleton() dot.gif{ }
InBlock.gif        
public static Singleton Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//惰性初 始化
InBlock.gif
                if (instance == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                                               instance 
= new Singleton();

                            }
ExpandedSubBlockEnd.gif                 }

InBlock.gif                 
return instance;
ExpandedSubBlockEnd.gif             }

InBlock.gif                
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    
单件模形与多线程
None.gif public   class  Singleton
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
//volatile 关键字的作用是  防止编辑器在编辑时对  它所修饰的对象
InBlock.gif        
//进行微调,达到真正锁定多线程的目的
InBlock.gif
        private static volatile Singleton instance = null;
InBlock.gif        
//一个辅助对象,用来作线程锁定
InBlock.gif
        private static object lockHelper = new object();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private Singleton() dot.gif{ }
InBlock.gif        
public static Singleton Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//双检查
InBlock.gif
                if (instance == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//锁定,防止多线程访问产生对个对象
InBlock.gif
                    lock (lockHelper)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif
InBlock.gif                        
if(instance == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            instance 
= new Singleton();
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

InBlock.gif                                     
return instance;
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

用static readyonly 表示,缺点是不支持参数传递
None.gif public   class  Singleton
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static readyonly Singleton sl = new Singleton();
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private Singleton()dot.gif{}
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
/**/ ///
None.gif              上面的代码与下面的等效
ExpandedBlockStart.gifContractedBlock.gif
/**/ //
None.gif public   class  Singleton
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static readyonly Singleton s1;
InBlock.gif    
//静态的实例构造器
InBlock.gif
    static Singleton()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         s1 
= new Singleton();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
/**/ ///
None.gif .net机制自动为static对象进行了多线程处理.所以可以省略
ExpandedBlockStart.gifContractedBlock.gif
/**/ ///

转载于:https://www.cnblogs.com/sunsjorlin/archive/2005/12/16/298176.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值