惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
N
News and Events Feed by Topic
SecWiki News
SecWiki News
T
The Exploit Database - CXSecurity.com
S
Security @ Cisco Blogs
H
Heimdal Security Blog
Security Latest
Security Latest
T
Threatpost
V2EX - 技术
V2EX - 技术
C
Cybersecurity and Infrastructure Security Agency CISA
GbyAI
GbyAI
The Last Watchdog
The Last Watchdog
Recent Announcements
Recent Announcements
P
Privacy International News Feed
K
Kaspersky official blog
P
Proofpoint News Feed
L
LangChain Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Threat Research - Cisco Blogs
博客园_首页
T
Tor Project blog
M
MIT News - Artificial intelligence
The Hacker News
The Hacker News
The GitHub Blog
The GitHub Blog
月光博客
月光博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
F
Full Disclosure
MyScale Blog
MyScale Blog
The Register - Security
The Register - Security
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
O
OpenAI News
T
The Blog of Author Tim Ferriss
S
Schneier on Security
小众软件
小众软件
The Cloudflare Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Know Your Adversary
Know Your Adversary
Microsoft Security Blog
Microsoft Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
Lohrmann on Cybersecurity
Vercel News
Vercel News

博客园 - BigRain

小学生学习汉字,汉字抓取 c# regex regextools cache 访问频率的思考 python 百度cpc点击 20130118SQL记录 PPT辅助工具 检查外链的方法 webService启用cookie的另一种方法 百度调价HttpWebRequest 装饰者模式 第一个android程序闪亮登场 SQL 的一些批量插入和修改 State Pattern 程序人生 SerialPort实现modem的来电显示 利用枚举进行状态的设计 singleton模式 在软件开发中的运用 我对当前项目的一些看法 闲话闲说——关于异常
职责链模式的运用
BigRain · 2009-05-27 · via 博客园 - BigRain

最近帮一个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 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 
/ 100000000ref result);
                result 
+= "亿";
            }
            
if (this.nextHandler != null)
            {
                
this.nextHandler.ProcessRequest(nubmer % 100000000ref 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 
/ 10000ref result);
                result 
+= "";
            }
            
if (this.nextHandler != null && nubmer % 10000 != 0)
            {
                
this.nextHandler.ProcessRequest(nubmer % 10000ref result);
            }
        }
    }
    
public class Qian : Handler
    {
        
public override void ProcessRequest(long nubmer, ref string result)
        {
            
if (nubmer > 1000)
            {
                Ge g 
= new Ge();
                g.ProcessRequest(nubmer 
/ 1000ref result);
                result 
+= "";
            }
            
if (this.nextHandler != null && nubmer % 1000 != 0)
            {
                nextHandler.ProcessRequest(nubmer 
% 1000ref 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 
/ 100ref result);
                result 
+= "";
            }
            
if (nextHandler != null && nubmer % 100 != 0)
            {
                nextHandler.ProcessRequest(nubmer 
% 100ref 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 
/ 10ref result);
                result 
+= "";
            }
            
if (nextHandler != null && nubmer % 10 != 0)
            {
                nextHandler.ProcessRequest(nubmer 
% 10ref 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就使用了职责链模式