备忘录模式-C#实现

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

目录

[一 Model](#一 Model)

[二 View](#二 View)

[三 ViewModel](#三 ViewModel)


一 Model

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

        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<Memento> GetMementos()
        {
            return mementoList;
        }
    }
}
cs 复制代码
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;
        }
    }
}
cs 复制代码
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

XML 复制代码
<Window x:Class="设计模式练习.View.备忘录模式.NoteWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:设计模式练习.View.备忘录模式"
        mc:Ignorable="d"
        Title="NoteWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" Text="{Binding Res}" TextWrapping="Wrap"/>

        <Grid Grid.Column="1" >
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Button Content="上一步操作" Command="{Binding executeCommand}" Grid.Row="0"/>
            <Button Content="下一步操作" Command="{Binding execute2Command}" Grid.Row="1"/>
            <Button Content="添加操作" Command="{Binding execute3Command}" Grid.Row="2"/>
            <Button Content="查询所有操作" Command="{Binding execute4Command}" Grid.Row="3"/>
        </Grid>
    </Grid>
</Window>

三 ViewModel

cs 复制代码
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";
            }
        }
    }
}

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

相关推荐
刷帅耍帅3 天前
设计模式-备忘录模式
设计模式·备忘录模式
java_heartLake5 天前
设计模式之备忘录模式
java·设计模式·备忘录模式
John_ToDebug5 天前
设计模式之备忘录模式
c++·设计模式·备忘录模式
林小果17 天前
备忘录模式
java·开发语言·设计模式·备忘录模式
仙魁XAN13 天前
Unity 设计模式 之 行为型模式 -【状态模式】【观察者模式】【备忘录模式】
观察者模式·unity·设计模式·状态模式·备忘录模式
coffee_baby14 天前
撤销与恢复的奥秘:设计模式之备忘录模式详解
java·设计模式·备忘录模式
LB_bei23 天前
设计模式-行为型模式-备忘录模式
设计模式·备忘录模式
程序员与背包客_CoderZ25 天前
C++设计模式——Memento备忘录模式
linux·c语言·开发语言·c++·设计模式·备忘录模式
WineMonk1 个月前
设计模式 18 备忘录模式
设计模式·备忘录模式
不是仙人的闲人1 个月前
C++ 设计模式——备忘录模式
c++·设计模式·备忘录模式