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

これらのメソッドの条件性が、これらのメソッドを複雑で重複させています。外観がすでに存在する場合は、1つのことを行います。そうでない場合は、他のことをします。したがって、外観が存在することを確認してください。

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 に答える