Home > Patterns and Practices > Memento Pattern

Memento Pattern

Memento Pattern

Problem:

The GoF intent is “Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.” Let us dissect this statement further for easier understanding: “Externalize an object’s internal state” contradicts encapsulation. So, how do we achieve this without violating encapsulation is the problem.

Solution:

The Memento pattern comes to rescue here. The GoF Class structure diagram is as follows:

Assume we have an Employee class and suppose we can save this Employee details to the database. We need to support an Undo feature, which should revert the Employee details to its original state. How should we do this?

 

public class Employee // Originator

{

            private string _name;

            private string _departmentCode;

            private decimal _salary;

 

            public string Name

            {

                        get { return _name; }

                        set { _name = value; }             

            }

 

            public string DepartmentCode

            {

                        get { return _departmentCode; }

                        set { _departmentCode = value; }

            }

 

            public decimal Salary

            {

                        get { return _salary; }

                        set { _salary = value; }

            }

 

            public class EmployeeMemento // Memento

            {

                        private string _mementoName;

                        private string _mementoDepartmentCode;

                        private decimal _mementoSalary;

                        private Employee _employee;

 

                        public void SetState(Employee employee)

                        {

                                    _employee = employee;

                                    _mementoName = employee._name;

                                    _mementoDepartmentCode = employee._departmentCode;

                                    _mementoSalary = employee._salary;

                        }

 

                        public void GetState()

                        {

                                    _employee._name = _mementoName;

                                    _employee._departmentCode = _mementoDepartmentCode;

                                    _employee._salary = _mementoSalary;

                        }

            }

 

            public Employee.EmployeeMemento CreateMemento()

            {

                        Employee.EmployeeMemento memento = new Employee.EmployeeMemento();

                        memento.SetState(this);

                        return memento;

            }

 

            public void SetMemento(Employee.EmployeeMemento memento)

            {

                        memento.GetState();

            }

 

            public override string ToString()

            {

                        return _name + " is from " + _departmentCode + " and earns $" + _salary.ToString();

            }

 

}

 

public class CareTaker

{

            public static void TestMemento()

            {

                        Employee emp = new Employee();

                        emp.Name = "Sendhil";

                        emp.DepartmentCode = "ISTG";

                        emp.Salary = 1000;

                        Console.WriteLine("Before Save");

                        Console.WriteLine(emp.ToString());

                        Employee.EmployeeMemento memento = emp.CreateMemento();

                        emp.Salary = 2000;

                        Console.WriteLine("After Save");

                        Console.WriteLine(emp.ToString());

                        emp.SetMemento(memento);

                        Console.WriteLine("After undo");

                        Console.WriteLine(emp.ToString());

            }

}

 
Advertisements
  1. No comments yet.
  1. November 19, 2010 at 11:00 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: