UML class diagrams

This article contains short and simple explanations of UML class diagram design concepts. They are helpful for ones who want to improve UML understanding. These explanations consist of a few lines of PHP code and a diagram and summary of how they are related to each other.

Quick links: generalization, composition, shared aggregation, interfaces, association.

Class is represented on the UML diagram by rectangle with class name in it. Class rectangle has three sections: class information (such as name, superclass, stereotype, etc) in the first, attributes in the second and operations in the third. In other words, class variable definitions are in the second section and method declarations are in the third one. Abstract classes have their names in italic. "+", "#" or "-" sign before attribute/operation name means its scope: "public", "protected" or "private" respectively.

Lines on diagram that connects classes represent relationships between them: inheritance (generalization), composition and association.

Class diagrams allow you to put into view the whole structure of your application, not only the class you are editing. Just looking on the diagram and thinking may bring up an idea on how to improve the structure of your application or prove that it is correct and matches requirements.

Example of class diagram: Zend Framework Zend_Json classes with class variables and methods.

Zend Framework Zend_Json UML class diagram

Generalization


A generalization defines a taxonomic relationship between two classifiers: a more general one and a more specific one. And that means inheritance. For example, when one class extends another this is called "Generalization" in UML. It is shown as a line with a hollow triangle that connects two classifiers.

 UML Generalization example

Composition


Composite aggregation defines a relation between composite and composed objects. The composite object consists (or is made up) of smaller “composed” objects. It creates, modifies and destroys them. For example: window contains title, table row contains fields.

Composition is shown as a line with a black diamond near the composed object.

 UML Composition example

Shared aggregation


The shared aggregation indicates that some classifier (process or class) cannot be performed/completed without another classifier. For example, book reading is not possible without a book but the book can be used in two different reading processes. Another example: database driven web form consists of fields, actions and pooled data connection and the connection can be used by another form/control.

So, the book reading process cannot be performed without a book and reader (it aggregates book and reader) and the book instance is shared by the reading process instances. Similarly, the data driven form cannot be completed without a data connection (it aggregates data connection) and the connection is shared by different forms.

On class diagrams shared aggregation is represented by a line with a hollow diamond.

Example #1. Book reading processes are represented by the ReadingProcess class and it aggregates the Reader and Book classes.

UML Shared Aggregation example

Example #2. A data driven form aggregates connection and fields. Fields are owned by the form and connection is shared by the forms.

UML Shareg Aggregation Example

A shared aggregation between two classes is usually represented by a class property that is initialized in constructor with instance of another class and never gets changed during the object lifetime.

Interfaces


An interface is a specification of behaviour that a class should implement in order to be used by some specific part of a system. In other words, interface specifies how objects communicate with each other.

There are two notations for interfaces:

  • Circle notation, when an interface is shown as a circle without methods and/or properties. Implementers are attached to the interface by a solid line.
  • Rectangular notation, when an interface is shown as a class with "<<interface>>" text over it. Implementers are attached to the interface by a realization link, a dashed line with triangular arrowhead.

Example of circle notation: class ChildNodes implements a set of interfaces from PHP SPL library.

UML Interface, circle notation example

Rectangular notation: class Button implements interface IButton and provide realization of the click method.

 UML Interfaces, rectangular notation example

Association


An association usually means that you have a sentence that describes a relation between two (or more) classes and this sentence is not like one of the following:

  • “the A class extends the B class” or “the B class implements the A interface” (use Generalization or Realization for this).
  • “the A class is an integral part of the B class” or “the B class consists of the A class (and some other classes)” (use Composition for this).

Examples of associations:

  • Node class contains Attribute classes.
  • Driver class drives Car class.
  • Transaction class modifies Table classes.
  • Manager class starts Machine class.
  • Page class contains Widget classes.

Usually the class method that operates with the object or the read/write class property represents an associative relationship in source code.

An association is represented by a solid line with optional navigation, multiplicity, roles, and name.

Example of association:  the Node class (parent) can contain unlimited number of the Node and Attribute objects (children).

UML Association example

I’m Alex Netkachov and I welcome you on my site, which is my technical playground and web log.

User login