CSharp: Chain of Responsibility Pattern

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/// <summary>
 /// Chain of Responsibility  Patterns 责任链模式
 /// 20220918
 /// geovindu,Geovin Du,涂聚文
 /// </summary>
 public abstract class Chain
 {
     /// <summary>
     /// describes how all chains work
     /// </summary>
     private bool hasLink;
     protected Chain chn;
     public Chain()
     {
         hasLink = false;
     }
     /// <summary>
     /// you must implement this in derived classes
     /// </summary>
     /// <param name="mesg"></param>
     public abstract void sendToChain(string mesg);
     /// <summary>
     ///
     /// </summary>
     /// <param name="c"></param>
     public void addToChain(Chain c)
     {
         //add new element to chain
         chn = c;
         hasLink = true;     //flag existence
     }
     /// <summary>
     ///
     /// </summary>
     /// <returns></returns>
     public Chain getChain()
     {
         return chn; //get the chain link
     }
     /// <summary>
     ///
     /// </summary>
     /// <returns></returns>
     public bool hasChain()
     {
         return hasLink;     //true if linked to another
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="mesg"></param>
     protected void sendChain(string mesg)
     {
         //send message on down the chain
         if (chn != null)
             chn.sendToChain(mesg);
     }
 }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/// <summary>
 /// receives color names in chain
 /// Chain of Responsibility  Patterns 责任链模式
 /// 20220918
 /// geovindu,Geovin Du,涂聚文
 /// </summary>
 public class ColorChain : Chain
 {
     private Hashtable colHash;  //color list kept here
     private Panel panel;        //color goes here
     /// <summary>
     ///
     /// </summary>
     /// <param name="pnl"></param>
     public ColorChain(Panel pnl)
     {
         panel = pnl;            //save reference
         //create Hash table to correlate color names
         //with actual Color objects
         colHash = new Hashtable();
         colHash.Add("red", Color.Red);
         colHash.Add("green", Color.Green);
         colHash.Add("blue", Color.Blue);
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="mesg"></param>
     public override void sendToChain(string mesg)
     {
         mesg = mesg.ToLower();
         try
         {
             Color c = (Color)colHash[mesg];
             //if this is a color, put it in the panel
             panel.BackColor = c;
         }
         catch (NullReferenceException e)
         {
             //send on if this doesn't work
             sendChain(mesg);
         }
 
     }
 }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/// <summary>
 /// A simple file handlng class
 /// Chain of Responsibility  Patterns 责任链模式
 /// 20220918
 /// geovindu,Geovin Du,涂聚文
 /// </summary>
 public class csFile
 {
     private string fileName;
     StreamReader ts;
     StreamWriter ws;
     private bool opened, writeOpened;
     /// <summary>
     ///
     /// </summary>
     public csFile()
     {
         init();
     }
     /// <summary>
     ///
     /// </summary>
     private void init()
     {
         opened = false;
         writeOpened = false;
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="file_name"></param>
     public csFile(string file_name)
     {
         fileName = file_name;
         init();
     }
     /// <summary>
     ///
     /// </summary>
     /// <returns></returns>
     public bool exists()
     {
         return File.Exists(fileName);
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="file_name"></param>
     /// <returns></returns>
     public bool OpenForRead(string file_name)
     {
         fileName = file_name;
         try
         {
             ts = new StreamReader(fileName);
             opened = true;
         }
         catch (FileNotFoundException)
         {
             return false;
         }
         return true;
     }
     /// <summary>
     ///
     /// </summary>
     /// <returns></returns>
     public bool OpenForRead()
     {
         return OpenForRead(fileName);
     }
     /// <summary>
     ///
     /// </summary>
     /// <returns></returns>
     public string readLine()
     {
         return ts.ReadLine();
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="s"></param>
     public void writeLine(string s)
     {
         ws.WriteLine(s);
     }
     /// <summary>
     ///
     /// </summary>
     public void close()
     {
         if (opened)
             ts.Close();
         if (writeOpened)
             ws.Close();
     }
     /// <summary>
     ///
     /// </summary>
     /// <returns></returns>
     public bool OpenForWrite()
     {
         return OpenForWrite(fileName);
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="file_name"></param>
     /// <returns></returns>
     public bool OpenForWrite(string file_name)
     {
         try
         {
             ws = new StreamWriter(file_name);
             fileName = file_name;
             writeOpened = true;
             return true;
         }
         catch (FileNotFoundException)
         {
             return false;
         }
     }
     /// <summary>
     ///
     /// </summary>
     /// <returns></returns>
     public string getRootName()
     {
         int i = fileName.LastIndexOf("\\");
         string root = fileName;
         if (i > 0)
         {
             root = fileName.Substring(i + 1);
         }
         return root;
     }
 }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// <summary>
 /// Summary description for FileChain.
 /// Chain of Responsibility  Patterns 责任链模式
 /// 20220918
 /// geovindu,Geovin Du,涂聚文
 /// </summary>
 public class FileChain : Chain
 {
     ListBox flist;
     /// <summary>
     ///
     /// </summary>
     /// <param name="lb"></param>
     public FileChain(ListBox lb)
     {
         flist = lb;
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="mesg"></param>
     public override void sendToChain(string mesg)
     {
         //if the string matches any part of a filename
         //put those filenames in the file list box
         string[] files;
         string fname = mesg + "*.*";
         files = Directory.GetFiles(Directory.GetCurrentDirectory(), fname);
         //add them all to the listbox
         if (files.Length > 0)
         {
             for (int i = 0; i < files.Length; i++)
             {
                 csFile vbf = new csFile(files[i]);
                 flist.Items.Add(vbf.getRootName());
             }
         }
         else
         {
             if (hasChain())
             {
                 chn.sendToChain(mesg);
             }
         }
     }
 }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/// <summary>
 /// Summary description for ImageChain.
 /// Chain of Responsibility  Patterns 责任链模式
 /// 20220918
 /// geovindu,Geovin Du,涂聚文
 /// </summary>
 public class ImageChain : Chain
 {
     PictureBox picBox;      //image goes here
     /// <summary>
     ///
     /// </summary>
     /// <param name="pc"></param>
     public ImageChain(PictureBox pc)
     {
         picBox = pc;        //save reference
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="mesg"></param>
     public override void sendToChain(string mesg)
     {
         //put image in picture box
         string fname = mesg + ".jpg";   //assume jpg filename
         csFile fl = new csFile(fname);
         if (fl.exists())
             picBox.Image = new Bitmap(fname);
         else
         {
             if (hasChain())
             {   //send off down chain
                 chn.sendToChain(mesg);
             }
         }
 
     }
 }

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// <summary>
 /// handles command that is not otherwise legal
 /// Chain of Responsibility  Patterns 责任链模式
 /// 20220918
 /// geovindu,Geovin Du,涂聚文
 /// </summary>
 public class NoCmd : Chain
 {
 
     /// <summary>
     ///
     /// </summary>
     private ListBox lsNocmd;    //commands go here
     /// <summary>
     ///
     /// </summary>
     /// <param name="lb"></param>
     public NoCmd(ListBox lb)
     {
         lsNocmd = lb;           //copy reference
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="mesg"></param>
     public override void sendToChain(string mesg)
     {
         //adds unknown commands to list box
         lsNocmd.Items.Add(mesg);
     }
 }

  

调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// <summary>
 /// Chain of Responsibility  Patterns 责任链模式
 /// 20220918
 /// geovindu,Geovin Du,涂聚文
 /// </summary>
 public partial class ChainofResponsibilityPatternsForm : Form
 {
     /// <summary>
     ///
     /// </summary>
     private Chain chn;
     /// <summary>
     ///
     /// </summary>
     private void init()
     {
         //set up chains
         ColorChain clrChain = new ColorChain(pnlColor);
         FileChain flChain = new FileChain(lsFiles);
         NoCmd noChain = new NoCmd(lsNocmd);
         //create chain links
         chn = new ImageChain(picImage);
         chn.addToChain(clrChain);
         clrChain.addToChain(flChain);
         flChain.addToChain(noChain);
     }
     /// <summary>
     ///
     /// </summary>
     public ChainofResponsibilityPatternsForm()
     {
         InitializeComponent();
         init();
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
     private void ChainofResponsibilityPatternsForm_Load(object sender, EventArgs e)
     {
 
     }
     /// <summary>
     ///
     /// </summary>
     /// <param name="sender"></param>
     /// <param name="e"></param>
     private void btSend_Click(object sender, EventArgs e)
     {
         chn.sendToChain(txCommand.Text);
     }
 }

  

输出:

 

posted @   ®Geovin Du Dream Park™  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2017-09-27 sql server: left join 重复数据
2015-09-27 sql: table,view,function, procedure created MS_Description in sql server
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示