主讲:李建忠
来源:
1: //已经存在的,实现细节,低层实现
2: class Document
3: {
4: public void ShowText()
5: {
6: //...
7: }
8: }
9:
10: class Graphics
11: {
12: public void ShowGraphics()
13: {
14: //...
15: }
16: }
17:
18: //实现Command设计模式
19: public interface Command
20: {
21: public void Show();
22: public void Undo();
23: public void Redo();
24: }
25:
26: //具体化的命令对象——从抽象意义来讲,DocumentCommand表示一个行为
27: class DocumentCommand:Command
28: {
29: Document document;
30: public DocumentCommand(Document doc)
31: {
32: this.document=doc;
33: }
34:
35: public void Show()
36: {
37: document.ShowText();
38: }
39:
40: public void Undo()
41: {
42:
43: }
44:
45: public void Redo()
46: {
47:
48: }
49: }
50:
51: class GraphicCommand:Command
52: {
53:
54: }
55:
56: //应用程序主干,高层抽象
57: class Application
58: {
59: Stackstack;
60:
61: public void Show()
62: {
63: foreach(Command c in list)
64: {
65: c.show();
66: }
67: }
68:
69: public void Undo()
70: {
71: if(canUndo)
72: {
73: Command command=stack.pop();
74: command.Undo();
75:
76: undoList.Add(command);
77: }
78: }
79:
80: public void Redo()
81: {
82: if(canRedo)
83: {
84: Command command=undoList.pop();
85: command.Undo();
86: }
87: }
88: }