Command命令模式
原始紧耦合的代码:
紧耦合
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 public class Application
19 {
20 public void Show()
21 {
22 Document doc = new Document();
23 doc.ShowText();//依赖具体行为实现
24
25 Graphics graph = new Graphics();
26 graph.ShowGraphics();//依赖具体行为实现
27 }
28 }
29
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 public class Application
19 {
20 public void Show()
21 {
22 Document doc = new Document();
23 doc.ShowText();//依赖具体行为实现
24
25 Graphics graph = new Graphics();
26 graph.ShowGraphics();//依赖具体行为实现
27 }
28 }
29
一种做法:
eg.1
1 using System.Collections;
2 using System.Collections.Generic;
3
4
5 public interface Command
6 {
7 public abstract void Show();
8 public abstract void Undo();
9 public abstract void Redo();
10 }
11
12
13 //虽然成功应用了模式,但是不应该 要求 系统中的所有类都实现上面command接口
14 class Document : Command
15 {
16
17 public override void Show()
18 {
19 //.
20 }
21
22 public override void Undo()
23 {
24 throw new System.NotImplementedException();
25 }
26
27 public override void Redo()
28 {
29 throw new System.NotImplementedException();
30 }
31 }
32
33 class Graphics : Command
34 {
35
36 public override void Show()
37 {
38 //
39 }
40
41 public override void Undo()
42 {
43 throw new System.NotImplementedException();
44 }
45
46 public override void Redo()
47 {
48 throw new System.NotImplementedException();
49 }
50 }
51
52
53 public class Application
54 {
55 Stack<Command> stack;
56 ArrayList<Command> list;
57 public void Show()
58 {
59 foreach (Command c in list)
60 {
61 c.Show();
62 }
63 }
64
65 public void Undo()
66 {
67 if (canUndo)
68 {
69 Command command = stack.Pop();
70 command.Undo();
71 }
72 }
73
74 public void Redo()
75 {
76 if (canRedo)
77 {
78 Command command = Undo.pop();
79 }
80 }
81 }
1 using System.Collections;
2 using System.Collections.Generic;
3
4
5 public interface Command
6 {
7 public abstract void Show();
8 public abstract void Undo();
9 public abstract void Redo();
10 }
11
12
13 //虽然成功应用了模式,但是不应该 要求 系统中的所有类都实现上面command接口
14 class Document : Command
15 {
16
17 public override void Show()
18 {
19 //.
20 }
21
22 public override void Undo()
23 {
24 throw new System.NotImplementedException();
25 }
26
27 public override void Redo()
28 {
29 throw new System.NotImplementedException();
30 }
31 }
32
33 class Graphics : Command
34 {
35
36 public override void Show()
37 {
38 //
39 }
40
41 public override void Undo()
42 {
43 throw new System.NotImplementedException();
44 }
45
46 public override void Redo()
47 {
48 throw new System.NotImplementedException();
49 }
50 }
51
52
53 public class Application
54 {
55 Stack<Command> stack;
56 ArrayList<Command> list;
57 public void Show()
58 {
59 foreach (Command c in list)
60 {
61 c.Show();
62 }
63 }
64
65 public void Undo()
66 {
67 if (canUndo)
68 {
69 Command command = stack.Pop();
70 command.Undo();
71 }
72 }
73
74 public void Redo()
75 {
76 if (canRedo)
77 {
78 Command command = Undo.pop();
79 }
80 }
81 }
Command命令模式的做法:
Command命令模式
1
2 //已经存在的类,实现细节,低层实现
3 class Document
4 {
5 public void ShowText()
6 {
7 //
8 }
9 }
10
11 class Graphics
12 {
13 public void ShowGraphics()
14 {
15 //.
16 }
17 }
18
19
20
21 //实现command设计模式--抽象体
22 public interface Command
23 {
24 public void Show();
25
26 public void Undo();
27 public void Redo();
28 }
29
30 //具体化的命令对象---从抽象意义来讲,DocumentCommand表示一个行为.
31 class DocumentCommand : Command
32 {
33 Document document;
34 public DocumentCommand(Document doc)
35 {
36 this.document = doc;
37 }
38 void Command.Show()
39 {
40 document.Show();
41 }
42
43 void Command.Undo()
44 {
45 document.Undo();
46 }
47
48 void Command.Redo()
49 {
50 document.Redo();
51 }
52 }
53
54 class GraphicsCommand : Command
55 {
56 Graphics graph;
57 public GraphicsCommand(Graphics graph)
58 {
59 this.graph = graph;
60 }
61 void Command.Show()
62 {
63 graph.Show();
64 }
65
66 void Command.Undo()
67 {
68 graph.Undo();
69 }
70
71 void Command.Redo()
72 {
73 graph.Redo();
74 }
75
76 }
77
78
79 //应用程序主干/高层抽象
80 public class Application
81 {
82 Stack<Command> stack;
83 ArrayList<Command> list;
84 public void Show()
85 {
86 foreach (Command c in list)
87 {
88 c.Show();
89 }
90 }
91
92 public void Undo()
93 {
94 if (canUndo)
95 {
96 Command command = stack.Pop();
97 command.Undo();
98 }
99 }
100
101 public void Redo()
102 {
103 if (canRedo)
104 {
105 Command command = Undo.pop();
106 }
107 }
108 }
109
110
111
112
1
2 //已经存在的类,实现细节,低层实现
3 class Document
4 {
5 public void ShowText()
6 {
7 //
8 }
9 }
10
11 class Graphics
12 {
13 public void ShowGraphics()
14 {
15 //.
16 }
17 }
18
19
20
21 //实现command设计模式--抽象体
22 public interface Command
23 {
24 public void Show();
25
26 public void Undo();
27 public void Redo();
28 }
29
30 //具体化的命令对象---从抽象意义来讲,DocumentCommand表示一个行为.
31 class DocumentCommand : Command
32 {
33 Document document;
34 public DocumentCommand(Document doc)
35 {
36 this.document = doc;
37 }
38 void Command.Show()
39 {
40 document.Show();
41 }
42
43 void Command.Undo()
44 {
45 document.Undo();
46 }
47
48 void Command.Redo()
49 {
50 document.Redo();
51 }
52 }
53
54 class GraphicsCommand : Command
55 {
56 Graphics graph;
57 public GraphicsCommand(Graphics graph)
58 {
59 this.graph = graph;
60 }
61 void Command.Show()
62 {
63 graph.Show();
64 }
65
66 void Command.Undo()
67 {
68 graph.Undo();
69 }
70
71 void Command.Redo()
72 {
73 graph.Redo();
74 }
75
76 }
77
78
79 //应用程序主干/高层抽象
80 public class Application
81 {
82 Stack<Command> stack;
83 ArrayList<Command> list;
84 public void Show()
85 {
86 foreach (Command c in list)
87 {
88 c.Show();
89 }
90 }
91
92 public void Undo()
93 {
94 if (canUndo)
95 {
96 Command command = stack.Pop();
97 command.Undo();
98 }
99 }
100
101 public void Redo()
102 {
103 if (canRedo)
104 {
105 Command command = Undo.pop();
106 }
107 }
108 }
109
110
111
112