Command 在一个对象中封装一个请求
菜单命令

ShowCommand
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Windows.Forms;
5
6
namespace Gof.Test.Command
7

{
8
public class ShowCommand : System.Windows.Forms.Form
9
{
10
public ShowCommand()
11
{
12
MenuItem exitItem = new MenuItem();
13
exitItem.Text = "退出";
14
exitItem.Click += new EventHandler(Excite);
15
16
Menu = new MainMenu();
17
Menu.MenuItems.Add(exitItem);
18
}
19
public void Excite(object o, EventArgs e)
20
{
21
Console.WriteLine("Exit");
22
}
23
}
24
}

客户代码
1
System.Windows.Forms.Application.Run(new ShowCommand());
2
利用命令模式提供一个服务

DataService
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Data;
5
using System.Data.OleDb;
6
using System.Reflection;
7
using System.IO;
8
9
namespace Gof.Test.Command
10

{
11
public delegate object BorrowReader(IDataReader reader);
12
public class DataService
13
{
14
public DataService()
15
{ }
16
17
public static DataTable CreateTable(string select)
18
{
19
return (DataTable) GetDataReader(select,new BorrowReader(CreateTable));
20
}
21
22
internal static DataTable CreateTable(IDataReader reader)
23
{
24
DataTable table = new DataTable();
25
for (int i = 0; i < reader.FieldCount; i++)
26
{
27
table.Columns.Add(reader.GetName(i), reader.GetFieldType(i));
28
}
29
30
while (reader.Read())
31
{
32
DataRow dr = table.NewRow();
33
for (int i = 0; i < reader.FieldCount; i++)
34
{
35
dr[i] = reader.GetValue(i);
36
}
37
table.Rows.Add(dr);
38
}
39
return table;
40
}
41
42
public static object GetDataReader(string sel, BorrowReader borrow)
43
{
44
try
45
{
46
using (OleDbConnection con = CreateConnection())
47
{
48
con.Open();
49
OleDbCommand com = new OleDbCommand();
50
com.Connection = con;
51
com.CommandText = sel;
52
OleDbDataReader reader = com.ExecuteReader();
53
return borrow(reader);
54
}
55
}
56
catch (Exception ex)
57
{
58
throw ex;//不处理了
59
}
60
}
61
62
public static OleDbConnection CreateConnection()
63
{
64
string fileName = GetFileName("db", "oozinoz.mdb");
65
OleDbConnection con = new OleDbConnection();
66
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + fileName;
67
return con;
68
}
69
70
public static String GetFileName(String dirName, String fileName)
71
{
72
String path;
73
// Can we find the file using the OOZINOZ environment variable?
74
String oozinozBase = Environment.GetEnvironmentVariable("OOZINOZ");
75
if (oozinozBase != null)
76
{
77
path = Path.Combine(Path.Combine(oozinozBase, dirName), fileName);
78
if (File.Exists(path))
79
{
80
return path;
81
}
82
}
83
// How 'bout relative to where the bin files are?
84
Assembly a = Assembly.GetAssembly(typeof(DataService));
85
DirectoryInfo thisDir = Directory.GetParent(a.Location);
86
DirectoryInfo parentDir = Directory.GetParent(thisDir.FullName);
87
path = Path.Combine(
88
parentDir.FullName,
89
dirName + Path.DirectorySeparatorChar + fileName);
90
if (File.Exists(path))
91
{
92
return path;
93
}
94
// Ok, how 'bout in the top-level directory?
95
path = Path.Combine(Path.Combine(@"\ConsoleApplication2", dirName), fileName);
96
if (File.Exists(path))
97
{
98
return path;
99
}
100
// dang
101
throw new Exception("FileFinder.GetFileName() cannot find " + fileName + " in directory " + dirName);
102
103
}
104
}
105
}
命令模式的钩子

命令模式钩子
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
namespace Gof.Test.Command
6

{
7
public delegate void BeforDoSomething();
8
9
public class DelegateForHook
10
{
11
public event BeforDoSomething befordosomething;
12
13
public void Begin()
14
{
15
Console.WriteLine("Begin");
16
}
17
18
public void DoSomething()
19
{
20
Console.WriteLine("Do Something");
21
}
22
23
public void End()
24
{
25
Console.WriteLine("End");
26
}
27
28
public void DoWork()
29
{
30
Begin();
31
if (befordosomething != null)
32
{
33
befordosomething();
34
}
35
DoSomething();
36
End();
37
}
38
}
39
}

客户代码
1
DelegateForHook delegateforhook = new DelegateForHook();
2
delegateforhook.befordosomething += new BeforDoSomething(MyBeforDoSomethingMethod);
3
delegateforhook.DoWork();
4
Console.ReadKey();
The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and suppoert undoable operations.
1
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Windows.Forms;5

6
namespace Gof.Test.Command7


{8
public class ShowCommand : System.Windows.Forms.Form9

{10
public ShowCommand()11

{12
MenuItem exitItem = new MenuItem();13
exitItem.Text = "退出";14
exitItem.Click += new EventHandler(Excite);15
16
Menu = new MainMenu();17
Menu.MenuItems.Add(exitItem);18
}19
public void Excite(object o, EventArgs e)20

{21
Console.WriteLine("Exit");22
}23
}24
}1
System.Windows.Forms.Application.Run(new ShowCommand());2

1
using System;2
using System.Collections.Generic;3
using System.Text;4
using System.Data;5
using System.Data.OleDb;6
using System.Reflection;7
using System.IO;8

9
namespace Gof.Test.Command10


{11
public delegate object BorrowReader(IDataReader reader);12
public class DataService13

{14
public DataService()15

{ }16

17
public static DataTable CreateTable(string select)18

{19
return (DataTable) GetDataReader(select,new BorrowReader(CreateTable));20
}21

22
internal static DataTable CreateTable(IDataReader reader)23

{24
DataTable table = new DataTable();25
for (int i = 0; i < reader.FieldCount; i++)26

{27
table.Columns.Add(reader.GetName(i), reader.GetFieldType(i));28
}29

30
while (reader.Read())31

{32
DataRow dr = table.NewRow();33
for (int i = 0; i < reader.FieldCount; i++)34

{35
dr[i] = reader.GetValue(i);36
}37
table.Rows.Add(dr);38
}39
return table;40
}41

42
public static object GetDataReader(string sel, BorrowReader borrow)43

{44
try45

{46
using (OleDbConnection con = CreateConnection())47

{48
con.Open();49
OleDbCommand com = new OleDbCommand();50
com.Connection = con;51
com.CommandText = sel;52
OleDbDataReader reader = com.ExecuteReader();53
return borrow(reader);54
}55
}56
catch (Exception ex)57

{58
throw ex;//不处理了59
}60
}61

62
public static OleDbConnection CreateConnection()63

{64
string fileName = GetFileName("db", "oozinoz.mdb");65
OleDbConnection con = new OleDbConnection();66
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + fileName;67
return con;68
}69

70
public static String GetFileName(String dirName, String fileName)71

{72
String path;73
// Can we find the file using the OOZINOZ environment variable?74
String oozinozBase = Environment.GetEnvironmentVariable("OOZINOZ");75
if (oozinozBase != null)76

{77
path = Path.Combine(Path.Combine(oozinozBase, dirName), fileName);78
if (File.Exists(path))79

{80
return path;81
}82
}83
// How 'bout relative to where the bin files are?84
Assembly a = Assembly.GetAssembly(typeof(DataService));85
DirectoryInfo thisDir = Directory.GetParent(a.Location);86
DirectoryInfo parentDir = Directory.GetParent(thisDir.FullName);87
path = Path.Combine(88
parentDir.FullName,89
dirName + Path.DirectorySeparatorChar + fileName);90
if (File.Exists(path))91

{92
return path;93
}94
// Ok, how 'bout in the top-level directory?95
path = Path.Combine(Path.Combine(@"\ConsoleApplication2", dirName), fileName);96
if (File.Exists(path))97

{98
return path;99
}100
// dang101
throw new Exception("FileFinder.GetFileName() cannot find " + fileName + " in directory " + dirName);102

103
}104
}105
}1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Gof.Test.Command6


{7
public delegate void BeforDoSomething();8

9
public class DelegateForHook10

{11
public event BeforDoSomething befordosomething;12

13
public void Begin()14

{15
Console.WriteLine("Begin");16
}17

18
public void DoSomething()19

{20
Console.WriteLine("Do Something");21
}22

23
public void End()24

{25
Console.WriteLine("End");26
}27

28
public void DoWork()29

{30
Begin();31
if (befordosomething != null)32

{33
befordosomething();34
}35
DoSomething();36
End();37
}38
}39
}1
DelegateForHook delegateforhook = new DelegateForHook();2
delegateforhook.befordosomething += new BeforDoSomething(MyBeforDoSomethingMethod);3
delegateforhook.DoWork();4
Console.ReadKey();

浙公网安备 33010602011771号