tags:

views:

65

answers:

2

We're writing an SDK for a CAD program and have run into a slight disagreement regarding a specific type of function (not just disagreement between different people, also disagreement between my two brain-halves).

Imagine there's a lot of classes for specific curve types (ellipse, circle, arc, line, bezier etc.). All of these classes can be represented by a Nurbs curve. So should we put the Circle->Nurbs function on Circle:

public NurbsCurve Circle.ToNurbsCurve()
{
  // Return a circular NurbsCurve or null if the Circle is invalid.
}

or should it be a static on NurbsCurve:

public static NurbsCurve NurbsCurve.CreateFromCircle(Circle)
{
  // Return a circular NurbsCurve or null if the Circle is invalid.
}
+1  A: 

I would put it in the Circle class, because it's the one that knows how it should be converted to a NurbsCurve. The NurbsCurve class isn't supposed to know about all specific types of curve. That way, if you create a new type of curve, you won't have to modify the NurbsCurve class.

BTW, I suggest you declare the ToNurbsCurve method in an interface implemented by all curves (or declare it virtual in an abstract base class)

Thomas Levesque
Thanks Thomas. Regarding the Interface comment, we unfortunately have many different ToXxxx() functions so using interfaces for all of them would become somewhat unwieldy.
David Rutten
+4  A: 

I think I'd go for the first (e.g. on the shape classes, maybe even with a common base class or interface like IConvertibleToNurbsCurve), because this makes it easier if you add other shapes later which are also convertible to a NurbsCurve.

The NurbsCurve seems to be less specialized and therefore should not "know" about the more specialized types IMHO.

Lucero
Thanks Lucero. good point about adding more classes later.
David Rutten