Question

The title might be a bit undescriptive, but I'll try to explain the situation as clearly as possible in here. I created a class that will be inherited by a class made by someone else. My class contains a shape struct variable using the IShape interface I created, like so:

IShape Bounds = new Rectangle(/*arguments go here*/);

How can I let the end-user have control over what shape to use while still allowing him access to methods and fields for the chosen shape (without unboxing 24/7)? Basically I want the user to pick the shape he desires, but still have the shape in IShape form to perform stuff like collisions on it. E.g.:

Bounds = new Triangle(/*arguments*/);
Bounds.Point3 = new Point(20f, 20f); //this property would be from the Triangle : IShape struct only and is not inherited from IShape

Is there a way or workaround to keep this easy for the end-user?

Was it helpful?

Solution

One possible solution would be to use a generic class with an interface constraint. So the declaration would look something like:

public class MyClass<TShape> where TShape : IShape
{
    protected TShape Bounds;
    //Rest of class
}

Alternatively, you could provide access to the shape through a generic method which would centralize where the casting is done:

public class MyClass
{
    private IShape Bounds;

    protected TShape GetBounds<TShape>() where TShape : IShape
    {
        return (TShape)Bounds; //Note this will throw an exception if the wrong shape is specified
    }
    //Rest of class
}

The former is more appropriate if the base class should take responsibility for knowing its own shape type. The latter is more appropriate if the base class should remain ignorant of its shape type while the inheriting class (or calling class if you make things public) should know.

However, while one of these solutions might fit your situation pretty well, having to do it at all is a bit of a code smell. There's not much point to using an interface if your classes are going to have to know and track the specific concrete class most of the time. The best solution might be changing your design, though it's hard to give advice on that without seeing more of what you have.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top