Separated (decoupled) API (a creator class) for the classes of a library with constructots having different signature
I have a library for 3D geometry.开发者_运维技巧 The library has classes like Point, Vector, Axis, Plane etc. The library has an interface and implementation hierarchy but simply all classes inherits from GeometryObject class.
class Point : public GeometryObject;
class Vector : public GeometryObject;
class Axis : public GeometryObject;
class LinePiece : public GeometryObject;
class Plane : public GeometryObject;
Each class has a numbber of constructors, for example:
Point::Point(){}
Point::Point(std::array<double, 3> theCoords){}
Vector::Vector(std::array<double, 3> theComponents){}
Axis::Axis(const Point& thePoint1, const Point& thePoint2){}
Axis::Axis(const Point& thePassingPoint, const Vector& theDirectionVector){}
Plane::Plane(const Point& thePoint1, const Point& thePoint2, const Point& thePoint3){}
Plane::Plane(const Point& thePassingPoint, const Vector& theNormalVector){}
As seen, the constructors have different signature. Only Point class has a default ctor.
I want to create a creator class to act as the API of the library. I want the creator class to have static methods to create geometry objects:
class Creator {
static GeometryObject create(...);
}
or
class Creator {
static Point createPoint(...);
static Vector createVector(...);
static Axis createAxis(...);
}
I left "..." for the parameters as the constructors vary for each object. A direct solution is to create a static member function for each constructor:
class Creator {
static Axis createAxis(const Point& thePoint1, const Point& thePoint2);
static Axis createAxis(const Point& thePassingPoint, const Vector& theDirectionVector);
}
However, the creator class is fully coupled with the library implementation. Any change in the library must be reflected to Creator too. So its useless. I studied abstract factory, factory method and builder design patterns. But i could not manage the varying constructor problem. Builder design pattern looks the best fit but i could not make it.
How can i create Creator class which is separated (decoupled) from the implementation of the geometry library?
Note: Only the Point has a default ctor as i mentioned. The other objects dont as physically not possible. For example a Vector cannot have all components zero. I could assign default values. For example, for the Plane, a default value would be the x-y plane of the global coordinate system. But, a Plane object has two members: a Point and a Vector. Hence, when i create the default Plane, i have to create a Point and a Vector. However, the user of the library would not be aware of these Point and Vector objects which is i think not a good design. Another solution is to have deault constructors which create uncomplete objects. But this breakes RAII and is not preferable. Hence, i removed default ctors accept for the Point.
精彩评论