你的位置:首页 > ASP.net教程

[ASP.net教程]《大话设计模式》学习笔记20:职责链模式


  

  

申请审批示例:

  

1.请求类:

  public class Request  {    public string RequestType { get; set; }    public string RequestContent { get; set; }    public int Number { get; set; }  }

2.Handler:

  public abstract class Manager  {    protected string name;    protected Manager superiorManager;    public Manager(string name)    {      this.name = name;    }    public void SetSuperior(Manager superiorManager)    {      this.superiorManager = superiorManager;    }    public abstract void RequestApplication(Request request);  }

3.ConcreteHandler:

  public class CommonManager:Manager  {    public CommonManager(string name)      :base(name)    {    }    public override void RequestApplication(Request request)    {      if(request.RequestType=="请假"&&request.Number<=2)      {        Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestType, request.Number);      }      else      {        if(superiorManager!=null)        {          superiorManager.RequestApplication(request);        }      }    }  }

  public class Majordomo:Manager  {    public Majordomo(string name)      :base(name)    {    }    public override void RequestApplication(Request request)    {      if(request.RequestType=="请假"&&request.Number<=5)      {        Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestType, request.Number);      }      else      {        if(superiorManager!=null)        {          superiorManager.RequestApplication(request);        }      }    }  }

  public class GeneralManager:Manager  {    public GeneralManager(string name)      :base(name)    {    }    public override void RequestApplication(Request request)    {      if(request.RequestType=="请假")      {        Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestType, request.Number);      }      else if(request.RequestType=="加薪")      {        if (request.Number <= 500)        {          Console.WriteLine("{0}:{1} 数量{2} 被批准", name, request.RequestType, request.Number);        }        else        {          Console.WriteLine("{0}:{1} 数量{2} 再说吧", name, request.RequestType, request.Number);        }      }    }  }

客户端代码:

  class Program  {    static void Main(string[] args)    {      Manager commonManager = new CommonManager("经理");      Manager majordom = new Majordomo("总监");      Manager generalManager = new GeneralManager("总经理");      commonManager.SetSuperior(majordom);      majordom.SetSuperior(generalManager);      Request request = new Request();      request.RequestType = "请假"; request.RequestContent = "事假"; request.Number = 1;      commonManager.RequestApplication(request);      request.RequestType = "请假"; request.RequestContent = "年假"; request.Number = 4;      commonManager.RequestApplication(request);      request.RequestType = "加薪"; request.RequestContent = "加薪一千"; request.Number = 1000;      commonManager.RequestApplication(request);    }  }

 

  优点:接收者和发送者都没有对方的明确信息,且链中对象自己也并不知道链的结构。结果是职责链可简化对象的相互连接,它们仅需保持一个指向其后继者的引用,而不需要保持它所有候选接受者的引用。