[读书笔记]UML基本概念---Representing Relationships

本文介绍了UML中四种重要的关系:继承、实现、依赖和关联,包括它们在Java中的映射。继承对应Java中的extends关键字,实现对应implements,依赖通过虚线箭头表示,关联则描述了类之间的结构关系。此外,还详细解释了多态性和聚合、组合等概念。

Representing Relationships

UML and Java may be languages for software development, but they exist in different planes of reality.  UML occupies the visual world, whereas Java is textual in nature. 

UML is also richer than Java in the sense that it offers more abstract and powerful ways of expressing a particular concept or relationship.  However, there is generally only one way to represent that concept or relationship in the Java language.

 

1.     Inheritance

The UML concept of generalization is analogous to inheritance in Java.  Generalization maps directly to the extends keyword and is shown visually via a line with a triangle at the end nearest the super class.

 

 

2.     Realization

In Java, a class may implement one or more interfaces.  The Java keyword implements maps to the concept of realization in UML.

 

 

3.     Dependency

Anytime a class uses another class in some fashion, a dependency exists between the two (classes---add by Fitzwilliam,也就是说,dependency之间的关系). 

In the UML, a dependency is shown via a dotted line with an arrow touching the class that is causing the dependency.

A dependency exists if a class:

l         Has a local variable based on another class

l         Has a reference to an object directly

l         Has a reference to an object indirectly, for example, via some operation parameters

l         Uses a class’s static operation

 

Dependency relationships also exist between packages containing classes that are related.  Dependencies between packages are shown via a dotted line with an arrowhead. 

 

 

4.     Association

Conceptually, an association between two classes signifies that some sort of structural relationship exists between the classes. 

 

 

1)     Unidirectional Association

Unidirectional association is shown with an arrow on a simple line’s end.

A unidirectional association implies that an object (not a class--- add by Fitzwilliam) of the class from which the arrow is originating may invoke methods on the class towards which the arrow is pointing.  In Java, this manifests itself as an instance variable on the class that may invoke methods.

 

2)     Bi-directional Association

Bi-directional association is shown with a simple line.

A bi-directional association simply means that either object in the association may invoke methods on the other.  In Java, this results in an instance variable on each class based on the type of the other class.

 

 

Each end of the association is a role in UML terminology and may be named.

 

注:原书的图如下图所示,但是我用的软件(JUDE)画出的效果如上图所示,我认为都可以。软件的区别,注意一下。

 

 

Multiplicity

Of course, objects in a class may have multiple associations with objects in another class.

In terms of Java implementation, multiplicity manifests itself as a multivalued instance variable.

 

注:同上所注

3)     Aggregation

Aggregation is a stronger form of association (this also means it is still an association---add by Fitzwilliam).  It is used to show a logical containment relationship, that is, a whole formed of parts.  Although the parts may exist independently of the whole (different from composition here---add by Fitzwilliam), their existence is primarily to form the whole.

Aggregation is modeled as an association with a hollow diamond at the class forming the whole.  In terms of implementation in Java, an aggregation maps to instance variables on a class.

Unlike association instances, instances of an aggregation cannot have cyclic links.  That is, an object may not directly or indirectly be part of itself.  For example, if an instance of A aggregates an instance of B, then that instance of B cannot itself aggregate that same instance of A.

In general, unless you believe that using aggregation adds value or clarifies something, you should use association or composition.

 

4)     Composition

Composition is another form of association and is similar to aggregation to some degree.  However, it is less ambiguous.

Composition is appropriate for modeling situations that call for physical containment.  It implies a much stronger whole-part coupling between the participants such that parts cannot exist without the whole.  That is, parts are share the life cycle of the whole.  They are created when the whole comes to life and destroyed when the whole ceases to exist.

Composition is shown in the same way as aggregation except that the diamond is filled in.

 

5)     Reflexive Relationships

A class may have an association with itself.  For example, if a person employs another person, the Person class may have an association with itself with the role names of employer and employee.  Such a relationship is called a reflexive relationship.

This notation can be considered a modeling shorthand.  Only one class icon rather than two is used to illustrate the relation.  It would be perfectly acceptable to show two separate Person icons with the relation drawn between them.  However, to do so consumes space on a diagram.

 

 

 

 

 

 

 补注1:

关联关系(association)描述了给定类的对象个体之间的语义连接。关联提供了不同交互对象间的连接。剩余的关系则相关于分类本身的描述,而非它们的实例。

摘自:The Unified Modeling Language Reference Manual

By James Rumbaugh, Ivar Jacobson, Grady Booch

Interpreted by Adams Wang

所以说,如果没有对象,那么就没有关联关系。在程序中,如果只是调用了类的静态方法,而没有产生这个类的实例,那么这两个类的对象之间就没有关联关系。但是这两个类之间是有依赖关系的

 

### 关于系统设计中的标准E-R图 #### 基本概念 实体-联系模型(Entity-Relationship Model, E-R模型)是一种用于描述现实世界中数据及其相互关系的概念工具。它通过图形化的方式表示实体、属性以及它们之间的关联[^1]。 #### E-R图的主要组成部分 E-R图由三个基本要素构成:实体、属性和联系。 - **实体**是指现实中可以区分的对象或事物,通常用矩形框表示。 - **属性**是实体所具有的特性,椭圆形用来描绘这些特征。 - **联系**则表达不同实体间的关系,菱形代表这种连接,并标注具体的联系类型如一对一(1:1),一对多(1:N)或多对多(N:M)[^1]。 #### 绘制流程概述 绘制E-R图的过程分为两个阶段——局部E-R图的设计与全局整合。首先针对各个子系统的业务逻辑分别构建其对应的局部视图;之后再把这些独立的部分综合起来形成完整的全局视角[^2]。 #### 示例说明 以下是基于上述理论的一个简单例子展示如何创建一个图书馆管理系统的基础框架: ```plaintext +------------------+ +--------------------+ | Book |---<-->| Author | +------------------+ +--------------------+ | book_id (PK) | | author_id (PK) | | title | | name | | publication_year| | birth_date | +------------------+ +--------------------+ Legend: (PK): Primary Key ``` 在这个案例里,“Book” 和 “Author” 是两个主要的实体集。“book_id” 及 “author_id” 分别作为各自表内的主键(primary key)存在。两者之间存在着一种双向的一对多关系,意味着一本书可能有多个作者而每位作家也可能撰写过多本书籍。 #### Python实现简单的ER图生成脚本 虽然手动画图是最常见的做法,但也可以利用编程自动生成基础结构。下面提供了一段使用 `graphviz` 库来生成 ER 图的小型 python 脚本: ```python from graphviz import Digraph dot = Digraph(comment='The Round Table') # Nodes representing entities dot.node('A', 'Book') dot.node('B', 'Author') # Attributes as sub-nodes under respective entity nodes with dot.subgraph() as s: s.attr(rank='same') s.node('C', 'book_id\n(PK)') s.node('D', 'title') s.node('E', 'publication_year') with dot.subgraph() as s: s.attr(rank='same') s.node('F', 'author_id\n(PK)') s.node('G', 'name') s.node('H', 'birth_date') # Relationships between attributes and their parent entities dot.edges(['AC','AD','AE']) dot.edges(['BF','BG','BH']) # Relationship line indicating many-to-many relationship dot.edge('A', 'B', label="writes") print(dot.source) dot.render('test-output/round-table.gv', view=True) ``` 此代码片段会输出一张反映书籍同作者之间写作关系的图表[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值