标题:Singleton 单件创建模式
只看楼主
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
结帖率:78.95%
 问题点数:0 回复次数:3 
Singleton 单件创建模式
程序代码:
'Singleton模式:一个类只会创建一个实例。
Module Module1

    Sub Main()
        Dim In1 As Singleton = Singleton.Intance
        In1.Name = "李小龙"
        Dim In2 As Singleton = Singleton.Intance

        System.Console.WriteLine(Object.ReferenceEquals(In1, In2) = True)
        System.Console.WriteLine(In1.Name)
        System.Console.WriteLine(In2.Name)
        System.Console.ReadLine()

    End Sub

End Module

Public Class Singleton

    Private Shared m_Intance As Singleton
    Private Shared m_Mutex As New System.Threading.Mutex '同步基元也可用于进程间同步
    Private m_Name As String
    Private Sub New()

    End Sub

    Public Shared Function Intance() As Singleton
        m_Mutex.WaitOne() '当在派生类中重写时,阻塞当前线程,直到当前的 System.Threading.WaitHandle 收到信号

        Try
            If m_Intance Is Nothing Then
                m_Intance = New Singleton   '释放 System.Threading.Mutex 一次
            End If
        Finally
            m_Mutex.ReleaseMutex()
        End Try
        Return m_Intance
    End Function

    Public Property Name() As String
        Get
            Return Me.m_Name
        End Get
        Set(ByVal value As String)
            Me.m_Name = value
        End Set
    End Property
End Class
搜索更多相关主题的帖子: Singleton 单件 模式 
2008-02-18 09:21
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
得分:0 
更简单的实现(缺点:不支持带参构造函数)
程序代码:
'Singleton模式:一个类只会创建一个实例。
Module Module1

    Sub Main()
        Dim In1 As Singleton = Singleton.Intance
        In1.Name = "李小龙"
        Dim In2 As Singleton = Singleton.Intance
        System.Console.WriteLine(Object.ReferenceEquals(In1, In2) = True)
        System.Console.WriteLine(In1.Name)
        System.Console.WriteLine(In2.Name)
        System.Console.ReadLine()

    End Sub

End Module

Public Class Singleton
    Private m_Name As String
    Public Shared ReadOnly Intance As New Singleton
    Private Sub New()

    End Sub

   

    Public Property Name() As String
        Get
            Return Me.m_Name
        End Get
        Set(ByVal value As String)
            Me.m_Name = value
        End Set
    End Property
End Class

/images/2011/147787/2011051411021524.jpg" border="0" />
2008-02-18 11:17
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
得分:0 
Singleton模式扩展思想:从一个实例扩展到n个实例,例如对象池的实现.
Singleton模式核心思想:如果控制用户使用New对一个类的实例构造器的
任意调用

[[it] 本帖最后由 ioriliao 于 2008-2-18 11:32 编辑 [/it]]

/images/2011/147787/2011051411021524.jpg" border="0" />
2008-02-18 11:31
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
得分:0 
.Net 框架中singleton模式的应用
一个简单的例子
程序代码:
Dim cl1 As New MyClasses
        Dim cl2 As New MyClasses

        Dim ct1 As Type
        Dim ct2 As Type
        '错误写法 Dim ct3 As New .... 你将会看到 加上New关键字后,智能感知将不会感知到Type
        '可知Type类是singleton模式的.

        ct1 = cl1.GetType
        ct2 = cl1.GetType

Public Class MyClasses

End Class

/images/2011/147787/2011051411021524.jpg" border="0" />
2008-02-18 11:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-200207-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.341970 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved