fedab
fedab

Reputation: 989

Create an Interface for existing classes?

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...

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?

Upvotes: 2

Views: 327

Answers (1)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391326

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.

For instance:

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);

Upvotes: 2

Related Questions