3

我正在为内部需求编写 API,因此易用性是首要任务之一。我想知道我是否像下面的例子那样把它推得太远了。

解决逆大地测量问题的函数采用两点的地理坐标,并计算它们之间的距离以及从每个点到另一个点的方位角(角度)。

易于使用优化的传统解决方案可能看起来像(请不要介意名称的长度,我知道还有改进的余地):

class Angle;
class GeoCoordinate;
...

struct InverseGeodeticOut
{
    Angle forwardAzimuth;
    Angle backAzimuth;
    double distance;
};

InverseGeodeticOut inverseGeodetic(const GeoCoordinate &firstPoint,
                                   const GeoCoordinate &secondPoint);
// or
void inverseGeodetic(const GeoCoordinate &firstPoint,
                     const GeoCoordinate &secondPoint,
                     InverseGeodeticOut *result);

我的问题是,进一步为用户节省一些输入是否合理:

class Angle;
class GeoCoordinate;
...

struct InverseGeodetic
{
    InverseGeodetic();
    InverseGeodetic(const GeoCoordinate &firstPoint,
                    const GeoCoordinate &secondPoint);

    Angle forwardAzimuth;
    Angle backAzimuth;
    double distance;
};

// so the usages would be
InverseGeodeticOut inverse = inverseGeodetic(firstPoint, secondPoint);

InverseGeodeticOut inverse;
inverseGeodetic(firstPoint, secondPoint, &inverse);

InverseGeodetic inverse(firstPoint, secondPoint);

也许在这个特殊的例子中,差异太小了,不值得一提,但我想知道这样的结构总体上是否还可以。

4

1 回答 1

2

我喜欢您的第二个代码示例,尽管我发现公共构造函数有点令人困惑。特别是如果有其他方法来构造InverseGeodetic. 我宁愿使用静态工厂方法来构造它。这样你就可以给方法一个更有意义的名字:

struct InverseGeodetic
{
    Angle forwardAzimuth;
    Angle backAzimuth;
    double distance;

    static InverseGeodetic FromTwoPoints(const GeoCoordinate &, const GeoCoordinate &);
};

// usage
auto inverse = InverseGeodetic::FromTwoPoints(a, b);

但这可能是我的 C# 背景。

于 2013-05-29T20:30:50.087 回答