2

Windows XP を実行しているデバイス用のアプリケーションを作成しています。デバイスには 2 つのバージョンがあり、各バージョンには、デバイスのソフトウェアと通信するための独自の API があります。私が書いているアプリケーションは、API から同じデータを取得する必要があります。私の質問は、実行時にデバイスのバージョンを検出し、適切な API を使用するアプリケーションを作成する方法です。レジストリを読み取ってデバイスを特定する方法を理解しました。

すべての一般的なメソッドと、インターフェイスを実装する各デバイスのクラスを含むインターフェイスを作成しました。ここで、実行時に正しいものを有効にする方法を知る必要があります。

 Public Interface IAPI

    Sub InitializeMachine()

    Function GetActiveProgram() As String

    Function GetActiveGCodes() As String

    Function GetCurrentBlockNumber() As Integer

    ''#etc...

End Interface

''#Mill API
Public Class CMAPI : Implements IAPI

    Private ObjMachine As Okuma.CMDATAPI.DataAPI.CMachine
    Private ObjPgm As Okuma.CMDATAPI.DataAPI.CProgram

    Public Sub New()

    End Sub

    Public Function GetActiveGCodes() As String Implements IAPI.GetActiveGCodes
        Try
            Return ObjPgm.GetGCodes
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function GetActiveProgram() As String Implements IAPI.GetActiveProgram
        Try

            Return ObjPgm.GetActiveProgramName

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function GetCurrentBlockNumber() As Integer Implements IAPI.GetCurrentBlockNumber
        Try

            Return ObjPgm.GetCurrentBlockNumber

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''#....
End Class

''#Lathe API
Public Class CLAPI : Implements IAPI
    Private ObjMachine As Okuma.CLDATAPI.DataAPI.CMachine
    Private ObjPgm As Okuma.CLDATAPI.DataAPI.CProgram

    Public Sub New()

    End Sub

    Public Function GetActiveGCodes() As String Implements IAPI.GetActiveGCodes
        Try
            Return ObjPgm.GetGCodes
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function GetActiveProgram() As String Implements IAPI.GetActiveProgram
        Try

            Return ObjPgm.GetActiveProgramName

        Catch ex As Exception
            Throw ex
        End Try
    End Function

''#...
End Class
4

2 に答える 2

2

テストされていませんが、理論は正しいです - タイプミスがあるかもしれません:P

Dim rightAPI As IAPI

If CheckForTypeCMAPI() = true Then ' You said you can determine which device youre on, replace this with the right function
    rightAPI = new CMAPI()
Else
    rightAPI = new CLAPI()
End If

' Use rightAPI wherever you need it
MessageBox.Show(rightAPI.GetActiveProgram())
于 2010-02-15T16:52:42.337 に答える
1

私はファクトリメソッドを使用します:

Dim rightAPI As IAPI


rightAPI = APIFactory.GetAPI(HowYouDistinguishDevice)

' Use rightAPI wherever you need it
MessageBox.Show(rightAPI.GetActiveProgram())


public class APIFactory

    public shared function GetAPI(string HowYouDistinguishDevice) as IAPI
        dim oAPI as IAPI
        'do whatever it is you need to do to determine which api to use
        if CMAPI then oAPI = new CMAPI
        if CLAPI then oAPI = new CLAPI
        'or you could use select, whatever
        return oAPI
    end function
end class
于 2010-02-15T18:20:24.890 に答える