Click here to Skip to main content
15,886,016 members
Articles / Programming Languages / C#

Practical approach to Memento design pattern

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
28 Dec 2012CPOL4 min read 32.8K   354   11  
This article explains the implementation of undo/redo using a Memento Pattern.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MementoPattern
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Employee _emp;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _emp = new Employee("Praveen", "Raghuvanshi", 1);
            this.buttonUndo.IsEnabled = this.buttonRedo.IsEnabled = false;
            Update();
        }
        
        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            Save();
        }

        private void buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            Revert();
        }

        private void buttonUndo_Click(object sender, RoutedEventArgs e)
        {
            SaveAndUpdateState();
            EnableUndo(false);
        }

        private void buttonRedo_Click(object sender, RoutedEventArgs e)
        {
            SaveAndUpdateState();
            EnableUndo(true);
        }

        private void Update()
        {
            textBoxFirstName.Text = _emp.FirstName;
            textBoxLastName.Text = _emp.LastName;
            textBoxId.Text = _emp.Id.ToString();
        }

        private void Save()
        {
            CareTaker.Instance.EmployeeMemento = _emp.SaveEmployee();

            _emp.FirstName = textBoxFirstName.Text;
            _emp.LastName = textBoxLastName.Text;
            _emp.Id = Convert.ToUInt16(textBoxId.Text);
            
            // Reset the undo/redo button
            EnableUndo(true);
        }

        private void Revert()
        {
            Update();
        }

        private void SaveAndUpdateState()
        {
            var empMemento = _emp.SaveEmployee();
            _emp.RestoreEmployee(CareTaker.Instance.EmployeeMemento);
            Update();
            CareTaker.Instance.EmployeeMemento = empMemento;
        }

        private void EnableUndo(bool isEnableUndo)
        {
            this.buttonUndo.IsEnabled = isEnableUndo;
            this.buttonRedo.IsEnabled = !isEnableUndo;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Harman International
India India
I am a Microsoft .Net developer having experience in developing enterprise applications using technologies such as WPF, Winform. Always look towards making the life simple by developing GUI based applications.

Comments and Discussions