How to implement a method of a base class for every possible combination of its derived types : <p>I have the following Shape interface which is implemented by multiple other classes such as Rectangle, Circle, Triangle ...</p>

<pre><code>interface IShape{
    bool IsColliding(IShape other);
}
</code></pre>

<p>The method IsColliding is supposed to check whether a Shape is colliding with another or not, regardless of their concrete type.
However, each couple of shape (Rectangle/Rectangle, Rectangle/Circle, Circle/Triangle etc...) have its own implementation for this collision check.</p>

<p>I'm trying to find a good design solution for this problem.</p>

<p>The naive method would be to switch over the type of the "other" shape to call the correct implementation :</p>

<pre><code>class Rectangle : IShape{
    bool IsColliding(IShape other){
        if(other is Rectangle){
            return CollisionHandler.CheckRectangleVsRectangle(this,(Rectangle)other);
        }else if(other is Circle){
            return CollisionHandler.CheckRectangleVsCircle(this,(Circle)other);
        } else
            // etc ...
    }
}
</code></pre>

<p>But adding a new shape would mean modifying the method in every derived class to add the new case.</p>

<p>I also thought of calling a unique static method like this one :</p>

<pre><code>static bool IsColliding(IShape shapeA, IShape shapeB);
</code></pre>

<p>But even if it centralizes everything, it doubles the number of type-test to perform and I'd still have to add a new case in each first-level "if".</p>

<pre><code>if(shapeA is Rectangle){
    if(shapeB is Rectangle){
        // Rectangle VS Rectangle
    }else if(shapeB is Circle){
        // Rectangle VS Circle
    }else{
        // etc ...
    }
}else if(shapeA is Circle){
    if(shapeB is Rectangle){
        // Rectangle VS Circle
    }else{
        // etc ...
    }
} // etc ...
</code></pre>

<p>So, how could it be better designed ?</p>
