Implementation of Memento Pattern in VB.net

Rajnilari2015
Posted by Rajnilari2015 under VB.NET category on | Points: 40 | Views : 948
What is Memento Pattern?
Sometimes we want to perform undo with the changes. In such case situations memento helps.

Let’s look below code.

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Collections
Imports System.Diagnostics

Namespace TestMemento

Class OriginalObject
Public Property Name() As String
Get
Return m_Name
End Get
Set
m_Name = Value
End Set
End Property
Private m_Name As String
Public Property SetMomento() As Momento
Get
Return m_SetMomento
End Get
Set
m_SetMomento = Value
End Set
End Property
Private m_SetMomento As Momento

Public Sub New(Name As String)
Me.Name = Name
Me.SetMomento = New Momento(Name)
End Sub

Public Sub ChangeData()
Me.Name = "Changed Name"
Console.WriteLine(SetMomento.Name)
End Sub

Public Sub Revert()
Console.WriteLine(SetMomento.Name)
End Sub
End Class

Class Momento
Public Property Name() As String
Get
Return m_Name
End Get
Set
m_Name = Value
End Set
End Property
Private m_Name As String
Public Sub New(Name As String)
Me.Name = Name
End Sub
End Class


Class Program
Private Shared Sub Main(args As String())
Dim obj As New OriginalObject("This is original name")
obj.ChangeData()
obj.Revert()
Console.ReadLine()
End Sub
End Class
End Namespace


In this case the Momento class plays the vital role.It stores the original value and when the ChangeData() is called then the new value is printed. Again when the Revert() is called, then it picks up the value from SetMomento.Name property and the original value retains.

Comments or Responses

Login to post response