我如何将这两种类似的方法合二为一

本文关键字:方法 合二为一 种类 两种 | 更新日期: 2023-09-27 18:36:27

下面的方法只是彼此的反转。 我怀疑我可以将逻辑组合成一种方法。 我宁愿避免反思。 是否可以将它们组合起来并保持可读性?

    private void btnAdd_Click(object sender, EventArgs e)
    {
        LabEntity selectedItem = bindingSource1.Current as LabEntity;
        selectedLabsData.Add(selectedItem);
        availableLabsData.Remove(selectedItem);
    }
    private void btnRemove_Click(object sender, EventArgs e)
    {
        LabEntity selectedItem = bindingSource2.Current as LabEntity;//new binding source
        availableLabsData.Add(selectedItem);//called Add instead of remove
        selectedLabsData.Remove(selectedItem);//called Remove instead of Add
    }

我如何将这两种类似的方法合二为一

您可以将逻辑分解到帮助程序方法中:

private void ListFixup(object entity, List<Item> addList, List<Item> removeList)
{
    LabEntity selectedItem = entity as LabEntity;
    // don't forget your error checking here
    addList.Add(selectedItem);
    removeList.Remove(selectedItem);
}
private void btnAdd_Click(object sender, EventArgs e)
{
    ListFixup(bindingSource1.Current, selectedLabsData, availableLabsData);
}
private void btnRemove_Click(object sender, EventArgs e)
{
    ListFixup(bindingSource2.Current, availableLabsData, selectedLabsData);
}

我不确定这是否有助于可读性,但它确实减少了代码重复。

private void btnAdd_Click(object sender, EventArgs e)
{
   SwapThem( bindingSource1, selectedLabsData, availableLabsData );
}
private void btnRemove_Click(object sender, EventArgs e)
{
   SwapThem( bindingSource2, availableLabsData, selectedLabsData );
}
// I just don't know the proper type-cast of the "toAddTo" and "toRemoveFrom" parameters.
private void SwapThem( BindingSource bs, List<yourType> toAddTo, List<yourType> toRemoveFrom )
{
   LabEntity selectedItem = bs.Current as LabEntity;
   toAddTo.Add(selectedItem);
   toRemoveFrom.Remove(selectedItem);
}
没有任何

方法可以重构这两种方法,这不会导致代码的可读性显着降低,您可以在发布的其他一些答案中看到这一点。在这种情况下,为了可读性起见,代码重复级别是可以接受的。

向发件人添加标签?

private void btnClick(object sender, EventArgs e)
{
Button *myButton = (Button)sender;
if (myButton.tag == 1){
        LabEntity selectedItem = bindingSource1.Current as LabEntity;
        selectedLabsData.Add(selectedItem);
        availableLabsData.Remove(selectedItem);
}
else {
LabEntity selectedItem = bindingSource2.Current as LabEntity;//new binding source
        availableLabsData.Add(selectedItem);//called Add instead of remove
        selectedLabsData.Remove(selectedItem);//called Remove instead of Add
}
}

我没有检查编译器错误,这只是一个例子。

将两个按钮链接到单个事件处理程序。然后处理程序可能看起来像这样:(请原谅对按钮的多次检查,因为我不知道将"可用实验室数据"和"选定实验室数据"的持有者声明为什么类型):

    private void btnClick(object sender, EventArgs e)
    {
        var bindingSource = (sender == btnRemove) ? bindingSource2 : bindingSource1;   
        var selectedItem = source.Current as LabEntity;
        if(sender == btnRemove) 
        {
            availableLabsData.Add(selectedItem);
            selectedLabsData.Remove(selectedItem);
        }
        else if(sender == btnAdd)
        {
            availableLabsData.Remove(selectedItem);
            selectedLabsData.Add(selectedItem);
        }
    }

每个人都是对的,不要以降低代码的可读性为代价来过火

我唯一的想法是...

仅从发布的代码来看,但看起来selectedLabsDataavailableLabsData紧密交织在一起,所以我不会将与其相关的逻辑放在事件处理程序中。 将逻辑放在不同的方法(最好是不同的类)中,这样您就不会意外地更新一个而不更新另一个。 这样做的好处是使所讨论的方法更简单,更"可读"。

private void LabsDataAdded(LabEntity value)
{
    selectedLabsData.Add(value);
    availableLabsData.Remove(value);
}
private void LabsDataRemoved(LabEntity value)
{
    availableLabsData.Add(value);
    selectedLabsData.Remove(value);
}

那么方法就是:

private void btnAdd_Click(object sender, EventArgs e)
{
    LabsDataAdded(bindingSource1.Current as LabEntity);
}
private void btnRemove_Click(object sender, EventArgs e)
{
    LabsDataRemoved(bindingSource2.Current as LabEntity);
}

更好的是,您可以使用 lambda 表达式使代码更加简洁:

btnAdd.Clicked += (sender, e) => LabsDataAdded(bindingSource1.Current as LabEntity);
btnAdd.Clicked += (sender, e) => LabsDataRemoved(bindingSource1.Current as LabEntity);

像这样:

private void btnAdd_Click(object sender, EventArgs e)
{
    LabEntity selectedItem = bindingSource1.Current as LabEntity;
    RemoveItemFromList(selectedItem);
}
private void btnRemove_Click(object sender, EventArgs e)
{
    LabEntity selectedItem = bindingSource2.Current as LabEntity;//new binding source
    RemoveItemFromList(selectedItem);
}
private void RemoveItemFromList(LabEntity ent)
{
    selectedLabsData.Add(ent);
    availableLabsData.Remove(ent);
}