UML Class Diagram

Visibility 

To specify the visibility of a class member (i.e. any attribute or method), these notations must be placed before the member's name

+ Public

- Private

# Protected

~ Package

Derived property is a property which value (or values) is produced or computed from other information, for example, by using values of other properties.

Derived property is shown with its name preceded by a forward slash '/'

Scope

The UML specifies two types of scope for members: instance and classfier, and the latter is represented by underlined names.

Classfier members are commonly recognized as "static" in many programming languages. The scope is the class itself

Instance members are scoped to a specific instance.

Relationships

Instance-level relationships

Association

一个关联(Association)代表一个家族的联系。关联可以命名,可以饰以角色名称,有权指针,多重性,可视性,以及其他属性(如相互关联和有方向的(带燕尾箭头的实线表示)关联)。在语义上是两个类之间、或类与接口之间一种强依赖关系,是一种长期的稳定的关系," ... has a ..." 。关联关系使一个类知道另外一个类的属性和方法;通常含有“知道”、“了解”的含义。某个对象会长期的持有另一个对象的引用,关联的两个对象彼此间没有任何强制性的约束,只要二者同意,可以随时解除关系或是进行关联,它们在生命期问题上没有任何约定。被关联的对象还可以再被别的对象关联,所以关联是可以共享的。 在代码层面上,被关联类以类属性的形式出现在关联类中,也可能是关联类引用了一个类型为被关联类的全局变量。当前定义有五种不同类型的关联。双向(Bi-directional)和单向(uni-directional)的关联是最常见的。

Class diagram example of association between two classes

Aggregation

Aggregation is a variant of the "has a" association relationship; aggregation is more specific than association.

Aggregation can occur when a class is a collection or container of other classes, but the contained classes do not have strong lifecycle dependency on the container. The contents of the container still exist when the container is destroyed .

Example: Library and Students. Here the student can exist without library. the relation between student and library is aggregation.

//Aggregation

Class Pond{

private:

    std::vector ducks; // 当vector 的析构函数被调用时,duck实例并不会被销毁

}

Composition

组成(Composition)关系,是一类“强”的整体与部分的包含关系(" ... is a part of ...")。成分类必须依靠合成类而存在。整体与部分是不可分的,整体的生命周期结束也就意味着部分的生命周期结束。合成类别完全拥有成分类别,负责创建、销毁成分类别。例如汽车与化油器,又例如公司与公司部门就是一种组成关系。图形以实心的菱形箭尾与实线表示。

//Composition

Class Car{

private:

// Car is the owner of carburetor.

// Carburetor is created when Car is created,

// it is destroyed when Car is destroyed.

    Carburetor carb;

}

Differences between  Composition and Aggregation

Composition relationship

    1. When attempting to represent real-world whole-part relationships, e.g. an engine is a part of a car.

    2. When the container is destroyed, the contents are also destroyed, e.g. a university and its departments.

Aggregation relationships

    1. when representing a software or database relationships.

    2. When the container is destroyed, the contents are usually not destroyed, e.g. a professor has students, when the professor dies the students don't die along with him or her.

Thus the aggregation relationship is often "catalog" containment to distinguish it from composition's "physical" containment.

Class-level relationships

Generalization/Inheritance (继承)

Realization/Implementation (实现)

General relationship

Dependency

Dependency is a weaker form of bond that indicates that one class depends on another because it uses it at some point in time. One class depends on another if the independent class is a parameter variable or local variable of a method of the dependent class. This is different from an association, where an attribute of the dependent class is an instance of the independent class. Sometimes the relationship between two classes is very weak. They are not implemented with member variables at all. Rather they might be implemented as member function arguments.

英文wiki

中文wiki

你可能感兴趣的:(UML Class Diagram)