は、いくつかの可能な解決策に私をリードしています。最も単純なのはスタックを使用している可能性があります。もう一つは、記念碑のパターンを使用しています。しかし、ここでのコマンドパターンについて尋ねたことは、単純な例です。
これは基本的にcodeprojectsの例です。
class Document
{
private List<string> _textArray = new List<string>();
public void Write(string text)
{
_textArray.Add(text);
}
public void Erase(string text)
{
_textArray.Remove(text);
}
public void Erase(int textLevel)
{
_textArray.RemoveAt(textLevel);
}
public string ReadDocument()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach(string text in _textArray)
sb.Append(text);
return sb.ToString();
}
}
abstract class Command
{
abstract public void Redo();
abstract public void Undo();
}
class DocumentEditCommand : Command
{
private Document _editableDoc;
private string _text;
public DocumentEditCommand(Document doc, string text)
{
_editableDoc = doc;
_text = text;
_editableDoc.Write(_text);
}
override public void Redo()
{
_editableDoc.Write(_text);
}
override public void Undo()
{
_editableDoc.Erase(_text);
}
}
class DocumentInvoker
{
private List<Command> _commands = new List<Command>();
private Document _doc = new Document();
public void Redo(int level)
{
Console.WriteLine("---- Redo {0} level ", level);
((Command)_commands[ level ]).Redo();
}
public void Undo(int level)
{
Console.WriteLine("---- Undo {0} level ", level);
((Command)_commands[ level ]).Undo();
}
public void Write(string text)
{
DocumentEditCommand cmd = new
DocumentEditCommand(_doc,text);
_commands.Add(cmd);
}
public string Read()
{
return _doc.ReadDocument();
}
}
コマンドパターンの使用。
私たちは、documentinvoker(コマンドパターンを実装する)のインスタンスに対して2つの "アクション"を行います。
DocumentInvoker instance = new DocumentInvoker();
instance.Write("This is the original text.");
instance.Write(" Here is some other text.");
これらの操作を元に戻すことができます。
instance.Undo(1);
文書内のテキストは次のようになります。
---- Undo 1 level
This is the original text.
今、私たちは、テキストがなります
instance.Redo(1);
この操作をやり直すことができます。
---- Redo 1 level
This is the original text. Here is some other text.
明らかに、必要に応じてこれを変更する必要があります。もう少し説明が必要な場合は、http://www.codeproject.com/KB/books/DesignPatterns.aspxの記事をご覧ください。
とても簡単です!本当にありがとう! –