memento,pattern

Erhan 8/7/2016 0

Memento Pattern

C#
Challenge
You are working on a Drawing application which allows the user to draw lines on a
Canvas. The user should be able to Save the state on particular intervals so any
further mistakes could be undone. You need to provide a solution for the problem.
Definition
GoF Definition: "Without violating encapsulation, capture and externalize an object’s
internal state so that the object can be restored to this state later"
Implementation
The above problem can be solved using the Memento Pattern. This pattern as said
in the definition saves the internal state of the object for restoring later. The object
state is saved without violating Encapsulation.
Drawing.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace MementoPattern
{
/// Class having Line drawing functionalities
publicclassDrawing
    {
privateGraphics _graphics;

public Drawing(Panel panel)
        {
            _graphics = panel.CreateGraphics();
        }

/// Internal State
privateList< Line> _list = new List<Line >();

publicvoid AddLine(Point start, Point end)
        {
            _list.Add( newLine() { Start = start, End = end });

            DrawLines();
        }

privatevoid DrawLines()
        {
            _graphics.Clear( Color.Navy);

foreach(Line line in _list)
                _graphics.DrawLine( Pens.Yellow, line.Start, line.End);
        }

/// Create snapshot of current State
publicMemento CreateMemento()
        {
returnnewMemento() { State = _list };
        }

/// Restore State to given Memento
publicvoid RestoreMemento(Memento memento)
        {
            _list = memento.State asList< Line>;

            DrawLines();
        }
    }

// Class holding Line information
publicclassLine
    {
publicPoint Start, End;
    }
}



Memento.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MementoPattern
{
publicclassMemento
    {
publicList< Line> _state;

publicList< Line> State
        {
get { return _state; }
set
            {
                _state = newList< Line>();
foreach(Line line invalue)
                    _state.Add(line);
            }
        }
    }
}



MainForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MementoPattern
{
publicpartialclassMainForm : Form
    {
privatePoint _start, _end;
privateDrawing _drawing;

public MainForm()
        {
            InitializeComponent();

            _drawing = newDrawing(panel1);
        }

privatevoid panel1_MouseDown(object sender, MouseEventArgs e)
        {
            _start = e.Location;
        }

privatevoid panel1_MouseUp(object sender, MouseEventArgs e)
        {
            _end = e.Location;

            _drawing.AddLine(_start, _end);
        }

privateMemento _memento;

privatevoid button1_Click(object sender, EventArgs e)
        {
            _memento = _drawing.CreateMemento();
        }

privatevoid button3_Click(object sender, EventArgs e)
        {
if (_memento != null )
                _drawing.RestoreMemento(_memento);

else
MessageBox.Show("Invalid Memento!" );
        }
    }
}
 

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments