套用GOF的Singleton(Design Patten)於VB.NET上,
可以實作於 計數器、取得系統參數、共用參數…等Class...
好處是記憶體中只會存在一個instance...所以資料的取得是非常一致的…
故名思意為singleton...


Public Class SystemParameter
Private Shared myInstance As SystemParameter

Private _syear As String ' 目前學年
Private _semseter As String ' 目前學期
Private _syear_data As String ' 資料將轉至學年
Private _semester_data As String ' 資料將轉至學期

Sub New()
        SetSystemParameter()
End Sub

' 取得系統參數資料
Private Sub SetSystemParameter()

        Dim conn As SqlConnection = DBUtil.CreateSQLConnection()
        Dim rd As SqlDataReader
        Dim scmSystemParameter As New SqlCommand

        Try
                With scmSystemParameter
                        .Connection = conn
                        .CommandType = CommandType.Text
                        .CommandText = "SELECT * FROM system_param"
                End With
                conn.Open()
                rd = scmSystemParameter.ExecuteReader
                While rd.Read
                        _syear = rd.Item("sp_syear").ToString() ' 目前學年
                        _semseter = rd.Item("sp_semester").ToString() ' 目前學期
                        _syear_data = rd.Item("sp_data_syear").ToString() ' 資料將轉至學年
                        _syear_data = rd.Item("sp_data_semester").ToString() ' 資料將轉至學期
                End While

        Catch ex As Exception
                Throw New Exception(ex.ToString)
        Finally
                '  關閉connectoin 連結
                conn.Close()
        End Try
End Sub

' 學年
Public ReadOnly Property SYear() As String
        Get
                Return Me._syear
        End Get
End Property

' 學期
Public ReadOnly Property Semester() As String
        Get
                Return Me._semseter
        End Get
End Property

' 資料將轉至學年
Public ReadOnly Property SYear_Data() As String
        Get
                Return Me._syear_data
        End Get
End Property

' 資料將轉至學期
Public ReadOnly Property Semester_Data() As String
        Get
                Return Me._semester_data
        End Get
End Property

Public Shared Function GetInstance() As SystemParameter
        If myInstance Is Nothing Then
                myInstance = New SystemParameter
        End If
        Return myInstance
End Function
End Class
arrow
arrow
    全站熱搜

    湯瑪的吳 發表在 痞客邦 留言(2) 人氣()