The challenge is to come up with the best vb.net design pattern that will allow a switch between 2 different mainframe accessing com objects. All this of course this without having to change the business logic.
What I’ve come up with so far is a factory type approach (see condensed pseudo code below). I have 2 classes that get instantiated based on an enum switch with the same methods and properties but with different underlying object specific methods.
Am I on the right track or should I take another approach? Thanks for any help.
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
You are close.
First define the common interface:
Second, define the subclasses:
You could then create a factory that would take in an enum or other arg and return the concrete Type1Session or Type1Session.
This can be done in a dedicated class:
There is also precident for putting the factory method in the superclass if you go for an abstract (MustOverride in VB) class instead of an interface.
Finally, COM is all about interfaces so if you had control of the COM component source you could have them implement the common interface directly.