为现有类创建接口? [英] Create an Interface for existing classes?
问题描述
我用模式
绘制屏幕上的某些系列。你可以创建一个像供LineSeries
或 CurveSeries
A系列,并将其添加到模型中:模型。 Series.Add(myLineSeries);
。该系列有一个属性,所有点都保存分数
,您可以轻松地添加一些: myLineSeries.Points.Add(...);
。 模式
需要的是从基类系列
继承的系列。
I use a Model
that draws some series on a screen. You can create a Series like LineSeries
or CurveSeries
and add them to the Model: Model.Series.Add(myLineSeries);
. The Series have a property Points
, where all points are saved, you can easily add some: myLineSeries.Points.Add(...);
. Model
takes all series that are inherited from the base class Series
.
但是,当我加入它模型,并希望以后加点分,我一定要投它回来:
But when i added it to Model and want to add some points later, i have to cast it back:
//Model.Series[0].Points does not exists. I have to cast them...
var lineSeries = (Model.Series[0] as LineSeries);
if(lineSeries != null)
lineSeries.Points.Add(...);
var curveSeries = (Model.Series[0] as CurveSeries);
//Same code for CurveSeries
//Same code for other series types...
$ B $ //相同的代码b
所以我不得不一遍又一遍地做同样的代码。如果可以访问源代码,你可以说:让我们创建一个点接口,或一个基类,所有具有这样的性质分。该系列只需要继承基类或接口。现在,你可以说:
So i have to do the same code over and over again. If you can access the source code, you can say: lets create a "point interface" or a base class where all have the property points. The series only have to inherit the base class or interface. Now you can say:
var Series = (Model.Series[0] as IPointSeries);
//Code only one time :)
但现在我有没有获得系列和模型的源代码。我不能改变型号也不系列也不抽象系列基类实现。
But now i have no access to the source code of Series and Model. I can not change Model nor series nor abstract series base class implementation.
有没有做到这一点不改变型号或系列的源代码中的常见方式?像现有的系列来访问他们所有的相同的方式创建一个界面呢?
Is there a common way to do this without changing source code of Model or Series? Like creating a interface for existing series to access them all the same way?
推荐答案
您可以编写一组包装类的,所以。至少你没有写所有的代码寂寂多种基础类型不止一次
You can write a set of wrapper classes, so that at least you don't have to write all the code knowing about multiple underlying types more than once.
例如:
public class PointSeries
{
public List<Point> Points { get { return new List<Point>(); } }
}
public class CurveSeries
{
public List<Point> Points { get { return new List<Point>(); } }
}
public interface ISeriesWrapper
{
List<Point> Points { get; }
}
public class PointSeriesWrapper : ISeriesWrapper
{
public PointSeriesWrapper(PointSeries series)
{
_Series = series;
}
private PointSeries _Series;
public List<Point> Points { get { return _Series.Points; } }
}
public class CurveSeriesWrapper : ISeriesWrapper
{
public CurveSeriesWrapper(CurveSeries series)
{
_Series = series;
}
private CurveSeries _Series;
public List<Point> Points { get { return _Series.Points; } }
}
public static class SeriesWrapper
{
public static ISeriesWrapper Create(object series)
{
var pointSeries = series as PointSeries;
if (pointSeries != null)
return new PointSeriesWrapper(pointSeries);
var curveSeries = series as CurveSeries;
if (curveSeries != null)
return new CurveSeriesWrapper(curveSeries);
throw new NotSupportedException();
}
}
例如:
Example of usage:
var pointSeries = SeriesWrapper.Create(Model.Series[0]);
lineSeries.Points.AddRange(pointSeries.Points);
这篇关于为现有类创建接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!