Click here to Skip to main content
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 2:44am
Edited 21-Mar-13 2: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 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 21 Mar 2013
Copyright © CodeProject, 1999-2014
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