Click here to Skip to main content
11,583,555 members (66,156 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Design pattern
I was asked to explain memento pattern in an interview. I did something like this.
Does this code look like Memento pattern? Where is it different?

namespace ConsoleApplication1
{
    class Originator:ICloneable
    {
        public string a;
        public int b;
 
        public object Clone()
        {
            Originator o = new Originator();
            o.a = this.a;
            o.b = this.b;
            return o;
        }
    }
 
    class CareTaker
    {
        List<Originator> l = new List<Originator>();
 
        public void SaveMemento(Originator o)
        {
             l.Add((Originator)o.Clone());
        }
 
        public Originator RetrieveMemento(int i)
        {
            return l[i];
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            CareTaker c = new CareTaker();
            Originator o = new Originator();
            c.SaveMemento(o);
 
        }   
    }
}
Posted 21-Mar-13 1:44am
Edited 21-Mar-13 1:46am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

If you were asked to "explain" the design pattern then there was no need to write any code - you'd have been better off putting in words what the design pattern tries to achieve and possible adding a UML diagram.

Failing that you could have put some comments in your code to demonstrate your knowledge of the pattern.

Your Main function should actually show a change of state and then roll-back to the previous state to fully demonstrate the pattern.

Have a look at this example for comparison http://www.dofactory.com/Patterns/PatternMemento.aspx[^]
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 390
1 Dave Kreskowiak 230
2 Sergey Alexandrovich Kryukov 210
3 virusstorm 209
4 Richard Deeming 180
0 OriginalGriff 1,523
1 Sergey Alexandrovich Kryukov 995
2 Abhinav S 696
3 Dave Kreskowiak 621
4 Suvendu Shekhar Giri 568


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100