Factory Patten

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim DE As New DataEntry()
        Dim IC As IConnect

        Dim isConnected As Boolean
        Dim strServerType As String

        IC = DE.SourceServer("O")
        isConnected = IC.IsConnected
        strServerType = IC.ServerType


        IC = DE.SourceServer("S")
        isConnected = IC.IsConnected
        strServerType = IC.ServerType


        IC = DE.SourceServer("M")
        isConnected = IC.IsConnected
        strServerType = IC.ServerType

    End Sub

End Class


Public Interface IConnect
    Property IsConnected As Boolean
    Function ServerType() As String
End Interface


Public Class CSQL
    Implements IConnect

    Public Property IsConnected As Boolean Implements IConnect.IsConnected

    Public Function ServerType() As String Implements IConnect.ServerType
        IsConnected = True
        Return "SQL Server"
    End Function

End Class


Public Class COracle
    Implements IConnect

    Public Property IsConnected As Boolean Implements IConnect.IsConnected

    Public Function ServerType() As String Implements IConnect.ServerType
        IsConnected = True
        Return "Oracle"
    End Function
End Class

Public Class CMongoDB
    Implements IConnect

    Public Property IsConnected As Boolean Implements IConnect.IsConnected

    Public Function ServerType() As String Implements IConnect.ServerType
        IsConnected = True
        Return "MongoDB"
    End Function

End Class


Public MustInherit Class DBOperator
    'Abstract Method. When Defined. Inherited Class Must have this Method. Some Thing on lines of Interface
    'This is done to force Method SourceSERVER in all Class Which need this Implementation
    Public MustOverride Function SourceServer(Type As String) As IConnect

End Class


Public Class DataEntry
    Inherits DBOperator

    'Method Return Interface which return Object of Class which is implementing this Interface
    Public Overrides Function SourceServer(Type As String) As IConnect
        If Type = "S" Then
            Return New CSQL

        ElseIf Type = "O" Then
            Return New COracle

        ElseIf Type = "M" Then
            Return New CMongoDB
        End If

        Return Nothing
    End Function

End Class

Comments

Popular posts from this blog

Shared / Static Class in vb.net/C#

Xamarin Forms : PopUp Page with RG.Plugins.Popup

Text was truncated or one or more characters had no match in the target code page.". (SQL Server Import and Export Wizard)