If—Else \ If-ElseIf—Else 的随想、改造
最近有一个想法,想把这些If--Else、If-ElseIf-Else,全部换成一条条的Command,然后把这个Command串起来,最后放到CommandList中把它们一锅端了。:)
所以以下就这个想法产物:
先列举一下通常的分支、支叉
static String IfElseIfElse (Int32 conditionParam) {
var result = String.Empty;
if ( conditionParam == 1 ) {
result = "conditionParam1";
}
else if ( conditionParam == 2 ) {
result = "conditionParam2";
}
else {
result = "default";
}
return result;
}
那现在就按照想法、分不同场景来进化Code吧
场景1:根据传统的If-Else进行的改造,只适用传统的True Or Else的操作
声明方法: T ExecuteCommand<T> (IDictionary<Boolean , List<Func<T>>> commandList)
具体代码:
/// <summary>
/// 适用传统的True Or False的操作
/// </summary>
static T ExecuteCommand<T> (IDictionary<Boolean , List<Func<T>>> commandList) {
T t = default ( T );
foreach ( var command in commandList ) {
if ( command.Key ) {
t = command.Value[0] ();
break;
}
else {
t = command.Value[1] ();
break;
}
}
return t;
}
具体应用:
/// <summary>
/// 根据传统的If-Else进行的改造
/// </summary>
static String CommandList (Int32 conditionParam) {
var commandList = new Dictionary<Boolean , List<Func<String>>> ();
{
commandList.Add ( ( conditionParam == 1 ) ,
new List<Func<String>> (){
()=> { return "conditionParam1"; },
()=> { return "-conditionParam1"; }
} );
commandList.Add ( ( conditionParam == 2 ) ,
new List<Func<String>> () {
()=> { return "conditionParam2"; }
} );
}
return ExecuteCommand<String> ( commandList );
}
--------------------------------------------------------------------------------------------------------------------------------------------------
场景2:根据“场景1”缺点,增加了对If-ElseIf-Else的支持
声明方法: T ExecuteCommand<T> (Boolean? condition , Func<T> action)
具体代码:
/// <summary>
/// 适用True And False And Default的操作
/// </summary>
static T ExecuteCommand<T> (Boolean? condition , Func<T> action) {
T t = default ( T );
if ( condition.HasValue ) {
if ( condition.Value ) { t = action (); }
}
else {
t = action ();
}
return t;
}
具体应用:
/// <summary>
/// 增加了对If-ElseIf-Else的支持
/// </summary>
static String CommandFuncList (Int32 conditionParam) {
var commandList = new List<String> (){
ExecuteCommand<String>(( conditionParam ==1 ),
()=> { return "conditionParam1"; }
),
ExecuteCommand<String>(( conditionParam ==2 ),
()=> { return "conditionParam2"; }
),
ExecuteCommand<String>(null,
()=> { return "default"; }
)
};
return commandList.Where ( o => o != null ).First ();
}
--------------------------------------------------------------------------------------------------------------------------------------------------
以上场景都是带返回值,那如何对不同分支、不同那个什么;只想执行方法,一个方法或多个方法,又应该如何处理呢?
场景3:告诉我们还可以这样干
声明方法:List<Action> ExecuteCommand (Boolean? condition , List<Action> action)
具体方法:
/// <summary>
/// 只适用If-Else的方法操作
/// </summary>
static List<Action> ExecuteCommand (Boolean? condition , List<Action> action) {
List<Action> _action = default ( List<Action> );
if ( condition.HasValue ) {
if ( condition.Value ) {
_action = action;
}
}
else {
_action = action;
}
return _action;
}
具体应用:
static void CommandActionList (Int32 conditionParam) {
var commandList = new List<List<Action>> (){
ExecuteCommand(( conditionParam ==1 ),
new List<Action>() { ()=> Write() }
),
ExecuteCommand(( conditionParam ==2 ),
new List<Action>(){
()=> Write(),
()=> WriteAction()
}
)
};
commandList.Where ( o => o != null ).Select ( o => o ).ToList ().ForEach ( o => o.ForEach ( i => i () ) );
}
--------------------------------------------------------------------------------------------------------------------------------------------------
场景4:告诉我们,加上执行步骤,会让我们对分支掌控游刃有余
声明方法:List<Action> ExecuteCommand (ref Boolean execStep , Boolean? condition , List<Action> action)
具体方法:
static List<Action> ExecuteCommand (ref Boolean execStep , Boolean? condition , List<Action> action) {
List<Action> _action = default ( List<Action> );
if ( execStep ) {
if ( condition.HasValue ) {
if ( condition.Value ) {
_action = action;
execStep = false;
}
}
else {
_action = action;
execStep = false;
}
}
return _action;
}
具体应用:
static void CommandActionListWithStep (Int32 conditionParam) {
var execStep = true;
var commandList = new List<List<Action>> (){
ExecuteCommand(ref execStep,( conditionParam ==1 ),
new List<Action>() { ()=> Write() }
),
ExecuteCommand(ref execStep,( conditionParam ==2 ),
new List<Action>(){
()=> Write(),
()=> WriteAction()
}
),
ExecuteCommand(ref execStep,null, new List<Action>(){ ()=> WriteDefault() } )
};
commandList.FindAll ( (p) => { return p != null; } )
.ForEach ( o => o.ForEach ( i => i () ) );
}
static void Write () { Console.Write ( "Hello Command" ); }
static void WriteAction () { Console.Write ( "Hello Action" ); }
static void WriteDefault () { Console.Write ( "Hello Default" ); }
--------------------------------------------------------------------------------------------------------------------------------------------------
最终全部方法执行结果:
static void Main (string[] args) {
Console.Write ( "IfElseIfElse ( 1 )" + IfElseIfElse ( 1 ) );
Console.WriteLine ();
Console.Write ( "IfElseIfElse ( 4 )" + IfElseIfElse ( 4 ) );
Console.WriteLine ();
Console.Write ( "CommandList ( 1 )" + CommandList ( 1 ) );
Console.WriteLine ();
Console.Write ( "CommandList ( 2 )" + CommandList ( 2 ) );
Console.WriteLine ();
Console.Write ( "CommandFuncList ( 1 )" + CommandFuncList ( 1 ) );
Console.WriteLine ();
Console.Write ( "CommandFuncList ( 4 )" + CommandFuncList ( 4 ) );
Console.WriteLine ();
CommandActionList ( 1 );
Console.WriteLine ();
CommandActionList ( 2 );
Console.WriteLine ();
CommandActionListWithStep ( 1 );
Console.WriteLine ();
CommandActionListWithStep ( 4 );
Console.WriteLine ();
}