2

使用这种代码:

    public void UpdateCellFont(int id, string colName, Font font)
    {
        CellLocation location = new CellLocation(id, colName);

        if (CellAppearances.ContainsKey(location))
        {
            CellAppearances[location].Font = font;
        }
        else
        {
            CellAppearance cell = new CellAppearance(font, _DefaultBackColor, _DefaultForeColor);
            CellAppearances.Add(location, cell);
        }
    }

    public void UpdateCellBackColor(int id, string colName, Color backColor)
    {
        CellLocation location = new CellLocation(id, colName);

        if (CellAppearances.ContainsKey(location))
        {
            CellAppearances[location].BackColor = backColor;
        }
        else
        {
            CellAppearance cell = new CellAppearance(_DefaultFont, backColor, _DefaultForeColor);
            CellAppearances.Add(location, cell);
        }
    }

    public void UpdateCellForeColor(int id, string colName, Color foreColor)
    {
        CellLocation location = new CellLocation(id, colName);

        if (CellAppearances.ContainsKey(location))
        {
            CellAppearances[location].ForeColor = foreColor;
        }
        else
        {
            CellAppearance cell = new CellAppearance(_DefaultFont, _DefaultBackColor, foreColor);
            CellAppearances.Add(location, cell);
        }
    }

这些方法几乎都做同样的事情——每一个都会更新 Font、BackColor 或 ForeColor(或者如果字典中没有条目,它们会创建一个新条目。

当它们作用于强类型的 CellAppearance 时,如何减少此处的重复?

谢谢

4

4 回答 4

4

那直截了当呢?

public CellAppearance GetAppearance(int id, string colName){
    var location = new CellLocation(id, colName);
    if(!CellAppearances.ContainsKey(location))
       CellAppearances.Add(location, cell);
    return CellAppearances[location];
}

// usage:
GetAppearance(1,"hello").Font = myFont;
GetAppearance(2,"blubb").BackColor = myColor;
于 2012-08-26T07:56:48.083 回答
2

代表救援!

在这种情况下,TheHe 的答案应该符合要求,但通常你可以通过使用委托作为方法参数来解决这种情况(并且组织你的主要方法有点不同):

public void UpdateCellProperty (int id, string colName,
                                Action<CellAppearance> appearanceAction)
{
    CellAppearance cell;

    CellLocation location = new CellLocation(id, colName);
    if (CellAppearances.ContainsKey(location))
    {
        cell = CellAppearances[location];
    }
    else
    {
        cell = new CellAppearance(_DefaultFont, _DefaultBackColor,
                                  _DefaultForeColor);
    }
    appearanceAction(cell);
}

public void UpdateCellFont(int id, string colName, Font font)
{
    UpdateCellProperty(id, colName, c => c.Font = font);
}

public void UpdateCellBackColor(int id, string colName, Color backColor)
{
    UpdateCellProperty(id, colName, c => c.BackColor = backColor);
}

public void UpdateCellForeColor(int id, string colName, Color foreColor)
{
    UpdateCellProperty(id, colName, c => c.ForeColor = foreColor);
}

我已经看到这种模式被称为“中间模式的洞”。非常合适:您定义了一个带有“孔”的方法体,该“孔”注入了委托。

于 2012-08-26T08:43:51.660 回答
1

正是这些方法的条件性使它们变得复杂和重复。如果 Appearance 已经存在,你做一件事;如果没有,你做别的事情。所以确保外观存在:

public void EnsureCellAppearance(CellLocation location)
{
    if (CellAppearances.ContainsKey(location))
        return;
    CellAppearances.Add(location, new CellAppearance(_DefaultFont, _DefaultBackColor, _DefaultForeColor));
}

现在您的方法要简单得多:

public void UpdateCellFont(int id, string colName, Font font)
{
    CellLocation location = new CellLocation(id, colName);
    EnsureCellAppearance(location);
    CellAppearances[location].Font = font;
}
于 2012-08-26T17:33:59.197 回答
0

您可能必须使用反射将代码概括为不同类型的多个字段。不确定是否值得。

于 2012-08-26T07:53:09.303 回答