1

課題は、comオブジェクトにアクセスする2つの異なるメインフレーム間の切り替えを可能にする最高のvb.netデザインパターンを考え出すことです。もちろん、これはすべて、ビジネスロジックを変更する必要はありません。

私がこれまでに思いついたのは、ファクトリタイプのアプローチです(以下の要約された擬似コードを参照してください)。同じメソッドとプロパティを使用し、基になるオブジェクト固有のメソッドが異なる列挙型スイッチに基づいてインスタンス化される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 に答える