vb.net下的单例模式

     单例模式确保某对象只能实例化一次,因此可以确保程序中所有对象访问同一个对象,但是也存在一个弊端,如,不能解决删除单个对象的问题等,因为缺少实际的开发运用,对性能以及单例的优缺点就不会太明白,这里重点总结一下单例模式的5种拓展实现。

      原文详见:http://www.cnblogs.com/psunny/archive/2010/06/18/1760133.html

      1 、单例模式的简单实现:(这种方式创建是线程不安全的)

Public NotInheritable Class UserDataReaderToEntityStrategy

    Shared singleInstance As UserDataReaderToEntityStrategy = Nothing

    '私有化构造函数
    Private Sub New()

    End Sub
    '创建静态方法,用于取得全局唯一实例

    Public Shared ReadOnly Property GetInstance() As UserDataReaderToEntityStrategy
        Get
            If singleInstance Is Nothing Then
                singleInstance = New UserDataReaderToEntityStrategy
            End If
            Return singleInstance
        End Get
    End Property

End Class
 

       2、安全线程的实现

Public NotInheritable Class UserDataReaderToEntityStrategy

    Shared singleInstance As UserDataReaderToEntityStrategy = Nothing
    Shared ReadOnly padlock As New Object()

    '私有化构造函数
    Private Sub New()

    End Sub

    Public Shared ReadOnly Property GetInstance() As UserDataReaderToEntityStrategy
        Get
            '这里添加一个锁,保证了线程的安全
            SyncLock padlock
                If singleInstance Is Nothing Then
                    singleInstance = New UserDataReaderToEntityStrategy
                End If
            End SyncLock
            Return singleInstance
        End Get
    End Property

End Class


      3、双重锁定

Public NotInheritable Class UserDataReaderToEntityStrategy

    Shared singleInstance As UserDataReaderToEntityStrategy = Nothing
    Shared ReadOnly padlock As New Object()

    '私有化构造函数
    Private Sub New()
    End Sub

    Public Shared ReadOnly Property GetInstance() As UserDataReaderToEntityStrategy
        Get
            '这里添加一个锁,保证了线程的安全

            If singleInstance Is Nothing Then
                SyncLock padlock
                    If singleInstance Is Nothing Then
                        singleInstance = New UserDataReaderToEntityStrategy
                    End If
                End SyncLock
            End If
            Return singleInstance
        End Get
    End Property

End Class

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

Public NotInheritable Class UserDataReaderToEntityStrategy

    Shared singleInstance As UserDataReaderToEntityStrategy = Nothing

    Shared Sub New()

    End Sub
    '私有化构造函数
    Private Sub New()
    End Sub

    Public Shared ReadOnly Property GetInstance() As UserDataReaderToEntityStrategy
        Get
            Return singleInstance
        End Get
    End Property

End Class


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

Public NotInheritable Class UserDataReaderToEntityStrategy

    Shared singleInstance As UserDataReaderToEntityStrategy = Nothing

   
    '私有化构造函数
    Private Sub New()
    End Sub

    Public Shared ReadOnly Property GetInstance() As UserDataReaderToEntityStrategy
        Get
            Return Nested.singleInstance
        End Get
    End Property
    Private Class Nested
        Shared Sub New()

        End Sub
        Friend Shared ReadOnly singleInstance As New UserDataReaderToEntityStrategy()
    End Class
End Class


 

 

作者: Sunny Peng
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Visual Basic 2010 (VB.NET) 是一种广泛使用的编程语言,旨在帮助开发者创建Windows应用程序。以下是从入门到精通VB.NET的一些要点: 入门: 1. 熟悉VB.NET的开发环境:可以下载安装Visual Studio,并了解其界面和各种工具。 2. 学习VB.NET的基本语法:从数据类型、变量和常量到条件语句、循环和函数等基础知识,都需要掌握。 3. 理解面向对象编程:VB.NET是基于面向对象编程的语言,需要掌握类、对象、继承、多态等概念。 进阶: 1. 学习VB.NET的高级语法:包括异常处理、事件处理、LINQ查询、委托和Lambda表达式等。 2. 深入理解Windows Forms:掌握创建窗体、界面布局、控件使用和事件处理等技术。 3. 数据库编程:学习如何使用VB.NET与各种数据库进行交互,包括连接、查询、更新和事务等操作。 4. 网络编程:了解如何使用VB.NET进行Socket编程、Web服务和远程调用等网络相关的操作。 精通: 1. 掌握高级技术和框架:学习如何使用VB.NET进行并发编程、多线程处理和异步操作。了解并使用.NET Framework中的各种高级类库和框架。 2. 设计模式和架构:学习各种设计模式如工厂模式、单例模式、观察者模式等,并能应用到实际项目中。 3. 性能优化和调试:了解和掌握VB.NET的性能优化技巧和常用的调试工具,提高代码的质量和效率。 4. 实际项目经验:通过参与实际的VB.NET项目开发,不断积累经验和掌握更多的开发技能。 总之,要精通VB.NET,需要通过学习基础知识、掌握高级技术和框架,并积累实际项目经验。同时也要持续学习和保持对新技术的关注,以不断提升自己的编程水平。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值