问题场景
一个请假的申请需要经过判定才能交给不同的上级进行处理,比如请假一天,需要需要副总经理确认,请假三天,需要总经理确认,请假三天以上需要董事长批准。如果使用条件语句来判定请假天数,新加入一个条件就得修改原来已经写好的代码,为了保证开闭原则的有效性,可以使用责任链模式。
总结模式
责任链相当于一个处理请求的管道,每个管道都持有它的上级管道的抽象引用,以便可以在处理请求之后把请求再转发给上级。
示例代码
以下是一个非常简陋的例子
public abstract class AbstractManager
{
public AbstractManager Manager { get; set; }
public abstract void Request(int day );
}
//董事长
public class Chairman : AbstractManager
{
public override void Request(int day )
{
if (day > 3) { Console.WriteLine( "嗯,批准请假" ); }
}
}
//经理
public class Manager : AbstractManager
{
public override void Request( int day )
{
if (day < 3) { Console.WriteLine( "嗯,批准请假" ); }
else
{
Console.WriteLine( "嗯,我批准请假,但你的情况还需要董事长批准" );
if (Manager != null) { Manager.Request( day ); }
}
}
}
public class Programe
{
static void Main( string[] args )
{
AbstractManager abstractManager = new Manager( );
abstractManager.Manager = new Chairman( );
abstractManager.Request( 5 );
}
}
{
public AbstractManager Manager { get; set; }
public abstract void Request(int day );
}
//董事长
public class Chairman : AbstractManager
{
public override void Request(int day )
{
if (day > 3) { Console.WriteLine( "嗯,批准请假" ); }
}
}
//经理
public class Manager : AbstractManager
{
public override void Request( int day )
{
if (day < 3) { Console.WriteLine( "嗯,批准请假" ); }
else
{
Console.WriteLine( "嗯,我批准请假,但你的情况还需要董事长批准" );
if (Manager != null) { Manager.Request( day ); }
}
}
}
public class Programe
{
static void Main( string[] args )
{
AbstractManager abstractManager = new Manager( );
abstractManager.Manager = new Chairman( );
abstractManager.Request( 5 );
}
}
实际上责任链和装饰者差不多,改写一下也就是个装饰者模式
public abstract class AbstractManager
{
protected AbstractManager Manager { get; set; }
public abstract void Request(int day );
}
//董事长
public class Chairman : AbstractManager
{
public override void Request(int day )
{
if (day > 3) { Console.WriteLine( "嗯,批准请假" ); }
}
}
//经理
public class Manager : AbstractManager
{
public Manager( AbstractManager abstractManager )
{
Manager = abstractManager;
}
public override void Request( int day )
{
if (day < 3) { Console.WriteLine( "嗯,批准请假" ); }
else
{
Console.WriteLine( "嗯,我批准请假,但你的情况还需要董事长批准" );
if (Manager != null) { Manager.Request( day ); }
}
}
}
public class Programe
{
static void Main( string[] args )
{
AbstractManager Chairman = new Chairman( );
AbstractManager Manager = new Manager( Chairman );
Manager.Request( 5 );
}
}
{
protected AbstractManager Manager { get; set; }
public abstract void Request(int day );
}
//董事长
public class Chairman : AbstractManager
{
public override void Request(int day )
{
if (day > 3) { Console.WriteLine( "嗯,批准请假" ); }
}
}
//经理
public class Manager : AbstractManager
{
public Manager( AbstractManager abstractManager )
{
Manager = abstractManager;
}
public override void Request( int day )
{
if (day < 3) { Console.WriteLine( "嗯,批准请假" ); }
else
{
Console.WriteLine( "嗯,我批准请假,但你的情况还需要董事长批准" );
if (Manager != null) { Manager.Request( day ); }
}
}
}
public class Programe
{
static void Main( string[] args )
{
AbstractManager Chairman = new Chairman( );
AbstractManager Manager = new Manager( Chairman );
Manager.Request( 5 );
}
}