备忘录模式-C#实现

该实例基于WPF实现,直接上代码,下面为三层架构的代码。

目录

一 Model

二 View

三 ViewModel


一 Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.备忘录模式
{
    //3,负责恢复状态
    internal class CareTaker
    {
        private List mementoList = new List();

        public void add(Memento memento)
        {
            mementoList.Add(memento);
        }

        public Memento get(int index)
        {
            if (index < mementoList.Count)
            {
                return mementoList[index];
            }
            else
            {
                return null;
            }

        }

        public List GetMementos()
        {
            return mementoList;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.备忘录模式
{
    //1,创建包含要被恢复的对象的状态
    internal class Memento
    {
        private string state;

        public Memento(string state)
        {
            this.state = state;
        }

        public string State
        {
            get => state;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.备忘录模式
{
    //2,负责在对象中存储状态
    internal class Originator
    {
        private string state;

        public string State { get => state; set => state = value; }


        public Memento saveStateToMemento()
        {
            return new Memento(state);
        }


        public void getStateFromMemento(Memento memento)
        {
            state = memento.State;
        }
    }
}

二 View


    
        
            
            
        
        

        
            
                
                
                
                
            
            

备忘录模式-C#实现_第1张图片

三 ViewModel

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 设计模式练习.Model.备忘录模式;

namespace 设计模式练习.ViewModel.备忘录模式
{
    partial class NoteWindow_ViewModel : ObservableRecipient
    {

        Originator originator = new Originator();
        CareTaker careTaker = new CareTaker();
        int index = -1;

        [ObservableProperty]
        private string res;

        [RelayCommand]
        private void execute()
        {
            int temp = careTaker.GetMementos().Count;

            if (temp == 0)
            {
                Res = "当前没有做任何操作,无记录";
            }

            if (index > 0)
            {
                index--;
                originator.getStateFromMemento(careTaker.get(index));
                Res = originator.State;
            }

        }

        [RelayCommand]
        private void execute2()
        {
            int temp = careTaker.GetMementos().Count;

            if (temp == 0)
            {
                Res = "当前没有做任何操作,无记录";
            }


            if (index >= 0 && index < temp - 2)
            {
                index++;
                originator.getStateFromMemento(careTaker.get(index));
                Res = originator.State;
            }
        }

        [RelayCommand]
        private void execute3()
        {
            originator.State = "开始准备筷子。。。";
            careTaker.add(originator.saveStateToMemento());
            index++;

            originator.State = "开始准备碗。。。";
            careTaker.add(originator.saveStateToMemento());
            index++;

            originator.State = "开始吃饭。。。";
            careTaker.add(originator.saveStateToMemento());
            index++;

            Res = "添加操作执行完成!!!";
        }

        [RelayCommand]
        private void execute4()
        {
            Res = "";

            if (index == -1)
            {
                Res = "暂时无任何操作记录";
                return;
            }

            foreach (Memento item in careTaker.GetMementos())
            {
                Res += item.State + "\r\n";
            }
        }
    }
}

演示完毕,有兴趣的同志研究一下。

你可能感兴趣的:(C#,备忘录模式)