国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c# 備忘錄模式

2020-01-24 03:40:25
字體:
供稿:網(wǎng)友
結(jié)構(gòu)圖:


Memento類:保存狀態(tài)的容器
復(fù)制代碼 代碼如下:

    class Memento
    {
        public string State { get; set; }
        public Memento(string state)
        {
            this.State = state;
        }
    }

Caretaker是保存Memento類:
復(fù)制代碼 代碼如下:

    class Caretaker
    {
        public Memento Memento { get; set; }
    }

Originator類就是需要保存狀態(tài)的類:
復(fù)制代碼 代碼如下:

    class Originator
    {
        public string State { get; set; }
        public Memento CreateMemento()
        {
            return (new Memento(State));
        }
        public void SetMemento(Memento memento)
        {
            State = memento.State;
        }
        public void Show()
        {
            Console.WriteLine("State:" + State);
        }
    }

 主函數(shù)調(diào)用:
復(fù)制代碼 代碼如下:

    class Program
    {
        static void Main(string[] args)
        {
            Originator o = new Originator();
            o.State = "On";
            o.Show();
            Caretaker c = new Caretaker();
            c.Memento = o.CreateMemento();
            o.State = "off";
            o.Show();
            o.SetMemento(c.Memento);
            o.Show();
            Console.ReadKey();
        }
    }
   
上一篇:c# 適配器模式

下一篇:c# 組合模式

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 盐边县| 陇西县| 开平市| 兴宁市| 郎溪县| 房山区| 镇远县| 利辛县| 屏东县| 哈巴河县| 虹口区| 临澧县| 铅山县| 历史| 武清区| 攀枝花市| 新泰市| 盱眙县| 鄢陵县| 西丰县| 北票市| 渝中区| 平和县| 中宁县| 麦盖提县| 平阳县| 冀州市| 苏尼特左旗| 闽侯县| 正安县| 红河县| 重庆市| 天台县| 武乡县| 柳江县| 格尔木市| 晋中市| 客服| 长宁区| 长宁区| 涞水县|