Click here to Skip to main content
15,886,069 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 241.7K   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;
using System.Drawing.Imaging;

namespace ShapeLib
{
    [Serializable]
    public abstract class Shape
    {
        protected Rectangle bound;
        protected Color color;

        public Rectangle Bound
        {
            get { return bound; }
        }

        public Shape(Color color, Rectangle bound)
        {
            this.bound = bound;
            this.color = color;
        }
        
        public abstract void Paint(Graphics g);
        
    }

    [Serializable]
    public class EllipseShape : Shape
    {
        public EllipseShape(Color color, Rectangle bound)
            : base(color, bound)
        {
        }

        public override void Paint(Graphics g)
        {
            g.FillEllipse(new SolidBrush(color), bound);
        }

    }

    [Serializable]
    public class TriangleShape : Shape
    {
        public TriangleShape(Color color, Rectangle bound)
            : base(color, bound)
        {
        }

        public override void Paint(Graphics g)
        {
            g.FillPolygon(new SolidBrush(color), new Point[] {
                new Point((bound.Left + bound.Right) / 2, bound.Top),
                new Point(bound.Left, bound.Bottom),
                new Point(bound.Right, bound.Bottom) });
        }

    }

    [Serializable]
    public class RectangleShape : Shape
    {
        public RectangleShape(Color color, Rectangle rect)
            : base(color, rect)
        {
        }

        public override void Paint(Graphics g)
        {
            g.FillRectangle(new SolidBrush(color), bound);
        }
    }

}

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