Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

类图(Class diagrams) #117

Open
dushaoshuai opened this issue Jul 14, 2023 · 0 comments
Open

类图(Class diagrams) #117

dushaoshuai opened this issue Jul 14, 2023 · 0 comments
Labels
design patterns diagrams draft Still only a draft Go The Go programming language.

Comments

@dushaoshuai
Copy link
Owner

dushaoshuai commented Jul 14, 2023

(本文主要关注 Relationships)
(想弄清楚这些关系怎样对应到 Go 里)
(!!!我好像还是没有分清楚聚合和组合的区别!!!)

定义关系

继承(Inheritance)

Is-A

子类型继承父类型。

classDiagram
    direction LR
    subtype --|> supertype : Inheritance
Loading

聚合(Aggregation)

Has-A

一种较弱的联系,两者可以相互独立存在,有各自的生命周期。

classDiagram
    direction LR
    component --o composite : Aggregation
Loading

组合(Composition)

Contains-A

一种较强的(强于聚合)联系,一方拥有另一方,两者不能独立存在。

classDiagram
    direction LR
    component --o composite : Composition
Loading

实现(Realization)

classDiagram
    direction LR
    class stringsReader["*strings.Reader"]
    class ioReader["io.Reader"]
    <<interface>> ioReader
    stringsReader ..|> ioReader : Realization
Loading

依赖(Dependency)

classDiagram
    direction LR
    A ..> B : Dependency
Loading

联系(Association)

TODO 就是表示有关系吧。

classDiagram
    direction LR
    A --> B
    C -- D
    E .. F
Loading

Go 中的关系如何表示

类型实现接口

个人感觉有两种表示方式,继承和实现,但我更偏向实现吧。

classDiagram
    direction LR
    class stringsReader["*strings.Reader"]
    class ioReader["io.Reader"]
    <<interface>> ioReader
    stringsReader ..|> ioReader : Realization
Loading
classDiagram
    direction LR
    class stringsReader["*strings.Reader"]
    class ioReader["io.Reader"]
    <<interface>> ioReader
    stringsReader --|> ioReader : Inheritance
Loading

结构体字段

有名字字段和匿名字段(嵌入)先都用组合表示吧。

(可能是没写过真正的 OOP 的原因,我感觉现在无法分辨聚合和组合。)

classDiagram
    direction LR
    class netIPConn["net.IPConn"]{
        - conn
    }
    class netconn["net.conn"]{
        - fd *netFD
    }
    class netnetFD["net.netFD"]{
        - pfd poll.FD
	- family      int
	- sotype      int
	- isConnected bool
	- net         string
	- laddr       Addr
	- raddr       Addr
    }
    netconn --* netIPConn : Composition
    netnetFD --* netconn: Composition
Loading

参见

@dushaoshuai dushaoshuai added draft Still only a draft design patterns diagrams Go The Go programming language. labels Jul 14, 2023
@dushaoshuai dushaoshuai changed the title Class diagrams 类图(Class diagrams) Jul 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design patterns diagrams draft Still only a draft Go The Go programming language.
Projects
None yet
Development

No branches or pull requests

1 participant