CSharp: Chain of Responsibility Pattern
/// <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);
}
}
/// <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);
}
}
}
/// <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;
}
}
/// <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);
}
}
}
}
/// <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);
}
}
}
}
/// <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);
}
}
调用:
/// <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);
}
}
输出:

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号