简体   繁体   English

OOP编程风格决定

[英]OOP Programming style decision

I just have a quick question about an OOP programming I've been having difficulty deciding. 对于我一直难以决定的OOP编程,我只是有一个简单的问题。 The premise is that I'm making a set of very simple geometric classes such as vertex and angle and vector objects, but one of the classes, the line class to be specific, is a little different. 前提是我要创建一组非常简单的几何类,例如顶点,角度和矢量对象,但是其中一个类(要特定的线类)略有不同。 It's basically just a collection of methods that I use one time only, I never actually save a line object for later use or recollection of data anywhere else in the program. 基本上,这只是我一次使用的方法的集合,我从来没有真正保存过行对象供以后在程序中的任何其他地方使用或收集数据。 An example usage to demonstrate my point would be this: 演示我的观点的一个示例用法是:

class Line:
    def __init__(self, vertex1, vertex2):
        self.start = vertex1
        self.end = vertex2
    def to_the_left(self, vertex):
        """Check to see if a vertex is to the left of the line segment."""
        #code stuff

data = Line(Vertex(0, 0), Vertex(10, 0)).to_the_left(Vertex(5, 5))

I only ever instantiate Line(Vertex(0, 0), Vertex(10, 0)) once to retrieve the data . 我只实例化Line(Vertex(0, 0), Vertex(10, 0))一次来检索data So I was thinking that I might as well just have a bunch of functions available instead of packing it all into a class, but then I was skeptical about doing that since there are a ton of methods that would have to be converted to functions. 所以我以为我可能只提供一堆函数,而不是将所有函数打包到一个类中,但是后来我对此持怀疑态度,因为有很多方法必须转换为函数。

Another thing I was thinking of doing was to make a Line class and then convert all it's methods into normal functions like so: 我想做的另一件事是制作一个Line类,然后将其所有方法转换为普通函数,如下所示:

#continuing from the code above
def to_the_left(line_start, line_end, vertex):
    return Line(line_start, line_end).to_the_left(vertex)
data = to_the_left(Vertex(0, 0), Vertex(10, 0), Vertex(5, 5))

Which method do you think I should use? 您认为我应该使用哪种方法?

I would opt for using an object as you might need to do multiple operations on Line. 我会选择使用一个对象,因为您可能需要在Line上执行多项操作。

For example you might compute the length, if it's to the left, and some other operation. 例如,您可以计算长度(如果在左侧)以及其他一些操作。 You might need to pass the Line around who knows. 您可能需要绕过谁知道的路线。

One thing you might want to consider is instead of using Line and Vertex, use Vector which acts as both. 您可能要考虑的一件事是,不要同时使用线和顶点,而要同时使用矢量。 If your vertex has x,y you can make a Vector that has x,y,w. 如果顶点具有x,y,则可以创建具有x,y,w的向量。 In this scheme w=1 for vertices and w=0 for Lines - it would simplify a lot of code. 在此方案中,对于顶点,w = 1;对于线,w = 0;这将简化大量代码。

Look up Homogenous coordinates to learn more 查找同质坐标以了解更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM