职责链模式的运用
最近帮一个mm写一个数字转换为大写的程序,在这里我使用的职责链模式。
意图:
使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
适用性:
有多个的对象可以处理一个请求,哪个对象处理该请求运行时刻自动确定。
你想在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。
可处理一个请求的对象集合应被动态指定。
适用性:
有多个的对象可以处理一个请求,哪个对象处理该请求运行时刻自动确定。
你想在不明确指定接收者的情况下,向多个对象中的一个提交一个请求。
可处理一个请求的对象集合应被动态指定。
首先定义一个处理类
public abstract class Handler
{
public void SetNextHandler(Handler next)
{
this.nextHandler = next;
}
protected Handler nextHandler;
public abstract void ProcessRequest(long nubmer, ref string result);
}
这是一个抽象类,因为不同的子类的处理不同{
public void SetNextHandler(Handler next)
{
this.nextHandler = next;
}
protected Handler nextHandler;
public abstract void ProcessRequest(long nubmer, ref string result);
}
然后,分别实现 个十百千万亿 这几个具体的处理者
public class YI : Handler
{
public override void ProcessRequest(long nubmer, ref string result)
{
if (nubmer > 100000000)
{
Wang w = new Wang();
Qian q = new Qian();
Bai b = new Bai();
Shi s = new Shi();
Ge g = new Ge();
w.SetNextHandler(q);
q.SetNextHandler(b);
b.SetNextHandler(s);
s.SetNextHandler(g);
w.ProcessRequest(nubmer / 100000000, ref result);
result += "亿";
}
if (this.nextHandler != null)
{
this.nextHandler.ProcessRequest(nubmer % 100000000, ref result);
}
}
}
public class Wang : Handler
{
public override void ProcessRequest(long nubmer, ref string result)
{
if (nubmer > 10000)
{
Qian q = new Qian();
Bai b = new Bai();
Shi s = new Shi();
Ge g = new Ge();
q.SetNextHandler(b);
b.SetNextHandler(s);
s.SetNextHandler(g);
q.ProcessRequest(nubmer / 10000, ref result);
result += "万";
}
if (this.nextHandler != null && nubmer % 10000 != 0)
{
this.nextHandler.ProcessRequest(nubmer % 10000, ref result);
}
}
}
public class Qian : Handler
{
public override void ProcessRequest(long nubmer, ref string result)
{
if (nubmer > 1000)
{
Ge g = new Ge();
g.ProcessRequest(nubmer / 1000, ref result);
result += "千";
}
if (this.nextHandler != null && nubmer % 1000 != 0)
{
nextHandler.ProcessRequest(nubmer % 1000, ref result);
}
}
}
public class Bai : Handler
{
public override void ProcessRequest(long nubmer, ref string result)
{
if (nubmer > 100)
{
if (result.Length > 0 && !result.EndsWith("百"))
result += "零";
Ge g = new Ge();
g.ProcessRequest(nubmer / 100, ref result);
result += "百";
}
if (nextHandler != null && nubmer % 100 != 0)
{
nextHandler.ProcessRequest(nubmer % 100, ref result);
}
}
}
public class Shi : Handler
{
public override void ProcessRequest(long nubmer, ref string result)
{
if (nubmer > 10)
{
if (result.Length > 0 && !result.EndsWith("百"))
result += "零";
Ge g = new Ge();
g.ProcessRequest(nubmer / 10, ref result);
result += "拾";
}
if (nextHandler != null && nubmer % 10 != 0)
{
nextHandler.ProcessRequest(nubmer % 10, ref result);
}
}
}
public class Ge : Handler
{
public override void ProcessRequest(long nubmer, ref string result)
{
if (nubmer < 10)
{
string str1 = "零壹贰叁肆伍陆柒捌玖";
result += str1[(int)nubmer];
}
}
}
{
public override void ProcessRequest(long nubmer, ref string result)
{
if (nubmer > 100000000)
{
Wang w = new Wang();
Qian q = new Qian();
Bai b = new Bai();
Shi s = new Shi();
Ge g = new Ge();
w.SetNextHandler(q);
q.SetNextHandler(b);
b.SetNextHandler(s);
s.SetNextHandler(g);
w.ProcessRequest(nubmer / 100000000, ref result);
result += "亿";
}
if (this.nextHandler != null)
{
this.nextHandler.ProcessRequest(nubmer % 100000000, ref result);
}
}
}
public class Wang : Handler
{
public override void ProcessRequest(long nubmer, ref string result)
{
if (nubmer > 10000)
{
Qian q = new Qian();
Bai b = new Bai();
Shi s = new Shi();
Ge g = new Ge();
q.SetNextHandler(b);
b.SetNextHandler(s);
s.SetNextHandler(g);
q.ProcessRequest(nubmer / 10000, ref result);
result += "万";
}
if (this.nextHandler != null && nubmer % 10000 != 0)
{
this.nextHandler.ProcessRequest(nubmer % 10000, ref result);
}
}
}
public class Qian : Handler
{
public override void ProcessRequest(long nubmer, ref string result)
{
if (nubmer > 1000)
{
Ge g = new Ge();
g.ProcessRequest(nubmer / 1000, ref result);
result += "千";
}
if (this.nextHandler != null && nubmer % 1000 != 0)
{
nextHandler.ProcessRequest(nubmer % 1000, ref result);
}
}
}
public class Bai : Handler
{
public override void ProcessRequest(long nubmer, ref string result)
{
if (nubmer > 100)
{
if (result.Length > 0 && !result.EndsWith("百"))
result += "零";
Ge g = new Ge();
g.ProcessRequest(nubmer / 100, ref result);
result += "百";
}
if (nextHandler != null && nubmer % 100 != 0)
{
nextHandler.ProcessRequest(nubmer % 100, ref result);
}
}
}
public class Shi : Handler
{
public override void ProcessRequest(long nubmer, ref string result)
{
if (nubmer > 10)
{
if (result.Length > 0 && !result.EndsWith("百"))
result += "零";
Ge g = new Ge();
g.ProcessRequest(nubmer / 10, ref result);
result += "拾";
}
if (nextHandler != null && nubmer % 10 != 0)
{
nextHandler.ProcessRequest(nubmer % 10, ref result);
}
}
}
public class Ge : Handler
{
public override void ProcessRequest(long nubmer, ref string result)
{
if (nubmer < 10)
{
string str1 = "零壹贰叁肆伍陆柒捌玖";
result += str1[(int)nubmer];
}
}
}
OK,现在每一个单独的节点都已经写好了,客户端的调用
Code
职责链的运用场景很广,很像我们工厂中的产品加工流水线,原料从入口进去,经过一道道的工序,然后就出来了一个产品。在asp.net中,IHttpHandler就使用了职责链模式