该实例基于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<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;
}
}
}
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
<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
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";
}
}
}
}
演示完毕,有兴趣的同志研究一下。