Click here to Skip to main content
15,896,912 members
Articles / Programming Languages / C#

Generic Memento Pattern for Undo-Redo in C#

Rate me:
Please Sign up or sign in to vote.
4.81/5 (89 votes)
16 Mar 20074 min read 242.3K   5.3K   169  
Improved Memento pattern particularly designed to support undo and redo.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ShapeLib
{
    [Serializable]
    public class ShapePool : IEnumerable<Shape>
    {
        List<Shape> shapes = new List<Shape>();

        public void Paint(System.Drawing.Graphics graphics)
        {
            foreach (Shape s in shapes)
            {
                s.Paint(graphics);
            }
        }

        public Shape this[int index]
        {
            get { return shapes[index]; }
        }

        public void Add(Shape shape)
        {
            shapes.Add(shape);
        }

        public void Insert(int index, Shape shape)
        {
            shapes.Insert(index, shape);
        }

        public void RemoveAt(int index)
        {
            shapes.RemoveAt(index);
        }

        public int IndexOf(Shape shape)
        {
            return shapes.IndexOf(shape);
        }

        public int Count
        {
            get { return shapes.Count; }
        }

        #region IEnumerable<Shape> Members

        public IEnumerator<Shape> GetEnumerator()
        {
            return shapes.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Singapore Singapore
This guy loves computer programming, software design and development. He is interested and specialized in C family languages, especially C#, Java, Objective-C and D Programming Language. Ruby and Python are starting to interest him as well.

Comments and Discussions