标签:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication20
{
public class Response
{
public string ResponseStr { get; set; }
}
public class Request
{
public string RequestStr { get; set; }
}
public interface IFiler
{
void doFilter(Request requestStr,Response str,FilterChain fc);
}
public class AFilter:IFiler
{
public void doFilter(Request request,Response response,FilterChain fc)
{
request.RequestStr = request.RequestStr + "--->AFilter";
fc.SetFilter(request,response,fc);
response.ResponseStr = response.ResponseStr + "--->AFilter";
}
}
public class BFilter : IFiler
{
public void doFilter(Request request, Response response,FilterChain fc)
{
request.RequestStr = request.RequestStr + "--->BFilter";
fc.SetFilter(request,response,fc);
response.ResponseStr = response.ResponseStr + "--->BFilter";
}
}
public class CFilter : IFiler
{
public void doFilter(Request request, Response response,FilterChain fc)
{
request.RequestStr = request.RequestStr + "--->CFilter";
fc.SetFilter(request, response, fc);
response.ResponseStr = response.ResponseStr + "--->CFilter";
}
}
public class FilterChain
{
IList<IFiler> fcFilers = new List<IFiler>();
private int index = 0;
public FilterChain AddFilter(IFiler f)
{
fcFilers.Add(f);
return this;
}
public void SetFilter(Request request,Response response,FilterChain fc)
{
if (index == fcFilers.Count) return;
IFiler f = fcFilers[index];
index++;
f.doFilter(request,response,fc);
}
}
class Program
{
static void Main(string[] args)
{
//客户端处理敏感信息
Response respons=new Response();
Request request=new Request();
respons.ResponseStr = "response";
request.RequestStr = "request";
FilterChain fc=new FilterChain();
fc.AddFilter(new AFilter()).AddFilter(new BFilter()).AddFilter(new CFilter());
fc.SetFilter(request,respons,fc);
Console.WriteLine(request.RequestStr+"\n"+respons.ResponseStr);
Console.ReadKey();
}
}
}
职责链 评价:挺好玩的 稍微复杂版 类似出栈入栈 (原理职责链调用一个之后跳转到第二个执行在跳转第三个执行 接下来因为每次调用完后都会返回被调用的位置)
标签:
原文地址:http://www.cnblogs.com/kexb/p/4507853.html