【问题标题】:avoid anti arrow pattern避免反箭头图案
【发布时间】:2015-09-23 12:12:59
【问题描述】:

我有模式模板方法,可以为不同类型的图表实现基本功能。 FastChart,SlowChart每种图表类型都实现了缩放功能Zoom()这个功能应该对不同的Series做不同的缩放AreaSeries,CandleStickSeries。

我认为

    public override void Zoom()
    {
        if (BTF.AreaSeries != null)
        {

        }
        if (BTF.CandleStickSeries!=null)
        { 

        }
    }

非常糟糕的主意,因为如果我应该有 10 种类型的系列,我应该在每个函数中创建 10 个 if 语句。如何避免这种情况?

namespace ConsoleApplication38765
{
public interface IFirstType
{
    AreaSeries AreaSeries { get; set; }
    CandleStickSeries CandleStickSeries { get; set; }
}
public class BoxTypeFirst : IFirstType
{
    public AreaSeries AreaSeries { get; set; }
    public CandleStickSeries CandleStickSeries { get; set; }
}
public interface IChart
{
    void Zoom();
    void DrawChart();
    void GetPoints();
}
public interface IRectangle
{
    void AddRectangles();
}
public abstract class BaseChart:IChart
{
    public abstract void Zoom();

    public virtual void DrawChart()
    {
    }

    public  virtual void GetPoints()
    {
    }
}
public class FastChart:BaseChart
{
    BoxTypeFirst BTF = new BoxTypeFirst();
    public override void Zoom()
    {
        if (BTF.AreaSeries != null)
        {

        }
    }
}
public class SlowChart:BaseChart,IRectangle
{
    BoxTypeFirst BTF = new BoxTypeFirst();

    public override void Zoom()
    {
        if (BTF.CandleStickSeries != null)
        {

        }
    }
    public void AddRectangles()
    {

    }
}
class Program
{
    static void Main(string[] args)
    {
        BaseChart BS = new SlowChart();
        BS.Zoom();
        BS = new FastChart();
        BS.Zoom();
    }
}
}

【问题讨论】:

  • 您的图表可以包含多个系列类型吗? (如果是:Visitor-Pattern - 如果不是,则使您的派生图表在使用的系列类型中通用〜>如果完全需要,则否)
  • @A191919 根据您的描述,您并没有避免使用 Arrow 反模式。这种反模式必然需要嵌套 If 语句。我认为您应该考虑更改误导性标题。
  • @Carsten 不,它应该只有一种系列类型。我会尝试通用的。
  • @Ian 我应该写什么标题?
  • @A191919 ... 不涉及任何这些词的东西!

标签: c# oop solid-principles


【解决方案1】:

作为一个选项,您可以让 BaseChart 它使用什么系列并在那里检查 null。

public abstract class BaseChart
{
    public void Zoom()
    {
        if (Series != null)
        {
            OnZoom();
        }
    }

    protected abstract void OnZoom();

    public virtual void DrawChart()
    {
    }

    protected abstract object Series { get; }

    public virtual void GetPoints()
    {
    }
}

public class FastChart : BaseChart
{
    private readonly BoxTypeFirst BTF = new BoxTypeFirst();

    protected override void OnZoom()
    {
        throw new System.NotImplementedException();
    }

    protected override object Series
    {
        get { return BTF.AreaSeries; }
    }
}

public class SlowChart : BaseChart
{
    private readonly BoxTypeFirst BTF = new BoxTypeFirst();

    protected override void OnZoom()
    {
        throw new System.NotImplementedException();
    }

    protected override object Series
    {
        get { return BTF.CandleStickSeries; }
    }
}

但无论如何,您的代码设计有异味。我没有看到全貌,所以我不能说太多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-04
    • 2020-10-04
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-25
    相关资源
    最近更新 更多