如何摆脱if-else链? [英] How to get rid of if-else chain?

查看:85
本文介绍了如何摆脱if-else链?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我目前正在使用Winforms应用程序。我有一个数据库,上面有表格。



我创建了一个表单,负责显示来自此数据表的数据。为此,我在此表单中添加了一个数据网格视图控件。我还创建了一个枚举来指定显示哪些数据。表单从数据库中获取数据并在数据网格视图中显示。



但我有一些问题。



我有很多不同的数据要显示,所以我有很多if-else链。喜欢;

Hi,

I'm currently working on a Winforms application. I have a database and there are tables on it.

I created a Form that responsible for showing data's coming from this data tables. For this purpose I added a Data Grid View control to this form. I also created an 'enum' to specify which data is shown. The form gets the data from the database and show it in Data Grid View.

But I have some problems about it.

I have a lot of different data to show, and so I have a lot of if-else chain. Like;

if(listType == ListViewTypes.Accounts)
{ 
   //get data from database
} else if(listType == ListViewTypes.Persons)
{ 
  // get data from database
} 
//etc...





有没有解决方法呢?



谢谢。



我的尝试:



我想创建一个不同的类来处理这个,但我也在该类中使用这个if-else链。



Is there any solution about that?

Thank you.

What I have tried:

I think about create a different class to handle this but I also use this if-else chain in that class too.

推荐答案

使用开关:

Use a switch:
switch (listType)
   {
   case ListViewTypes.Accounts:
      ...
      break;
   case ListViewTypes.Persons:
      ...
      break;
...
   default:
      ...
      break;
   }


根据执行代码的同质性,你可以使用 Dictionary 委托 s。例如,请参阅: c# - 带委托或交换机的词典? - 堆栈溢出 [ ^ ]。
Depending on the homogenity of the executed code, you could possibly use a Dictionary of delegates. See, for instance: c# - Dictionary with delegate or switch? - Stack Overflow[^].


您可以将if语句移至工厂类



You could move the "if" statement to a factory class

public interface IDataHandler
{
    void PopulateGridView(GridView gridView);
}

public class AccountsHandler : IDataHandler
{
    public void PopulateGridView(GridView gridView)
    {
        // populate gridView with accounts
    }
}

public class PersonsHandler : IDataHandler
{
    public void PopulateGridView(GridView gridView)
    {
        // populate gridView with people
    }
}

public class DataHandlerFactory
{
    public IDataHandler GetDataHandler (ListViewTypes types)
    {
        switch(types)
        {
            case ListViewTypes.Accounts:
                return new AccountsHandler();
            case ListViewTypes.Persons:
                return new PersonsHandler();
            default:
                throw new NotImplementedException();
        }
    }
}





使用





usage

DataHandlerFactory f = new DataHandlerFactory();

IDataHandler dh = f.GetDataHandler(types);

dh.PopulateGridView(GridView1);





您甚至可以做这样的事情来摆脱开关





You could even do something like this to get rid of the switch

public class DataHandlerFactory
{
    private static Dictionary<ListViewTypes, IDataHandler> handlers;

    static DataHandlerFactory()
    {
        handlers = new Dictionary<ListViewTypes, IDataHandler>();

        handlers.Add(ListViewTypes.Accounts, new AccountsHandler());
        handlers.Add(ListViewTypes.Persons, new PersonsHandler());
    }

    public IDataHandler GetDataHandler (ListViewTypes types)
    {
        IDataHandler dh;
            
        if (!handlers.TryGetValue(types, out dh))
        {
            throw new NotImplementedException();
        }

        return dh;

    }
}


这篇关于如何摆脱if-else链?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆