Created
April 3, 2018 14:42
-
-
Save miteshsureja/d8cb9858d40ae89d092a22078d5949a6 to your computer and use it in GitHub Desktop.
Memento Design Pattern Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace MementoPattern | |
{ | |
//Originator class | |
public class Backup | |
{ | |
public string BackupName { get; set; } | |
public DateTime BackupDate { get; set; } | |
public Backup(string name, DateTime date) | |
{ | |
BackupName = name; | |
BackupDate = date; | |
} | |
public Memento CreateBackup() | |
{ | |
Memento m = new Memento(); | |
m.SetBackup(BackupName, BackupDate); | |
return m; | |
} | |
public void RestoreBackup(Memento m) | |
{ | |
Backup b = m.RestoreBackup(); | |
BackupName = b.BackupName; | |
BackupDate = b.BackupDate; | |
} | |
public override string ToString() | |
{ | |
return string.Format("Backup Name - {0}, Date - {1}", BackupName, BackupDate.ToString("dd-MM-yyyy")); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace MementoPattern | |
{ | |
//caretaker class | |
public class BackupManager | |
{ | |
List<Memento> mementoList = new List<Memento>(); | |
public void CreateBackup(Backup b) | |
{ | |
Console.WriteLine("{0} created.", b.BackupName); | |
mementoList.Add(b.CreateBackup()); | |
} | |
public void RestoreBackup(string backupName, Backup b) | |
{ | |
b.RestoreBackup(mementoList.Find(x => x.b.BackupName.Equals(backupName))); | |
Console.WriteLine("{0} restored.", b.BackupName); | |
} | |
public void ListAllBackups() | |
{ | |
Console.WriteLine("\nBackups available for restore - {0}\n", string.Concat(mementoList.Select(x => x.b.BackupName.ToString() + ","))); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace MementoPattern | |
{ | |
//Memento class | |
public class Memento | |
{ | |
public Backup b; | |
public void SetBackup(string name, DateTime date) | |
{ | |
b = new Backup(name, date); | |
} | |
public Backup RestoreBackup() | |
{ | |
return b; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace MementoPattern | |
{ | |
class Program | |
{ | |
//Client entry point | |
static void Main(string[] args) | |
{ | |
BackupManager bm = new BackupManager(); | |
Backup b = new Backup("Backup1", DateTime.Now.AddDays(-10)); | |
Console.WriteLine(b.ToString()); | |
bm.CreateBackup(b); | |
Backup b1 = new Backup("Backup2", DateTime.Now.AddDays(-8)); | |
Console.WriteLine(b1.ToString()); | |
bm.CreateBackup(b1); | |
Backup b2 = new Backup("Backup3", DateTime.Now.AddDays(-5)); | |
Console.WriteLine(b2.ToString()); | |
bm.CreateBackup(b2); | |
Backup b3 = new Backup("Backup4", DateTime.Now.AddDays(-2)); | |
Console.WriteLine(b3.ToString()); | |
bm.CreateBackup(b3); | |
bm.ListAllBackups(); | |
bm.RestoreBackup("Backup2",b); | |
Console.WriteLine(b.ToString()); | |
bm.RestoreBackup("Backup1", b2); | |
Console.WriteLine(b2.ToString()); | |
Console.Read(); | |
} | |
} | |
} |
Author
miteshsureja
commented
Apr 3, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment