Real Time Interface - 2

Why use Interface
Code :

Public Interface IStorage
    Function Save() As Boolean
    Function Load() As Boolean
End Interface

Public Class DBStorage
    Implements IStorage

    Public Function Load() As Boolean Implements IStorage.Load
        Console.WriteLine("Trigger from DBStorage : Load ")
        Return True
    End Function

    Public Function Save() As Boolean Implements IStorage.Save
        Console.WriteLine("Trigger from DBStorage : Save ")
        Return True
    End Function
End Class

Public Class FileStorage
    Implements IStorage

    Public Function Load() As Boolean Implements IStorage.Load
        Console.WriteLine("Trigger from FileStorage : Load ")
        Return True
    End Function

    Public Function Save() As Boolean Implements IStorage.Save
        Console.WriteLine("Trigger from FileStorage : Save ")
        Return True
    End Function
End Class

Public Class CacheStorage
    Implements IStorage

    Public Function Load() As Boolean Implements IStorage.Load
        Console.WriteLine("Trigger from CacheStorage : Load ")
        Return True
    End Function

    Public Function Save() As Boolean Implements IStorage.Save
        Console.WriteLine("Trigger from CacheStorage : Save ")
        Return True
    End Function
End Class



FrmMain :
Public Class frmMain

   Private Sub btnDBStorage_Click(sender As Object, e As EventArgs) Handles
btnDBStorage.Click, btnFileStorage.Click, btnCacheStorage.Click

        Dim IStorage As IStorage = Nothing

        If sender.Name = btnDBStorage.Name Then
            Dim DBStorage As New DBStorage
            IStorage = DBStorage

        ElseIf sender.Name = btnFileStorage.Name Then
            Dim FileStorage As New FileStorage
            IStorage = FileStorage

        ElseIf sender.Name = btnCacheStorage.Name Then
            Dim CacheStorage As New CacheStorage
            IStorage = CacheStorage
        End If

        Dim Frm As New frmInterface
        Frm.INIT(IStorage)
        Frm.Show()

    End Sub

End Class

frmInterface :-
Public Class frmInterface

    Dim StorageObject As IStorage

    Public Sub INIT(Storage As IStorage)
        StorageObject = Storage
    End Sub

    Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
        StorageObject.Load()
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        StorageObject.Save()
    End Sub

End Class

Above code will automatically trigger method of relevant class.

If Interface is not used than Take a Look Code Below



Public Class frmNonInterface

 ''' If You Compare this Working with Interface Driven Code This code is much more complex, bigger and Diffcult to maintain in future

 ''' Maintaance in case you need to add new Type of Saving you need to change logic when its Interface no change has to be done.

 ''' Moreover no logic to identify which type of class Method to be fired


    Private Enum SaveType
        DBStorage = 1
        FileStorage = 2
        CacheStorage = 3
    End Enum

    Dim Save As SaveType

    Dim DBStorage As New DBStorage
    Dim FileStorage As New FileStorage
    Dim CacheStorage As New CacheStorage

    Public Sub INIT(Storage As DBStorage)
        DBStorage = Storage
        Save = SaveType.DBStorage
    End Sub

    Public Sub INIT(Storage As FileStorage)
        Save = SaveType.FileStorage
        FileStorage = Storage
    End Sub

    Public Sub INIT(Storage As CacheStorage)
        Save = SaveType.CacheStorage
        CacheStorage = Storage
    End Sub


    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If Save = SaveType.DBStorage Then
            DBStorage.Save()

        ElseIf Save = SaveType.FileStorage Then
            FileStorage.Save()

        ElseIf Save = SaveType.CacheStorage Then
            CacheStorage.Save()
        End If
    End Sub

    Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
        If Save = SaveType.DBStorage Then
            DBStorage.Load()

        ElseIf Save = SaveType.FileStorage Then
            FileStorage.Load()

        ElseIf Save = SaveType.CacheStorage Then
            CacheStorage.Load()
        End If
    End Sub

End Class


Note : Code is more Complex and lengthier compare to interface driven Concept. Any change in will be difficult to manage. Whereas Will much simpler in case of Interface 



Any One Need Source code for Same. Send me Email : SwitinKotian@gmail.com

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)