一个小的O-O设计模式问题...... [英] A little O-O design pattern question...

查看:61
本文介绍了一个小的O-O设计模式问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很想知道是否有人对这个小设计问题有任何意见 -


我正在用C#做一个计算天文库,纯粹是供我自己使用,

和其中一件经常发生的事情是将坐标从一个参照系转换为另一个参考坐标:

黄道中的球面坐标参考框架,赤道框架中的球面坐标

参考,三维直方坐标,银河坐标,

水平坐标,以及开启和关闭。这个问题涉及到哪里最自然地转换方法。


为了说明这个问题,我把它归结为一个简单的案例:


考虑2D平面坐标:你有笛卡儿(矩形)坐标

(X和Y)和极坐标(角度和半径矢量)。

之间的转换是众所周知且简单的。所以你可以把

的直角坐标(Point的子类,也许,命名为

CartesianCoords),将变换应用到X和Y,然后走出极地

坐标。反之亦然。


那么转换方法最佳位置在哪里?这里有一些

的可能性:


1.静态转换函数,如CartesianToPolar(CartesianCoords cc)

返回一个PolarCoordinates对象;


2. CartesianCoords对象上的实例方法返回一个

PolarCoordinates对象;


3. A PolarCoordinates构造函数采用CartesianCoords对象,

应用转换并从转换结果中填充其数据成员。


不可否认,对于上面的例子来说,这是一个相当小的一点,但是当你的b $ b有六个不同的坐标系,在所有对之间有潜在的转换

'组合爆炸可能会变得丑陋。

另外,虽然上面描述的转换除了坐标之外不需要其他信息

,但是一些天文变换需要

附加参数。


A. ny想法?


或者如果你不认为这是发布这样一个问题的正确位置,

如何建议更多适当的新闻组或网站(我没有找到一个真正的自然场所,快速搜索新闻组名称)?


谢谢,
Tom Dacon

Dacon软件咨询

I''m curious to see if anyone has an opinion on this little design question -

I''m doing a computational astronomy library in C#, purely for my own use,
and one of the things that happens regularly is conversion of coordinates
from one frame of reference to another: spherical coordinates in the
ecliptic frame of reference, spherical coordinates in the equatorial frame
of reference, 3D rectangular coordinates in either, galactic coordinates,
horizontal coordinates, and on and on. The question relates to where to site
the conversion methods most naturally.

To illustrate the question, I''ve boiled it down to a simple case:

Consider 2D plane coordinates: you have Cartesian (rectangular) coordinates
(X and Y) and polar coordinates (angle and radius vector). The
transformations between them are well-known and simple. So you can take
rectangular coordinates (a subclass of Point, perhaps, named
CartesianCoords), apply the transformation to X and Y, and get out polar
coordinates. And vice versa.

So where do the conversion methods best reside? Here are some
possibilities:

1. A static conversion function such as CartesianToPolar(CartesianCoords cc)
returning a PolarCoordinates object;

2. An instance method on a CartesianCoords object returning a
PolarCoordinates object;

3. A PolarCoordinates constructor taking a CartesianCoords object, which
applies the conversion and populates its data members from the outcome of
the transformation.

Admittedly this is a fairly minor point, for the example above, but when you
have a half-dozen different coordinate systems, with potential conversions
between all pairs, there''s a combinatorial explosion that might get ugly.
Also, while the transformation described above needs no other information
besides the coordinates, some of the astronomical transformations require
additional parameters.

Any thoughts?

Or if you don''t think this is the right place to post a question like this,
how about a suggestion for a more appropriate newsgroup or site (I didn''t
find a real natural venue in a quick search of the newsgroup names)?

Thanks,
Tom Dacon
Dacon Software Consulting

推荐答案

我建​​议写一个班级各种坐标,然后在等价的之间添加

转换运算符。


因此,这将允许您指定采用PolarCoordinate的方法,如果

用户通过了CartesianCoordinate,它将自动转换。


对于那些带参数的人,我想你会想要定义

采用坐标和附加参数的构造函数。


Tom Dacon <为了** @ t-dacons.com>在消息中写道

新闻:O4 ************** @ TK2MSFTNGP09.phx.gbl ...
I would suggest writing a class for each kind of coordinate, and then add
conversion operators between the ones that are equivalent.

So, that would allow you to specify a method taking PolarCoordinate, and if
the user passed a CartesianCoordinate, it would be converted automatically.

For the ones that take parameters, I think you''ll want to define
constructors that take the coordinate and the additional parameters.

"Tom Dacon" <To**@t-dacons.com> wrote in message
news:O4**************@TK2MSFTNGP09.phx.gbl...
我很好奇看看是否有人对这个小设计有一个意见
问题 -
我正在用C#做一个计算天文学库,纯粹是为了我自己的用途,
和其中一件事情发生了定期是从一个参照系到另一个参考系的坐标转换:黄道参考框架中的球面坐标,赤道框架中的球面坐标,参考中的三维直角坐标,银河系坐标,水平坐标,以及开启和关闭。这个问题涉及到b $ b网站最自然的转换方法。

为了说明这个问题,我把它归结为一个简单的案例:

考虑2D平面坐标:你有笛卡儿(矩形)
坐标(X和Y)和极坐标(角度和半径矢量)。它们之间的转换是众所周知和简单的。所以你可以采用
直角坐标(Point的子类,也许,名为
CartesianCoords),将变换应用到X和Y,并获得极坐标。反之亦然。

那么转换方法最好的位置在哪里?以下是一些可能性:

1.静态转换函数,如CartesianToPolar(CartesianCoords
cc)返回PolarCoordinates对象;

2.返回PolarCoordinates对象的CartesianCoords对象上的实例方法;

3.使用CartesianCoords对象的PolarCoordinates构造函数,该对象应用转换并从其结果中填充其数据成员
转换。

不可否认,对于上面的例子来说,这是一个相当小的一点,但是当
你有六个不同的坐标系,有潜在的转换
在所有对之间,存在可能变得难看的组合爆炸。
此外,虽然上述变换除了坐标之外不需要其他信息,但是一些天文变换需要额外的参数。

任何想法?

或者如果你不认为这是正确的位置pos像
这样的问题,对于一个更合适的新闻组或网站的建议怎么样(我没有在快速搜索新闻组名称时找到真正的自然场所)?
<谢谢,
Tom Dacon
Dacon软件咨询

I''m curious to see if anyone has an opinion on this little design question -
I''m doing a computational astronomy library in C#, purely for my own use,
and one of the things that happens regularly is conversion of coordinates
from one frame of reference to another: spherical coordinates in the
ecliptic frame of reference, spherical coordinates in the equatorial frame
of reference, 3D rectangular coordinates in either, galactic coordinates,
horizontal coordinates, and on and on. The question relates to where to site the conversion methods most naturally.

To illustrate the question, I''ve boiled it down to a simple case:

Consider 2D plane coordinates: you have Cartesian (rectangular) coordinates (X and Y) and polar coordinates (angle and radius vector). The
transformations between them are well-known and simple. So you can take
rectangular coordinates (a subclass of Point, perhaps, named
CartesianCoords), apply the transformation to X and Y, and get out polar
coordinates. And vice versa.

So where do the conversion methods best reside? Here are some
possibilities:

1. A static conversion function such as CartesianToPolar(CartesianCoords cc) returning a PolarCoordinates object;

2. An instance method on a CartesianCoords object returning a
PolarCoordinates object;

3. A PolarCoordinates constructor taking a CartesianCoords object, which
applies the conversion and populates its data members from the outcome of
the transformation.

Admittedly this is a fairly minor point, for the example above, but when you have a half-dozen different coordinate systems, with potential conversions
between all pairs, there''s a combinatorial explosion that might get ugly.
Also, while the transformation described above needs no other information
besides the coordinates, some of the astronomical transformations require
additional parameters.

Any thoughts?

Or if you don''t think this is the right place to post a question like this, how about a suggestion for a more appropriate newsgroup or site (I didn''t
find a real natural venue in a quick search of the newsgroup names)?

Thanks,
Tom Dacon
Dacon Software Consulting



从阅读它,我认为共享[静态]方法可能是你最好的利息,特别是如果你不需要特别引用任何东西。


你可以创建继承的基类,但我不知道你是否真的需要
,除非两者之间有明确的层次结构。


但是只需构建一个外部库(就像你做的那样,创建一个名为

计算的类并添加共享方法来完成你的肮脏工作......但我不是'

在这一点上看到对象继承的真正需要,也许我错了。


希望它有所帮助。


-CJ

Tom Dacon " <为了** @ t-dacons.com>在消息中写道

新闻:O4 ************** @ TK2MSFTNGP09.phx.gbl ...
From reading it, I think a shared [static] method may be in your best
interests especially if you don''t need to reference anything in particular.

You could create a base class to inherit off of, but I don''t know if you
really need to, unless there is a clear heirarchy between the two.

But simply build a external library (like your doing, create a class called
computations and add shared methods to do your dirty work... But I dont''
see a real need for object inheritance at this point, maybe I''m wrong.

Hope it helps.

-CJ
"Tom Dacon" <To**@t-dacons.com> wrote in message
news:O4**************@TK2MSFTNGP09.phx.gbl...
我很好奇看看是否有人对这个小设计有一个意见
问题 -
我正在用C#做一个计算天文学库,纯粹是为了我自己的用途,
和其中一件事情发生了定期是从一个参照系到另一个参考系的坐标转换:黄道参考框架中的球面坐标,赤道框架中的球面坐标,参考中的三维直角坐标,银河系坐标,水平坐标,以及开启和关闭。这个问题涉及到b $ b网站最自然的转换方法。

为了说明这个问题,我把它归结为一个简单的案例:

考虑2D平面坐标:你有笛卡儿(矩形)
坐标(X和Y)和极坐标(角度和半径矢量)。它们之间的转换是众所周知和简单的。所以你可以采用
直角坐标(Point的子类,也许,名为
CartesianCoords),将变换应用到X和Y,并获得极坐标。反之亦然。

那么转换方法最好的位置在哪里?以下是一些可能性:

1.静态转换函数,如CartesianToPolar(CartesianCoords
cc)返回PolarCoordinates对象;

2.返回PolarCoordinates对象的CartesianCoords对象上的实例方法;

3.使用CartesianCoords对象的PolarCoordinates构造函数,该对象应用转换并从其结果中填充其数据成员
转换。

不可否认,对于上面的例子来说,这是一个相当小的一点,但是当
你有六个不同的坐标系,有潜在的转换
在所有对之间,存在可能变得难看的组合爆炸。
此外,虽然上述变换除了坐标之外不需要其他信息,但是一些天文变换需要额外的参数。

任何想法?

或者如果你不认为这是正确的位置pos像
这样的问题,对于一个更合适的新闻组或网站的建议怎么样(我没有在快速搜索新闻组名称时找到真正的自然场所)?
<谢谢,
Tom Dacon
Dacon软件咨询

I''m curious to see if anyone has an opinion on this little design question -
I''m doing a computational astronomy library in C#, purely for my own use,
and one of the things that happens regularly is conversion of coordinates
from one frame of reference to another: spherical coordinates in the
ecliptic frame of reference, spherical coordinates in the equatorial frame
of reference, 3D rectangular coordinates in either, galactic coordinates,
horizontal coordinates, and on and on. The question relates to where to site the conversion methods most naturally.

To illustrate the question, I''ve boiled it down to a simple case:

Consider 2D plane coordinates: you have Cartesian (rectangular) coordinates (X and Y) and polar coordinates (angle and radius vector). The
transformations between them are well-known and simple. So you can take
rectangular coordinates (a subclass of Point, perhaps, named
CartesianCoords), apply the transformation to X and Y, and get out polar
coordinates. And vice versa.

So where do the conversion methods best reside? Here are some
possibilities:

1. A static conversion function such as CartesianToPolar(CartesianCoords cc) returning a PolarCoordinates object;

2. An instance method on a CartesianCoords object returning a
PolarCoordinates object;

3. A PolarCoordinates constructor taking a CartesianCoords object, which
applies the conversion and populates its data members from the outcome of
the transformation.

Admittedly this is a fairly minor point, for the example above, but when you have a half-dozen different coordinate systems, with potential conversions
between all pairs, there''s a combinatorial explosion that might get ugly.
Also, while the transformation described above needs no other information
besides the coordinates, some of the astronomical transformations require
additional parameters.

Any thoughts?

Or if you don''t think this is the right place to post a question like this, how about a suggestion for a more appropriate newsgroup or site (I didn''t
find a real natural venue in a quick search of the newsgroup names)?

Thanks,
Tom Dacon
Dacon Software Consulting



嗨Tom,


为什么不注意框架本身处理类似

情况的方式,它提供一个转换类来处理从一个转换

基本类型到其他,如果你看到这个类与你想要的类似。它是
有例如ToDecimal,它返回一个十进制结构,并且对于CLR中的每个类型都有一个版本

,你也可以这样做,创建一个类

用于定义类似ToCartesian的方法的CoordinateConvert需要




CartesianClass ToCartesian(SphericClass o);

CartesianClass ToCartesian(GalacticClass o);

CartesianClass ToCartesian(EclipticClass o);


希望这个帮助,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局

Tom Dacon <为了** @ t-dacons.com>在消息中写道

新闻:O4 ************** @ TK2MSFTNGP09.phx.gbl ...
Hi Tom,

Why do not take note of the way the framework itself handle a similar
situation, It provide a Convert class that deal with converting from one
basic type to other, if you see this class is similar to what you want. It
has for example ToDecimal that return a decimal struct and has one version
for each of the types in the CLR, you could do the same, create a class
CoordinateConvert that define a method like ToCartesian overloaded as
needed:

CartesianClass ToCartesian( SphericClass o);
CartesianClass ToCartesian( GalacticClass o);
CartesianClass ToCartesian( EclipticClass o);

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Tom Dacon" <To**@t-dacons.com> wrote in message
news:O4**************@TK2MSFTNGP09.phx.gbl...
我很好奇看看是否有人对这个小设计有一个意见
问题 -
我正在用C#做一个计算天文学库,纯粹是为了我自己的用途,
和其中一件事情发生了定期是从一个参照系到另一个参考系的坐标转换:黄道参考框架中的球面坐标,赤道框架中的球面坐标,参考中的三维直角坐标,银河系坐标,水平坐标,以及开启和关闭。这个问题涉及到b $ b网站最自然的转换方法。

为了说明这个问题,我把它归结为一个简单的案例:

考虑2D平面坐标:你有笛卡儿(矩形)
坐标(X和Y)和极坐标(角度和半径矢量)。它们之间的转换是众所周知和简单的。所以你可以采用
直角坐标(Point的子类,也许,名为
CartesianCoords),将变换应用到X和Y,并获得极坐标。反之亦然。

那么转换方法最好的位置在哪里?以下是一些可能性:

1.静态转换函数,如CartesianToPolar(CartesianCoords
cc)返回PolarCoordinates对象;

2.返回PolarCoordinates对象的CartesianCoords对象上的实例方法;

3.使用CartesianCoords对象的PolarCoordinates构造函数,该对象应用转换并从其结果中填充其数据成员
转换。

不可否认,对于上面的例子来说,这是一个相当小的一点,但是当
你有六个不同的坐标系,有潜在的转换
在所有对之间,存在可能变得难看的组合爆炸。
此外,虽然上述变换除了坐标之外不需要其他信息,但是一些天文变换需要额外的参数。

任何想法?

或者如果你不认为这是正确的位置pos像
这样的问题,对于一个更合适的新闻组或网站的建议怎么样(我没有在快速搜索新闻组名称时找到真正的自然场所)?
<谢谢,
Tom Dacon
Dacon软件咨询

I''m curious to see if anyone has an opinion on this little design question -
I''m doing a computational astronomy library in C#, purely for my own use,
and one of the things that happens regularly is conversion of coordinates
from one frame of reference to another: spherical coordinates in the
ecliptic frame of reference, spherical coordinates in the equatorial frame
of reference, 3D rectangular coordinates in either, galactic coordinates,
horizontal coordinates, and on and on. The question relates to where to site the conversion methods most naturally.

To illustrate the question, I''ve boiled it down to a simple case:

Consider 2D plane coordinates: you have Cartesian (rectangular) coordinates (X and Y) and polar coordinates (angle and radius vector). The
transformations between them are well-known and simple. So you can take
rectangular coordinates (a subclass of Point, perhaps, named
CartesianCoords), apply the transformation to X and Y, and get out polar
coordinates. And vice versa.

So where do the conversion methods best reside? Here are some
possibilities:

1. A static conversion function such as CartesianToPolar(CartesianCoords cc) returning a PolarCoordinates object;

2. An instance method on a CartesianCoords object returning a
PolarCoordinates object;

3. A PolarCoordinates constructor taking a CartesianCoords object, which
applies the conversion and populates its data members from the outcome of
the transformation.

Admittedly this is a fairly minor point, for the example above, but when you have a half-dozen different coordinate systems, with potential conversions
between all pairs, there''s a combinatorial explosion that might get ugly.
Also, while the transformation described above needs no other information
besides the coordinates, some of the astronomical transformations require
additional parameters.

Any thoughts?

Or if you don''t think this is the right place to post a question like this, how about a suggestion for a more appropriate newsgroup or site (I didn''t
find a real natural venue in a quick search of the newsgroup names)?

Thanks,
Tom Dacon
Dacon Software Consulting



这篇关于一个小的O-O设计模式问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆