使用统一建模语言图表编程Go语言。

huangapple go评论72阅读模式
英文:

Programming Go, using Unified Modelling Language Diagrams

问题

我刚开始使用Go(GoLang),发现它是一种很棒的语言。然而,经过多年的UML和面向对象的方法,我发现对Go程序进行建模(逆向工程)有点问题,因为Go结构体包含属性/状态,但没有方法,并且使用结构体作为参数的方法/函数(即使是那些通过魔法使结构体看起来像对象的方法)也不包含方法或状态。

这是否意味着我应该使用另一种方法来建模Go程序,或者UML是否足以建模语言结构?

是的,我知道如果在结构体上使用方法,可以通过结构体和结构体方法的组合将UML中对象的行为映射到Go中,但我发现这是错误的,是一种范式不匹配。

是不是该是时候使用一种新的(不要想太多!)图表技术了,在这个新的世界中,行为不再由对象控制?行为能否在不涉及受影响状态的情况下进行建模?

更新:

我正在尝试使用数据流程图,看看它们是否更适合这种范式。到目前为止还不错,但当我对结构体的方法进行建模时,我觉得会遇到问题,因为在DFD中,它们被视为函数。:(

Go支持继承!!!啊啊啊啊啊!!!(脑袋被炸飞了。)你可以组合一个由另一个结构体组成的结构体,该结构体具有方法,然后子结构体现在继承了这些方法...你明白吗?我的脑袋都炸了。这意味着UML是有效的...完全有效,但感觉有点不舒服。

Go不支持继承,只是看起来像支持。:) 那就用DFD吧!

英文:

I have just started using Go (GoLang) and I am finding it a great language. However, after many years of UML and the Object Oriented methods, I find that modelling Go programs (Reverse engineering) is a bit problematic, in that Go Structs contain properties/state, but no methods, and methods/functions that use Structs as parameters (even the ones that do magic so that it makes a Struct look like an object), don't contain methods, or state.

Does this mean I should be using another Methodology to model a Go Program or does UML sufficiently model the language constructs?

Yes I know that if you use methods on the Structs that the behavior of an object in UML can be mapped into Go via a combination of a Struct and a Struct Method, but I am finding this to be wrong, an impedance mismatch in paradigms of sorts.

Is it time for a new (perish the thought!) diagramming technique, for the brave new world where behavior is no longer controlled by an object? can behavior be modeled without reference to the state that it is affecting?

Update:

I am trying Data Flow Diagrams out, to see if they fit better to the paradigm. So far so good, but I think I am going to come unstuck when I model the Methods of a Struct, the compromise in the DFD being that they are treated as Functions. 使用统一建模语言图表编程Go语言。

Go supports inheritance!!! arghhh!!! (head is blown clean off.) you can compose a Struct which is made of another Struct, which has methods, that the Sub Struct now inherits...you getting this? my mind is blown. Means that UML IS valid...fully but it feels dirty.

Go Does not support inheritance, it just appears so. 使用统一建模语言图表编程Go语言。 DFD's it is then!

答案1

得分: 8

方法在结构体定义之外声明并不重要;这只是一个微小的语法差异。这些方法与结构体类型一样属于结构体类型,就好像它们在大括号内部一样。(它们在大括号外部声明是因为方法不仅限于结构体。)

在使用UML与Go一起使用的真正潜在问题是,UML通常与传统的面向对象设计(即类层次结构)一起使用,而Go对继承和多态性采取了不同的方法。也许UML可以适应Go的类型系统-我对UML不够熟悉,无法确定-但是无论您是否继续使用UML,您的设计方法可能需要做一些改变。

Go不打算以Smalltalk和Java的一切皆为对象的风格来使用。典型的Go程序通常包含大量的过程化代码。如果您的设计过程侧重于对象建模,那么对于Go来说效果可能不好。

英文:

The fact that methods are declared outside the definition of the struct itself should not be significant; it is just a minor syntax difference. The methods belong to the struct type just as surely as if they were inside the braces. (They are declared outside the braces because methods aren't limited to structs.)

The real potential problem with using UML with Go is that UML is normally used with traditional object-oriented design (i.e. class hierarchies), and Go takes a different approach to inheritance and polymorphism. Maybe UML can be adapted to Go's type system—I'm not familiar enough with UML to say—but your design approach will probably need to change somewhat whether you continue using UML or not.

Go is not intended to be used in the everything-is-an-object style of Smalltalk and Java. Idiomatic Go programs generally contain a large percentage of procedural code. If your design process focuses on object modeling, it will not work well for Go.

答案2

得分: 5

UML仍然为组件、接口和数据的分析和设计提供了有用的工具。Go不是一种面向对象的语言,因此您无法使用继承、多态、方法等。您不需要一个新的范例,您可能需要一个旧的范例:结构化分析和结构化设计

英文:

UML still gives you tools useful for analysis and design of components, interfaces, and data. Go is not an OO language, so you cannot use inheritance, polymorphism, methods, etc. You don't need a new paradigm, you may need an old one: Structured Analysis and Structured Design.

答案3

得分: 0

Go结构体包含属性/状态,但没有方法,使用结构体作为参数的方法/函数(即使是那些通过魔法使结构体看起来像对象的方法)也不包含方法或状态。

正如你可能知道的,在C++中,你也可以在结构体上声明方法 - 就像在类中一样,唯一的区别是你不能使用访问修饰符。

在面向对象的语言中,你在类定义中声明类的方法,给人一种这些方法是类的一部分的感觉。但对于大多数语言来说,这并不是真的,而Go则明确表明了这一点。

当你在传统的面向对象编程语言中声明以下伪代码时:

class Foo {
    public function bar(x int) {
        // ...
    }
}

链接器将导出一个类似以下的函数:

Foo__bar(this Foo, x int)

然后当你执行以下操作时(假设fFoo的一个实例):

f.bar(3)

实际上(间接地,稍后会详细解释),你在执行以下操作:

Foo__bar(f, 3)

类实例本身只包含一个所谓的_vtable_,其中包含指向它实现和/或继承的方法的函数指针。

此外,方法不包含状态,至少在当代编程世界中是这样的。

> 这是否意味着我应该使用另一种方法来建模Go程序,或者UML足以建模语言结构?

UML应该足够。

> 是不是该是时候使用一种新的(不要想象!)图表技术,用于这个新的世界,其中行为不再由对象控制?

不需要。

> 行为是否可以在不涉及受影响状态的情况下进行建模?

是的,这就是接口的作用。

> 我正在尝试使用数据流程图,看看它们是否更适合这种范式。到目前为止还不错,但我认为当我对结构体的方法进行建模时,我可能会遇到困难,因为在DFD中它们被视为函数。:(

不要迷失在抽象中,打破它们。没有完美的范式,也永远不会有。

英文:

> Go Structs contain properties/state, but no methods, and
> methods/functions that use Structs as parameters (even the ones that
> do magic so that it makes a Struct look like an object), don't contain
> methods, or state.

As you may know, in C++ you can also declare methods on structs - just as in classes with the only difference that you won't be able to use access modifiers.

In OOP languages you declare methods of a class inside the class definition, giving the feeling that these methods are somehow part of the class. This is not true for most languages and Go makes this obvious.

When you declare something like the following pseudo-code in a traditional OOP language:

class Foo {
    public function bar(x int) {
        // ...
    }
}

the linker will export a function that will look something like:

Foo__bar(this Foo, x int)

When you then do (assume f is an instance of Foo):

f.bar(3)

you are in fact (and indirectly, more on that later) doing:

Foo__bar(f, 3)

The class instance itself will only contain a so called vtable with function pointers to the methods it implements and/or inherits.

Additionally, methods do not contain state, at least not in the contemporary programming world.

> Does this mean I should be using another Methodology to model a Go
> Program or does UML sufficiently model the language constructs?

UML should suffice.

> Is it time for a new (perish the thought!) diagramming technique, for
> the brave new world where behavior is no longer controlled by an
> object?

Naa.

> Can behavior be modeled without reference to the state that it is
> affecting?

Yes, that's what interfaces are for.

> I am trying Data Flow Diagrams out, to see if they fit better to the
> paradigm. So far so good, but I think I am going to come unstuck when
> I model the Methods of a struct, the compromise in the DFD being that
> they are treated as Functions. 使用统一建模语言图表编程Go语言。

Do not get lost in abstractions, break them. There is no perfect paradigm and there will never be.

答案4

得分: -1

如果你更喜欢规划和建模而不是编程,你应该坚持使用Java。

如果你更喜欢构建和维护实际代码和工作系统,你可以尝试在纸上或白板上规划你的Go程序,然后开始编程。

英文:

If you like planning and modeling more than programming you should just stick with Java.

If you like building and maintaining the actual code and working systems you should try just planning your Go program on a piece of paper or a whiteboard and get programming.

huangapple
  • 本文由 发表于 2013年7月30日 23:55:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/17951803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定