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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值