最近有一個想法,想把這些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 ();
}