views:

51

answers:

2

I'm writing an application for a device running Windows XP. There are 2 versions of the device and each version has its own API to communicate with the device's software. The application I'm writing needs to pull the same data from the API. My question is how to write a application that will detect at runtime which version of the device it is on and use the appropriate API. I've figured out how to read the registry to determine the device.

I've created an interface containing all of the common methods and also classes for each device that implement the interface. Now I need to know how to activate the correct one at runtime.

 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
+1  A: 

Untested, theory is right - there just might be typos :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())
BarrettJ
what's to test? prime principle: program to the interface not the implementation. with that in mind the road forward is plain to see...
Sky Sanders
edited to indicate I meant there might be typos as I don't have a vb.net compiler on this machine :P
BarrettJ
Excellent thanks so much! I understand now the concept of programing to the interface. I've been banging my head on the wall all morning trying to figure this out.
jw7694
A: 

I would use a factory method:

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
David
Thanks this is a good idea
jw7694