You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.5 KiB
55 lines
1.5 KiB
using Memonto.Mementos;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Memonto.Originators
|
|
{
|
|
internal class Originator
|
|
{
|
|
private string _state;
|
|
|
|
public Originator(string state)
|
|
{
|
|
_state = state;
|
|
Console.WriteLine($"Originator: My initial state is: {state}");
|
|
}
|
|
|
|
public void DoSomething()
|
|
{
|
|
Console.WriteLine("Originator: I'm doing something important.");
|
|
_state = GenerateRandomString(30);
|
|
Console.WriteLine($"Originator: And my state has changed to: {_state}");
|
|
}
|
|
|
|
private string GenerateRandomString(int length = 10)
|
|
{
|
|
string allowedSymbols = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
string result = string.Empty;
|
|
|
|
while (length > 0 )
|
|
{
|
|
result += allowedSymbols[new Random().Next(0, allowedSymbols.Length)];
|
|
length--;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public IMemento Save()
|
|
{
|
|
return new ConcreteMemento( _state );
|
|
}
|
|
|
|
public void Restore(IMemento memento )
|
|
{
|
|
if (!(memento is ConcreteMemento))
|
|
throw new Exception("Unknown memento class " + memento.ToString());
|
|
|
|
_state = memento.GetState();
|
|
Console.WriteLine($"Originator: My state has changed to: {_state}");
|
|
}
|
|
}
|
|
}
|
|
|