WebCast听课录(3)

课程名:C#面向对象设计模式纵横谈2. Singleton件(创建型模式)
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

模式分类

 

从目的来看:

创建型(Creational)模式:负责对象创建。

结构型(Structural)模式:处理类与对象间的组合。

行为型(Behavioral)模式:类与对象交互中的职责分配。

          

从范围来看:

类模式处理类与子类的静态关系。

对象模式处理对象间的动态关系。

 

动机(Motivation

 

在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例,才能确保它们的逻辑正确性、以及良好的效率。如何绕过常规的构造器,提供一种机制来保证一个类只有

一个实例?这应该是类设计者的责任,而不是使用者的责任。

 

意图(Intent

 

保证一个类仅有一个实例,并提供一个该实例的全局访问点。——《设计模式》GoF

 

结构(Structure

 

单线程Singleton 模式实现

ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
InBlock.gif    
/// 单线程Singleton类
ExpandedBlockEnd.gif    
/// </summary>

None.gif     
None.gif
public   class  Singleton
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private static Singleton instance = null;
InBlock.gif
InBlock.gif        
private Singleton()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static Singleton Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(instance==null)
InBlock.gif                    instance 
= new Singleton();
InBlock.gif
InBlock.gif                
return instance;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Singleton st1 
= Singleton.Instance;
InBlock.gif
InBlock.gif            Singleton st2 
= Singleton.Instance;
InBlock.gif
InBlock.gif            Console.WriteLine(Object.ReferenceEquals(st1,st2)
==true);
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif

单线程Singleton模式的几个要点

 

. Singleton 模式中的实例构造器可以设置为protected以允许子类派生。

 

. Singleton 模式一般不要支持ICloneable接口,因为这可能会导致多个对象实例,与Singleton模式的初衷违背。

 

. Singleton 模式一般不要支持序列化,因为这也有可能导致多个对象实例,同样与Singleton模式的初衷违背。因为序列化对象到磁盘后,再反序列化就可以生成一个新对象出来,并且是原对象的深度拷贝。

 

. Singletom 模式只考虑到了对象创建的管理,没有考虑对象销毁的管理。就支持垃圾回收的平台和对象的开销来讲,我们一般没有必要对其销毁进行特殊的管理。

 

. 不能应对多线程环境:在多线程环境下,使用Singleton模式仍然有可能得到Singleton类的多个实例对象。

 

多线程Singleton 模式实现

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif    
/// 多线程Singleton类
ExpandedBlockEnd.gif    
/// </summary>

None.gif      public   class  Singleton
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private static volatile Singleton instance = null;
InBlock.gif
InBlock.gif        
private static object lockHelper = new object();
InBlock.gif
InBlock.gif        
private Singleton()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static Singleton Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(instance==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.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                        }

InBlock.gif
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
return instance;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedBlockEnd.gif    }

None.gif
None.gif

其中,volatile 关键字指示字段可由操作系统、硬件或并发执行的线程在程序中进行修改。

 

使用.NET类型初始化机制实现多线程Singleton 模式

 

使用了静态构造器,.net framework可以提供内建的机制确保多线程访问时不会产生多个实例。

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif    
/// 多线程Singleton类
ExpandedBlockEnd.gif    
/// </summary>
None.gif      public   class  Singleton
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public static readonly Singleton Instance = new Singleton();
InBlock.gif
InBlock.gif        
private Singleton()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }
        
ExpandedBlockEnd.gif    }

None.gif


与下面的类是等同的:

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif    
/// 多线程Singleton类
ExpandedBlockEnd.gif    
/// </summary>
None.gif
None.gif    
public   class  Singleton
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public static readonly Singleton Instance;
InBlock.gif
InBlock.gif        
static  Singleton()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Instance 
= new Singleton();
ExpandedSubBlockEnd.gif        }
    
InBlock.gif    
InBlock.gif        
private Singleton()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif

Singleton模式扩展

 

. 将一个实例扩展到n个实例,例如对象池的实现。

 

. new 构造器的调用转移到其他类中,例如多个类协同工作环境中,某个局部环境只需要拥有某个类的一个实例。

 

. 理解和扩展Singleton模式的核心是“如何控制用户使用new对一个类的实例构造器的任意调用”。

.NET框架中的Singleton应用

None.gif             Class1 c1  =   new  Class1();
None.gif            Class1 c2 
=   new  Class1();
None.gif
None.gif            Type t1 
=  c1.GetType();
None.gif            Type t2 
=  c2.GetType();
None.gif
None.gif            
if (t1 == t2)
None.gif                Console.WriteLine(
" type equal " );
None.gif


 

这里GetType()内部就是扩展了Singleton模式,从而确保只有一种type。再例如:HttpContext.Current也是一个Singleton的实例。



课后心得:

 

1使用了静态构造器,.net framework可以提供内建的机制确保多线程访问时不会产生多个实例。

 

2,初步了解使用idasm查看汇编出的代码结构,如下图:

2006101301.JPG

3,思考:

 

考虑单线程的Singleton模式:

None.gif public   class  Singleton
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private static  Singleton instance;
InBlock.gif
InBlock.gif        
private int x;
InBlock.gif        
private int y;
InBlock.gif      
InBlock.gif        
private Singleton(int x,int y)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.x = x;
InBlock.gif            
this.y = y;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int X
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.x;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public static Singleton GetInstance(int x,int y)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(instance==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                instance 
= new Singleton(x,y);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return instance;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
None.gif        
static   void  Main( string [] args)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Singleton st1 
= Singleton.GetInstance(100,200);
InBlock.gif
InBlock.gif            Singleton st2 
= Singleton.GetInstance(399,399);
InBlock.gif
InBlock.gifConsole.WriteLine(st1.X);
InBlock.gif
InBlock.gif            Console.WriteLine(st2.X);
ExpandedBlockEnd.gif        }

None.gif
None.gif

会是什么结果呢?答案是都是100,因为.net的内建机制可以确保只会调用一次实例化过程,而不会受到你以后传的参数的影响。那你硬是想以后的参数修改也要有效,怎么办呢?很简单,只要修改如下:

None.gif public   static  Singleton GetInstance( int  x, int  y)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if(instance==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                instance 
= new Singleton(x,y);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                instance.x 
= x;
InBlock.gif                instance.y 
= y;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return instance;
ExpandedBlockEnd.gif        }

None.gif

那如果想在多线程模式中实现,又当如何呢?由于它是静态的构造函数,因此不能够直接传参数给构造函数,因此只有放出公共属性给外部调用。

None.gif      public   class  staticSingleton
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif
InBlock.gif        
private int x;
InBlock.gif        
private int y;
InBlock.gif
InBlock.gif        
public static readonly staticSingleton  Instance = new staticSingleton();
InBlock.gif        
public int X
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.x;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.x = x;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int Y
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.y;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.y = y;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private staticSingleton()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif        
static   void  Main( string [] args)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            staticSingleton st1 
= staticSingleton.Instance;
InBlock.gif            st1.X 
= 100;
InBlock.gif            st1.Y 
= 200;
InBlock.gif
InBlock.gif            Console.WriteLine(st1.X);
ExpandedBlockEnd.gif        }

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值