D
Dan Vogel
I'd like to find an elegant solution to the problem of calling a
certain function based on the types of two parameters. In my case,
the functions compute the distance between different types of
geometric objects, and I want to call the function that gives me the
most accurate distance.
I have a base class called Shape with basic functionality:
Shape
Then I have the following geometric types derived from Shape (not
always directly). Note I'm simplifying things here, not necessarily
the actual derived types:
Ellipse : Shape;
Circle : Ellipse;
Polyline : Shape;
BezierLine : Polygon;
Text : Shape;
Elsewhere in my code I have an ArrayList of Shapes, and I want to
compute the most accurate distance between any pair of shapes. Let's
say I call a static function called AccurateDistance:
distance = AccurateDistance(shape1, shape2)
I want this to select from a list of static functions that know how to
compute the most accurate distance between any combination of two
shapes:
Distance = Distance(Ellipse shape1, Polyline shape2)
Distance = Distance(Circle shape1, Text shape2)
Distance = Distance(BezierLine shape1, Polyline shape2)
etc ...
I know I could make a big switch statement, or do a bunch of if else
if clauses to dispatch to the best function based on the types of
shape1 and shape2 passed into AccurateDistance. But this seems
tedious and with more shapes added will become very difficult to
maintain. There must be a better solution to this problem. Perhaps
some way of adding the Distance calculation into the Shape classes
themselves? I thought about using delegates, but they need exactly
the same parameters in the param list don't they? Maybe someway of
using reflection?
Any help/ideas is much appreciated,
dan
certain function based on the types of two parameters. In my case,
the functions compute the distance between different types of
geometric objects, and I want to call the function that gives me the
most accurate distance.
I have a base class called Shape with basic functionality:
Shape
Then I have the following geometric types derived from Shape (not
always directly). Note I'm simplifying things here, not necessarily
the actual derived types:
Ellipse : Shape;
Circle : Ellipse;
Polyline : Shape;
BezierLine : Polygon;
Text : Shape;
Elsewhere in my code I have an ArrayList of Shapes, and I want to
compute the most accurate distance between any pair of shapes. Let's
say I call a static function called AccurateDistance:
distance = AccurateDistance(shape1, shape2)
I want this to select from a list of static functions that know how to
compute the most accurate distance between any combination of two
shapes:
Distance = Distance(Ellipse shape1, Polyline shape2)
Distance = Distance(Circle shape1, Text shape2)
Distance = Distance(BezierLine shape1, Polyline shape2)
etc ...
I know I could make a big switch statement, or do a bunch of if else
if clauses to dispatch to the best function based on the types of
shape1 and shape2 passed into AccurateDistance. But this seems
tedious and with more shapes added will become very difficult to
maintain. There must be a better solution to this problem. Perhaps
some way of adding the Distance calculation into the Shape classes
themselves? I thought about using delegates, but they need exactly
the same parameters in the param list don't they? Maybe someway of
using reflection?
Any help/ideas is much appreciated,
dan