VB.NET中的单例模式

Visual Basic .NET 和 Visual C# .NET都属于.NET平台下的语言,它们之间的本质区别较小,区别基本都是语法结构上的,网上提供了很多关于Visual C# .NET的设计模式的例子,唯独Visual Basic .NET基本没有。既然两种语言都属于.NET平台,并且最终都是通过MSIL和CLR机制来运行,要实现它们之间的转换并不难。

在参照了Terry Lee的设计模式系列之单例模式之后,我把它们改造为Visual Basic代码。

单例模式的五种写法。

引用:http://terrylee.cnblogs.com/archive/2005/12/09/293509.html

1.简单实现

注意:这种方式创建是线程不安全的

C# Code

public sealed class Singleton
{
    static Singleton instance=null;

    Singleton()
    {
    }

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

 

VB Code

Public NotInheritable Class Singleton
	Shared m_instance As Singleton = Nothing

	Private Sub New()
	End Sub

	Public Shared ReadOnly Property Instance() As Singleton
		Get
			If m_instance Is Nothing Then
				m_instance = New Singleton()
			End If
			Return m_instance
		End Get
	End Property
End Class

2.安全的线程

C# Code

public sealed class Singleton
{
     static Singleton instance=null;
     static readonly object padlock = new object();
 
     Singleton()
     {
     }
 
    public static Singleton Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance==null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }
}

VB Code

Public NotInheritable Class Singleton
	Shared m_instance As Singleton = Nothing
	Shared ReadOnly padlock As New Object()

	Private Sub New()
	End Sub

	Public Shared ReadOnly Property Instance() As Singleton
		Get
			SyncLock padlock
				If m_instance Is Nothing Then
					m_instance = New Singleton()
				End If
				Return m_instance
			End SyncLock
		End Get
	End Property
End Class

3.双重锁定

C# Code

public sealed class Singleton
{
     static Singleton instance=null;
     static readonly object padlock = new object();
 
     Singleton()
     {
     }
 
    public static Singleton Instance
    {
        get
        {
            if (instance==null)
            {
                lock (padlock)
                {
                    if (instance==null)
                    {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }
}

VB Code

Public NotInheritable Class Singleton
	Shared m_instance As Singleton = Nothing
	Shared ReadOnly padlock As New Object()

	Private Sub New()
	End Sub

	Public Shared ReadOnly Property Instance() As Singleton
		Get
			If m_instance Is Nothing Then
				SyncLock padlock
					If m_instance Is Nothing Then
						m_instance = New Singleton()
					End If
				End SyncLock
			End If
			Return m_instance
		End Get
	End Property
End Class

4.静态初始化(首选方式)

C# Code

public sealed class Singleton
{
     static readonly Singleton instance=new Singleton();
 
     static Singleton()
     {
     }
 
     Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return instance;
        }
    }
}

VB Code

Public NotInheritable Class Singleton
	Shared ReadOnly m_instance As New Singleton()

	Shared Sub New()
	End Sub

	Private Sub New()
	End Sub

	Public Shared ReadOnly Property Instance() As Singleton
		Get
			Return m_instance
		End Get
	End Property
End Class

5.延迟初始化(比较常用)

C# Code

public sealed class Singleton
 {
     Singleton()
     {
     }
 
     public static Singleton Instance
     {
         get
        {
            return Nested.instance;
        }
    }
    
    class Nested
    {
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }
}

VB Code

Public NotInheritable Class Singleton
	Private Sub New()
	End Sub

	Public Shared ReadOnly Property Instance() As Singleton
		Get
			Return Nested.instance
		End Get
	End Property

	Private Class Nested
		Shared Sub New()
		End Sub

		Friend Shared ReadOnly instance As New Singleton()
	End Class
End Class

 

 

实现要点
        Singleton模式是限制而不是改进类的创建。
         Singleton类中的实例构造器可以设置为Protected以允许子类派生。
         Singleton模式一般不要支持Icloneable接口,因为这可能导致多个对象实例,与Singleton模式的初衷违背。
         Singleton模式一般不要支持序列化,这也有可能导致多个对象实例,这也与Singleton模式的初衷违背。
         Singleton只考虑了对象创建的管理,没有考虑到销毁的管理,就支持垃圾回收的平台和对象的开销来讲,我们一般没必要对其销毁进行特殊的管理。
         理解和扩展Singleton模式的核心是“如何控制用户使用new对一个类的构造器的任意调用”。
 
 
         可以很简单的修改一个Singleton,使它有少数几个实例,这样做是允许的而且是有意义的。
优点
         实例控制:Singleton 会阻止其他对象实例化其自己的 Singleton 对象的副本,从而确保所有对象都访问唯一实例
         灵活性:因为类控制了实例化过程,所以类可以更加灵活修改实例化过程
缺点
         开销:虽然数量很少,但如果每次对象请求引用时都要检查是否存在类的实例,将仍然需要一些开销。可以通过使用静态初始化解决此问题,上面的五种实现方式中已经说过了。
          可能的开发混淆:使用 singleton 对象(尤其在类库中定义的对象)时,开发人员必须记住自己不能使用 new 关键字实例化对象。因为可能无法访问库源代码,因此应用程序开发人员可能会意外发现自己无法直接实例化此类。
         对象的生存期:Singleton 不能解决删除单个对象的问题。在提供内存管理的语言中(例如基于 .NET Framework 的语言),只有 Singleton 类能够导致实例被取消分配,因为它包含对该实例的私有引用。在某些语言中(如 C++),其他类可以删除 
对象实例,但这样会导致 Singleton 类中出现悬浮引用。
适用性
         当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。
         当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展

实现要点

l        Singleton模式是限制而不是改进类的创建。

l         Singleton类中的实例构造器可以设置为Protected以允许子类派生。

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

l         Singleton模式一般不要支持序列化,这也有可能导致多个对象实例,这也与Singleton模式的初衷违背。

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

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

 

 

l         可以很简单的修改一个Singleton,使它有少数几个实例,这样做是允许的而且是有意义的

优点

l         实例控制:Singleton 会阻止其他对象实例化其自己的 Singleton 对象的副本,从而确保所有对象都访问唯一实例

l         灵活性:因为类控制了实例化过程,所以类可以更加灵活修改实例化过程

缺点

l         开销:虽然数量很少,但如果每次对象请求引用时都要检查是否存在类的实例,将仍然需要一些开销。可以通过使用静态初始化解决此问题,上面的五种实现方式中已经说过了。

l          可能的开发混淆:使用 singleton 对象(尤其在类库中定义的对象)时,开发人员必须记住自己不能使用 new 关键字实例化对象。因为可能无法访问库源代码,因此应用程序开发人员可能会意外发现自己无法直接实例化此类。

l         对象的生存期:Singleton 不能解决删除单个对象的问题。在提供内存管理的语言中(例如基于 .NET Framework 的语言),只有 Singleton 类能够导致实例被取消分配,因为它包含对该实例的私有引用。在某些语言中(如 C++),其他类可以删除 
对象实例,但这样会导致 Singleton 类中出现悬浮引用。

适用性

l         当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。

l         当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。

posted @ 2010-06-18 14:39  Sunny Peng  阅读(3559)  评论(0编辑  收藏  举报