singleton示例代码

 

实现singleton基础代码:

    public class singleton
    {
        
private static singleton instance;

        
private singleton() { }

        
public static singleton instance
        {
            
get
            {
                
if (instance == null)
                {
                    instance 
= new singleton();
                }
                
return instance;
            }
        }
    }

 

DOUBLE CHECK避免多线程同时产生对象:

 

ContractedBlock.gif ExpandedBlockStart.gif 加入了locker
 1  public class singleton
 2     {
 3         private static volatile singleton instance = null;
 4 
 5         private static object lockHelper = new object();
 6 
 7         private singleton() { }
 8 
 9         public static singleton instance
10         {
11             get
12             {
13                 if (instance == null)
14                 {                       //当多线程同时到达此处,检查instance为null,会同时产生实例.
15                     lock (lockHelper)
16                     {
17                         if (instance == null)
18                         {
19                             instance = new singleton();
20                         }
21                     }
22                 }
23                 return instance;
24             }
25         }
26     }

 

简捷的代码实现double check的效果:弊端在于无法传递参数

 

ContractedBlock.gif ExpandedBlockStart.gif 简单实现
 1    class Singleton     //最简捷的代码,实现double check效果的singleton
 2     {
 3         public static readonly Singleton instance = new Singleton();
 4         private Singleton() { }
 5     }
 6 
 7     class Singleton     //上例中的代码展开的写法
 8     {
 9         public static readonly Singleton instance;
10 
11         static Singleton()
12         {
13             instance = new Singleton();
14         }
15 
16         private Singleton() { }
17     }

 

传递参数的:

ContractedBlock.gif ExpandedBlockStart.gif 传参
 1     public class singleton
 2     {
 3         private static singleton instance;
 4 
 5         private singleton(int x,int y)
 6         {
 7             this.x = x;
 8             this.y = y;
 9         }
10 
11         public static singleton GETinstance
12         {
13             get
14             {
15                 if (instance == null)
16                 {
17                       instance = new singleton(x,y);
18                 }
19                 return instance;
20             }
21             int x;
22             int y;
23         }
24     }
25 

 

static menthod get singleton :

ContractedBlock.gif ExpandedBlockStart.gif static menthod
        public static singleton GETinstance
        {
            
get
            {
                
if (instance == null)
                {
                      instance 
= new singleton(x,y);
                }
                
return instance;
            }
            
int x;
            
int y;
        }
    }

    
class Singleton     //最简捷的代码,实现double check效果的singleton
    {
        
public static readonly Singleton instance = new Singleton();
        
private Singleton() { }

        
public int x
        {
            
get 
            {
                
return this.x;
            }
            
set
            {
                
this.x = x;
            }
        }

        
public int y
        {
            
get 
            {
                
return this.y;
            }
            
set
            {
                
this.y = y;
            }
            
int x;
            
int y;
        }
    }

调用方法:

 

ContractedBlock.gif ExpandedBlockStart.gif linke
    class test
    {
        
public static void Main()
        {
            Singleton instance 
= Singleton.instance();
            instance.x 
= 100;
            instance.y 
= 200;
            Console.WriteLine(instance);
        }
    }

 

 

我考。。终于写完啦。。。

 

转载于:https://www.cnblogs.com/stupid-fool/archive/2009/04/24/1443096.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值