技术开发 频道

.Net设计模式之桥接模式

  具体的读取类实现ReadData方法。下面是从文本文件中读取数据,其中给出了读取用逗号分隔数据的方法:

Public Class clsReadDataFromFileClass clsReadDataFromFile Inherits ClsAbstractReadData Private datafilepath As String Public Sub New()Sub New(ByVal strpath As String)  MyBase.New( ) Datafilepath = strpath End Sub Public Overrides Function ReadData()Function ReadData(ByVal strDate As String) As System.Date.DataSet '调用ReadDataFromFile从具体的文件中读取数据,省略 Return mysourceds End Function Private Sub ReadDataFromFile()Sub ReadDataFromFile(ByVal_ fn As String,,ByVal dt As DataTable,ByVal cn As Integer )  Dim fileNumber As Integer  FileNumber = FreeFile( )  FileOpen(fileNumber, fn, OpenMode.Input)  Dim NextLine As String  I = 1  Do Until EOF(fileNumber) NextLine = LineInput(fileNumber) If I > 1 And Trim(NextLine) <> " " Then Dim j As Integer Dim r As DataRow = dt.NewRow Dim s As String = "," Dim sp As Char( ) = s.ToCharArray Dim rs As String( ) = NextLine.Split(sp) For j = 0 To cn – 1     Dim v As String     Try    V = rs ( j )    r.Item( j ) = Trim( v )    Catch ex As Exception    r.Item( j ) = " "    End Try Next Dt.Rows.Add(r) End If I = I + 1 Loop FileClose(fileNumber) End Sub Private Function IsFileExist()Function IsFileExist_ (ByVal fn As String) As Boolean If FileSystem.Dir (fn) = " " Then Return False Return True End Function End Class
  下面是产生读取对象的简单工厂:
Public Class clsReaDataFactoryClass clsReaDataFactory Pbulic Shared Function getDataReader()Function getDataReader ( ) As clsAbstractReadData  Dim datafilepath As String = ConfigurationSettings.AppSettings (“DataFilePath”) Dim strDataType As String = ConfigurationSettings.AppSettings (“DataType”) Dim strHttp As String = ConfigurationSettings.AppSettings (“DataURL”) Select Case strDataType Case “http”   Dim mrd As clsReadDataFromRemoteFile   Mrd = New clsReadDataFromRemoteFile (strHttp)   Return mrd Case Else   Dim mrd As clsReadDataFromFile   Mrd = New clsReadDataFromFile(datafilepath)   Return mrd End Select End Function end class

0
相关文章