Saturday, July 11, 2015

Memento Design Pattern

Memento pattern is a way to preserve internal state of an object without violating encapsulation.

It is a widely used behavioral pattern.

There are three section of this pattern

Originator:
This is the original object for that memento get created
Memento: This is the copy of original class to preserve the original state of originator object.
Care Taker: This object holds memento object to restore to originator at any point of time.


Requirement:
During changing of object state if something goes wrong, object can be reverted to original state.

Use:
Original state can be re-stored at any point of time during application running.

Problem Solved:
Generally when required people restore object to its original state from database. So by using this pattern no need to query database to restore object.


Sample Code:

    
/// <summary>
    /// Original class
    /// </summary>
    public class Origintor
    {
        private string name;
        private string mobile;
        private string eMail;

        public Origintor(string _name, string _mobile, string _email)
        {
            name = _name;
            mobile = _mobile;
            eMail = _email;
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public string Mobile
        {
            get
            {
                return mobile;
            }
            set
            {
                mobile = value;
            }
        }

        public string Email
        {
            get
            {
                return eMail;
            }
            set
            {
                eMail = value;
            }
        }

        public Memento SaveMemento()
        {
            return new Memento(name, mobile, eMail);
        }

        public void RestoreMemento(Memento objMemento)
        {
            this.Name = objMemento.Name;
            this.Mobile = objMemento.Mobile;
            this.Email = objMemento.Email;
        }
    }


    /// <summary>
    /// Backup class
    /// </summary>
    public class Memento
    {
        private string name;
        private string mobile;
        private string eMail;

        public Memento(string _name, string _mobile, string _email)
        {
            name = _name;
            mobile = _mobile;
            eMail = _email;
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public string Mobile
        {
            get
            {
                return mobile;
            }
            set
            {
                mobile = value;
            }
        }

        public string Email
        {
            get
            {
                return eMail;
            }
            set
            {
                eMail = value;
            }
        }

    }

    /// <summary>
    /// Memento value holder class
    /// </summary>
    public static class CareTaker
    {
        private static Memento memento;

        public static Memento Memento
        {
            get
            {
                return memento;
            }
            set
            {
                memento = value;
            }
        }
    }


Client Code:

    public partial class Customer : Form
    {
        Origintor objCustomer;
        public Customer()
        {
            InitializeComponent();
        }

        private void Customer_Load(object sender, EventArgs e)
        {
          objCustomer = new Origintor("Raghu","9827302912","tomargs11@gmail.com");
          DisplayCustomer();   
          CareTaker.Memento = objCustomer.SaveMemento();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            objCustomer.Name = this.txtName.Text ;
            objCustomer.Mobile = this.txtMobile.Text ;
            objCustomer.Email =  this.txtEmail.Text ;

            CareTaker.Memento = objCustomer.SaveMemento();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            objCustomer.RestoreMemento(CareTaker.Memento);
            DisplayCustomer();
        }

        private void DisplayCustomer()
        {
            this.txtName.Text = objCustomer.Name;
            this.txtMobile.Text = objCustomer.Mobile;
            this.txtEmail.Text = objCustomer.Email;
        }
    }



No comments:

Post a Comment

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...