How can C reduce or eliminate the use of switch
- 2020-05-30 19:47:57
- OfStack
The solution for Insus.NET is to use factory methods to handle it. You can create one factory interface, then design one factory class for each method, and implement the factory interface.
Factory interface:
Get factory:
GetTest class:
And the GetSet class:
So your code ends up being:
15:50 the modifications and additions are as follows:
In the final code above, there is no incoming parameter mothedName. How to do it? We can consider 1 under reflection.
"get" > > "Get";
"get1" > > "GetTest"
"testget" > > "GetSet"
So once you change 1, you can use the reflection syntax
Change to (the following is the application of asp.net) :
If you are not using asp.net, you can change "App_Code" to "assembly name" :
Factory interface:
IGetFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for IGetFactory
/// </summary>
namespace Insus.NET
{
public interface IGetFactory
{
string GetResult();
}
}
Get factory:
GetFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for GetFactory
/// </summary>
namespace Insus.NET
{
public class GetFactory : IGetFactory
{
public GetFactory()
{
//
// TODO: Add constructor logic here
//
}
public string GetResult()
{
return "get";
}
}
}
GetTest class:
GetTestFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for GetTestFactory
/// </summary>
namespace Insus.NET
{
public class GetTestFactory : IGetFactory
{
public GetTestFactory()
{
//
// TODO: Add constructor logic here
//
}
public string GetResult()
{
return "gettest";
}
}
}
And the GetSet class:
GetSetFactory
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for GetSetFactory
/// </summary>
namespace Insus.NET
{
public class GetSetFactory : IGetFactory
{
public GetSetFactory()
{
//
// TODO: Add constructor logic here
//
}
public string GetResult()
{
return "getset";
}
}
}
So your code ends up being:
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string Exec(string mothedName)
{
string ret = "";
//switch (mothedName)
//{
// case "get":
// ret = get();
// break;
// case "get1":
// ret = gettest();
// break;
// //.....
// case "testget":
// ret = getrset();
// break;
//}
IGetFactory get = new GetTestFactory(); // Here is the implementation factory class
ret = get.GetResult();
return ret;
}
//public string get()
//{
// return "get";
//}
//public string gettest()
//{
// return "gettest";
//}
//public string getrset()
//{
// return "getset";
//}
}
15:50 the modifications and additions are as follows:
In the final code above, there is no incoming parameter mothedName. How to do it? We can consider 1 under reflection.
"get" > > "Get";
"get1" > > "GetTest"
"testget" > > "GetSet"
So once you change 1, you can use the reflection syntax
IGetFactory get = new GetTestFactory(); // Here is the implementation factory class
Change to (the following is the application of asp.net) :
IGetFactory get = (IGetFactory)Assembly.Load("App_Code").CreateInstance("Insus.NET." + mothedName + "Factory");
If you are not using asp.net, you can change "App_Code" to "assembly name" :
IGetFactory get = (IGetFactory)Assembly.Load(" Assembly name ").CreateInstance("Insus.NET." + mothedName + "Factory");