我读设计模式之Chain Of Responsibility Pattern
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
///使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系
///将这个对象练成一条链,并沿着这条链条传递该请求,直到有一个对象处理它为止
///
/// 举例:
///员工发送申请单据,直接直接交给自己直接主管;
///至于到底谁有权限审核,则发送者不必关心。
///
namespace ChainOfResponsibilityPattern
{
class Program
{
static void Main(string[] args)
{
Manager commonManager = new CommonManager("Spike");
Manager majorManager = new MajorManager("kenny");
Manager generalManager = new GeneralManager("John");
commonManager.SetSuperior(majorManager);
majorManager.SetSuperior(generalManager);
Request request = new Request();
request.RequestType = "请假";
commonManager.RequestApplications(request);
request = new Request();
request.RequestType = "加薪";
commonManager.RequestApplications(request);
request = new Request();
request.RequestType = "离职";
commonManager.RequestApplications(request);
Console.Read();
}
}
public abstract class Manager
{
public string name;
//管理者上级
protected Manager superior;
public Manager(string name)
{
this.name = name;
}
//设置管理者上级
public void SetSuperior(Manager superior)
{
this.superior = superior;
}
//申请请求
public abstract void RequestApplications(Request request);
}
//Common Manager
public class CommonManager : Manager
{
public CommonManager(string name) : base(name) { }
public override void RequestApplications(Request request)
{
if (request.RequestType == "请假")
{
Console.WriteLine("你的请假单已经被Common Manager 【" + name + "】批准");
}
else
{
if (superior != null)
{
superior.RequestApplications(request);
}
else
{
Console.WriteLine("没有人具有批准【" + request.RequestType + "】的权限");
}
}
}
}
//Major Manager
public class MajorManager : Manager
{
public MajorManager(string name) : base(name) { }
public override void RequestApplications(Request request)
{
if (request.RequestType == "请长假")
{
Console.WriteLine("你的长期请假单已经被Major Manager 【" + name + "】 批准");
}
else
{
if (superior != null)
{
superior.RequestApplications(request);
}
else
{
Console.WriteLine("没有人具有批准【" + request.RequestType + "】的权限");
}
}
}
}
//General Manager
public class GeneralManager : Manager
{
public GeneralManager(string name) : base(name) { }
public override void RequestApplications(Request request)
{
if (request.RequestType == "加薪")
{
Console.WriteLine("你的加薪申请已经被general Manager 【"+name+"】批准");
}
else
{
if (superior != null)
{
superior.RequestApplications(request);
}
else
{
Console.WriteLine("没有人具有批准【"+request.RequestType+"】的权限");
}
}
}
}
//申请
public class Request
{
//申请类别
public string RequestType { set; get; }
//申请内容
public string RequestContent { set; get; }
//Number
public string Number { set; get; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
///使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系
///将这个对象练成一条链,并沿着这条链条传递该请求,直到有一个对象处理它为止
///
/// 举例:
///员工发送申请单据,直接直接交给自己直接主管;
///至于到底谁有权限审核,则发送者不必关心。
///
namespace ChainOfResponsibilityPattern
{
class Program
{
static void Main(string[] args)
{
Manager commonManager = new CommonManager("Spike");
Manager majorManager = new MajorManager("kenny");
Manager generalManager = new GeneralManager("John");
commonManager.SetSuperior(majorManager);
majorManager.SetSuperior(generalManager);
Request request = new Request();
request.RequestType = "请假";
commonManager.RequestApplications(request);
request = new Request();
request.RequestType = "加薪";
commonManager.RequestApplications(request);
request = new Request();
request.RequestType = "离职";
commonManager.RequestApplications(request);
Console.Read();
}
}
public abstract class Manager
{
public string name;
//管理者上级
protected Manager superior;
public Manager(string name)
{
this.name = name;
}
//设置管理者上级
public void SetSuperior(Manager superior)
{
this.superior = superior;
}
//申请请求
public abstract void RequestApplications(Request request);
}
//Common Manager
public class CommonManager : Manager
{
public CommonManager(string name) : base(name) { }
public override void RequestApplications(Request request)
{
if (request.RequestType == "请假")
{
Console.WriteLine("你的请假单已经被Common Manager 【" + name + "】批准");
}
else
{
if (superior != null)
{
superior.RequestApplications(request);
}
else
{
Console.WriteLine("没有人具有批准【" + request.RequestType + "】的权限");
}
}
}
}
//Major Manager
public class MajorManager : Manager
{
public MajorManager(string name) : base(name) { }
public override void RequestApplications(Request request)
{
if (request.RequestType == "请长假")
{
Console.WriteLine("你的长期请假单已经被Major Manager 【" + name + "】 批准");
}
else
{
if (superior != null)
{
superior.RequestApplications(request);
}
else
{
Console.WriteLine("没有人具有批准【" + request.RequestType + "】的权限");
}
}
}
}
//General Manager
public class GeneralManager : Manager
{
public GeneralManager(string name) : base(name) { }
public override void RequestApplications(Request request)
{
if (request.RequestType == "加薪")
{
Console.WriteLine("你的加薪申请已经被general Manager 【"+name+"】批准");
}
else
{
if (superior != null)
{
superior.RequestApplications(request);
}
else
{
Console.WriteLine("没有人具有批准【"+request.RequestType+"】的权限");
}
}
}
}
//申请
public class Request
{
//申请类别
public string RequestType { set; get; }
//申请内容
public string RequestContent { set; get; }
//Number
public string Number { set; get; }
}
}