本文介绍了Memento模式与此代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求在采访中解释纪念品模式。我做了类似的事情。

这段代码看起来像Memento模式吗?它有什么不同?



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);

        }   
    }
}

推荐答案


这篇关于Memento模式与此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:39