(NET) NET (2016)

Memento in VB.NET

Capture and restore an object's internal state

Return


Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later. Memento pattern temporary saves and restores another object's internal state.







Please download project with this source code from https://github.com/ViacheslavUKR/StandardDisignOopPattern



   1:      ' Memento Design Pattern.
   2:      ' See description in //www.vb-net.com/ProgramTheory/Memento.htm
   3:      Class MainApp
   4:          ' Entry point into console application.
   5:          Public Shared Sub Main()
   6:              Dim o As New Originator()
   7:              o.State = "On"
   8:              ' Store internal state
   9:              Dim c As New Caretaker()
  10:              c.Memento = o.CreateMemento()
  11:              ' Continue changing originator
  12:              o.State = "Off"
  13:              ' Restore saved state
  14:              o.SetMemento(c.Memento)
  15:              ' Wait for user
  16:              Console.ReadKey()
  17:          End Sub
  18:      End Class
  19:   
  20:      ' The 'Originator' class
  21:      Class Originator
  22:          Private _state As String
  23:          ' Property
  24:          Public Property State() As String
  25:              Get
  26:                  Return _state
  27:              End Get
  28:              Set(value As String)
  29:                  _state = value
  30:                  Console.WriteLine(Convert.ToString("State = ") & _state)
  31:              End Set
  32:          End Property
  33:          ' Creates memento 
  34:          Public Function CreateMemento() As Memento
  35:              Return (New Memento(_state))
  36:          End Function
  37:          ' Restores original state
  38:          Public Sub SetMemento(memento As Memento)
  39:              Console.WriteLine("Restoring state...")
  40:              State = memento.State
  41:          End Sub
  42:      End Class
  43:   
  44:      ' The 'Memento' class
  45:      Class Memento
  46:          Private _state As String
  47:          ' Constructor
  48:          Public Sub New(state As String)
  49:              Me._state = state
  50:          End Sub
  51:          ' Gets or sets state
  52:          Public ReadOnly Property State() As String
  53:              Get
  54:                  Return _state
  55:              End Get
  56:          End Property
  57:      End Class
  58:   
  59:      ' The 'Caretaker' class
  60:      Class Caretaker
  61:          Private _memento As Memento
  62:          ' Gets or sets memento
  63:          Public Property Memento() As Memento
  64:              Get
  65:                  Return _memento
  66:              End Get
  67:              Set(value As Memento)
  68:                  _memento = value
  69:              End Set
  70:          End Property
  71:      End Class




See also:
Creational Patterns Structural Patterns Behavioral Patterns

Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/ProgramTheory/Memento.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>