理解:当一件事情需要一级一级地处理时,并且每一级都有指定的下一级来处理,就好像工厂里面一个员工要请假,会先把请假条拿给领班,领班看能否处理,不能再传给主管,主管如果也不能处理再传给经理,经理不能处理再往上传。
代码:一个数据报文在路由之间传递实例
using System.Windows.Forms;
namespace DesignMode.Chain_of_Responsibility
{
//数据报文
public class Datagram
{
private string aimIP;
public string AimIP
{
get { return aimIP; }
set { aimIP = value; }
}
private string content;
public string Content
{
get { return content; }
set { content = value; }
}
}
//抽象处理数据包的主机类
public abstract class Host
{
//设定下一个指定主机
protected Host _nextHost;
public void SetNextHost(Host nextHost)
{
this._nextHost = nextHost;
}
//检查数据报是否应该处理
public abstract void CheckDatagram(Datagram datagram);
}
public class A_Host : Host
{
public override void CheckDatagram(Datagram datagram)
{
if (datagram.AimIP.Equals("192.168.1.11"))
{
MessageBox.Show("11主机检查通过,处理完成");
}
else
{
if (_nextHost != null)
{
this._nextHost.CheckDatagram(datagram);
}
}
}
}
public class B_Host : Host
{
public override void CheckDatagram(Datagram datagram)
{
if (datagram.AimIP.Equals("192.168.1.22"))
{
MessageBox.Show("22主机检查通过,处理完成");
}
else
{
if (_nextHost != null)
{
this._nextHost.CheckDatagram(datagram);
}
}
}
}
public class C_Host : Host
{
public override void CheckDatagram(Datagram datagram)
{
if (datagram.AimIP.Equals("192.168.1.33"))
{
MessageBox.Show("33主机检查通过,处理完成");
}
else
{
MessageBox.Show("找不到处理该报文的主机");
}
}
} }
namespace DesignMode.Chain_of_Responsibility
{
//数据报文
public class Datagram
{
private string aimIP;
public string AimIP
{
get { return aimIP; }
set { aimIP = value; }
}
private string content;
public string Content
{
get { return content; }
set { content = value; }
}
}
//抽象处理数据包的主机类
public abstract class Host
{
//设定下一个指定主机
protected Host _nextHost;
public void SetNextHost(Host nextHost)
{
this._nextHost = nextHost;
}
//检查数据报是否应该处理
public abstract void CheckDatagram(Datagram datagram);
}
public class A_Host : Host
{
public override void CheckDatagram(Datagram datagram)
{
if (datagram.AimIP.Equals("192.168.1.11"))
{
MessageBox.Show("11主机检查通过,处理完成");
}
else
{
if (_nextHost != null)
{
this._nextHost.CheckDatagram(datagram);
}
}
}
}
public class B_Host : Host
{
public override void CheckDatagram(Datagram datagram)
{
if (datagram.AimIP.Equals("192.168.1.22"))
{
MessageBox.Show("22主机检查通过,处理完成");
}
else
{
if (_nextHost != null)
{
this._nextHost.CheckDatagram(datagram);
}
}
}
}
public class C_Host : Host
{
public override void CheckDatagram(Datagram datagram)
{
if (datagram.AimIP.Equals("192.168.1.33"))
{
MessageBox.Show("33主机检查通过,处理完成");
}
else
{
MessageBox.Show("找不到处理该报文的主机");
}
}
} }
客户端代码:
private void btn_ChainOfResponsibility_Click(object sender, EventArgs e)
{
Host host11 = new A_Host();
Host host22 = new B_Host();
Host host33 = new C_Host();
//为每台主机指定下一个处理主机
host11.SetNextHost(host22);
host22.SetNextHost(host33);
Datagram data = new Datagram();
data.AimIP = "192.168.1.111";
data.Content = "test content";
host11.CheckDatagram(data); }
{
Host host11 = new A_Host();
Host host22 = new B_Host();
Host host33 = new C_Host();
//为每台主机指定下一个处理主机
host11.SetNextHost(host22);
host22.SetNextHost(host33);
Datagram data = new Datagram();
data.AimIP = "192.168.1.111";
data.Content = "test content";
host11.CheckDatagram(data); }