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

推荐订阅源

L
LangChain Blog
博客园 - 司徒正美
美团技术团队
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Troy Hunt's Blog
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
B
Blog
NISL@THU
NISL@THU
月光博客
月光博客
博客园 - 【当耐特】
AWS News Blog
AWS News Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
L
Lohrmann on Cybersecurity
The Cloudflare Blog
L
LINUX DO - 最新话题
S
Security @ Cisco Blogs
S
Secure Thoughts
Spread Privacy
Spread Privacy
有赞技术团队
有赞技术团队
The Last Watchdog
The Last Watchdog
Project Zero
Project Zero
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
H
Hacker News: Front Page
S
SegmentFault 最新的问题
Schneier on Security
Schneier on Security
aimingoo的专栏
aimingoo的专栏
P
Privacy & Cybersecurity Law Blog
博客园 - 三生石上(FineUI控件)
Forbes - Security
Forbes - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
I
InfoQ
T
Tailwind CSS Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
GRAHAM CLULEY
W
WeLiveSecurity
小众软件
小众软件
Recorded Future
Recorded Future
Cyberwarzone
Cyberwarzone
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org

博客园 - 破甲

转:Chrome调试工具介绍 转:一组jQuery插件的连接 关于导出属性 Aop中动态横切与静态横切 老张的灵魂——敏捷回顾 LINQ to SQL(LINQ2SQL) vs. ADO.NET Entity Framework(ADOEF)-ccBoy版 ---阅读笔记 linq to sql 与linq to entities的选择 linq to sql 算ORM吗? 匿名方法实现(转) System.Linq扩张方法Where (Lambda表达式) Oracle的并发多版本 读一致性 共享锁 排他锁 LING与HQL(三) LINQ与HQL (二) LINQ 与 HQL (一) C# 3.0的新特性(一) 挖掘ADO.NET Entity框架的性能 castle ar 的update方法!! HQL查询中的几个函数
动态的链式传递执行
破甲 · 2013-12-17 · via 博客园 - 破甲

我通常在想到设计模式中的职责链时, 都会想到A保存B的句柄,比如:

public class A{

     private B _b;

     void Invoke(){ _b.Invoke() ;  }

public class B{

     private C _c;

     void Invoke(){ _c.Invoke() ;  }

当出现多个对象需要串联起来执行是,就需要bulid 他们的关系,让前面知道后的,这样就必须有个factory或builder 来做这件是事情,我估计把这汇总方式称为静态链式;

下面换种方式来看这个这个问题,我也姑且称它为动态链式; 这应该也是Entlib 拦截器的实现和Castle不一样地方(可能,我已经记不住了);

这么一种方式为: 

public delegate void InvokeHandler(GetNextInvokeHandler getNext);  
public delegate InvokeHandler GetNextInvokeHandler();   //获取下一个Handler

public class DelegateContainer
{

public IList<InvokeHandler> _listDelegates;

public DelegateContainer() { }

public DelegateContainer(IList<InvokeHandler> list)
: this()
{
_listDelegates = list;
}

public void Invoke(InvokeHandler invoke)
{
if (_listDelegates.Count == 0)
{
//Do mywork

invoke(null);
return;
}
int index = 0;
InvokeHandler delg = _listDelegates[index];
delg(delegate
{
if (_listDelegates.Count > ++index)
{
return _listDelegates[index];
}
return invoke; //Real InvokeHandler;
});
}

}

测试方法如下 : 

[TestClass]
public class TestDelegateContainer
{
[TestMethod]
public void TestInvoke()
{
IList<InvokeHandler> handlers = new List<InvokeHandler>();
handlers.Add(new InvokeHandler(InjectInvoke1));
handlers.Add(new InvokeHandler(InjectInvoke2));
handlers.Add(new InvokeHandler(InjectInvoke3));
DelegateContainer container = new DelegateContainer(handlers);
container.Invoke(RealInvoke);
Assert.IsTrue(1 == _resultCount);
Assert.IsTrue(_executionStack.Contains("Real Execution"));
}

private int _resultCount = 0;
public void RealInvoke(GetNextInvokeHandler getNext)
{
//Domywork
_executionStack += "Real Execution";
_resultCount++;
}

private string _executionStack = string.Empty;
public void InjectInvoke1(GetNextInvokeHandler getNext)
{
_executionStack += "Invoke1 >>";
InvokeHandler handler = getNext();
if (handler != null)
{
handler.Invoke(getNext); //这里其实是个关键点
}
//Domywork
}
public void InjectInvoke2(GetNextInvokeHandler getNext)
{
_executionStack += "Invoke2 >>";
InvokeHandler handler = getNext();
if (handler != null)
{
handler.Invoke(getNext);
}
//Domywork

}
public void InjectInvoke3(GetNextInvokeHandler getNext)
{
_executionStack += "Invoke3 >>";
InvokeHandler handler = getNext();
if (handler != null)
{
handler.Invoke(getNext);
}
//Domywork
}
}

输出结果:

resultCount:1

executionStack:'Invoke1 >>Invoke2 >>Invoke3 >>Real Execution'