1

挑战在于提出最好的 vb.net 设计模式,该模式将允许在 2 个不同的大型机访问 com 对象之间进行切换。当然这一切都无需改变业务逻辑。

到目前为止,我想出的是一种工厂类型的方法(请参阅下面的精简伪代码)。我有 2 个基于枚举开关实例化的类,它们具有相同的方法和属性,但具有不同的底层对象特定方法。

我是在正确的轨道上还是应该采取另一种方法?谢谢你的帮助。

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Private MFA as MFSession = New MFSession(Enums.mfaccesstype.type1)
Private Sub Form1_Load()
    MFA.GotoScreen("XYZ")
End Sub
End Class

Public Class MFSession
Private SystemAccess As Object
End Property
Sub New(ByVal AccessType As Enums.mfaccesstype)
    Select Case AccessType
        Case Enums.mfaccesstype.type1
            SystemAccess = New AccessType1()
        Case Enums.mfaccesstype.type2
            SystemAccess = New AccessType2()
    End Select
End Sub

Public Sub GotoScreen(ByVal ScreenName As String, Optional ByVal Window As String = "")
    SystemAccess.GoToScreen(ScreenName)
End Sub
End Class

Public Class AccessType1
Private Type1Object As Object = CreateObject("comobj.Type1Object")

Sub New()
    'new session housekeeping
End Sub

Public Sub GotoScreen(ByVal ScreenName As String)
    'specialize Type1 method for going to 'ScreenName' here
End Sub

End Class


Public Class AccessType2
Private Type2Object As Object = CreateObject("comobj.Type2Object")

Sub New()
    'new session housekeeping
End Sub

Public Sub GotoScreen(ByVal ScreenName As String)
    'specialized Type2 method for going to 'ScreenName' here
End Sub
End Class
4

1 回答 1

1

你很亲密。

首先定义通用接口:

Public Interface ISession 'could be abstract class instead if there is common code you'd like to share between the subclasses

   Sub GotoScreen(ByVal ScreenName As String) 

End Interface

其次,定义子类:

Public Class Type1Session Implements ISession

   Private innerComObj As SomeComObject

   Public Sub GotoScreen(ByVal ScreenName As String)
      'type1-specific code using innerComObj
   End Sub 

End Class

Public Class Type2Session Implements ISession

   Private anotherComObj As SomeOtherComObject

   Public Sub GotoScreen(ByVal ScreenName As String)
      'type2-specific code using anotherComObj
   End Sub 

End Class

然后,您可以创建一个工厂,该工厂将采用枚举或其他参数并返回具体的 Type1Session 或 Type1Session。

这可以在专门的课程中完成:

Public Class SessionFactory

   Public CreateSession(criteria) As ISession 
      'select case against your enum or whatever to create and return the specific type
   End Sub

End Class

如果您选择抽象(VB 中的 MustOverride)类而不是接口,则将工厂方法放在超类中也有先例。

最后,COM 是关于接口的,所以如果你控制了 COM 组件源,你可以让它们直接实现通用接口。

于 2012-08-07T02:41:09.263 回答