【发布时间】: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