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

推荐订阅源

P
Proofpoint News Feed
V
V2EX
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
Martin Fowler
Martin Fowler
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
The Cloudflare Blog
T
Tailwind CSS Blog
H
Help Net Security
腾讯CDC
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
D
DataBreaches.Net
C
Check Point Blog
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 破甲

转: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'