Skip to main content
edited title
Link
Robert Harvey
  • 200.7k
  • 55
  • 470
  • 683

What is the design pattern is best suitable with this context?

Source Link

What is the design pattern with this context?

I have a base class Shape, and drived classes Line Segment, Arc, B-spline,... I want to find the intersection point between these shapes. I would like:

Shape* a = new LineSegment(); Shape* b = new Arc(); bool did_intersect = a->Intersect(b); 

I don't like this design in Intersect:

bool LineSegment(Shape* s) { Arc* arc = dynamic_cast<Arc*>(s); if (NULL != arc) return LineSegmentArcIntersection(this, arc); BSpline* bspline = dynamic_cast<BSpline*>(s); if (NULL != bspline) return LineSegmentBSplineIntersection(this, bspline); ... } 

So, What design pattern is best suitable with this context?