Visibility between objects in a UML Class Diagram

In this blogpost, I will start with briefly explaining four common ways that visibility can be achieved from object A to object B and finish with two examples how to visualise visibility from a UML Communication Diagram to a UML Class Diagram.

Attribute visibility

B is an attribute of A

If we want to give an object A permanent visibility of an object B, we can include an attribute of type B in class A. The resulting link is visible in the class diagram as a unilateral association from A to B. To illustrate this in Java:

public class A {
    private B b;

Parameter visibility

B is a parameter of a method of A

Often we have the situation that A must have a permanent visibility of B, but that B does not exist yet at the creation of A. In such a case, if B is also created, we can display this as a parameter in a method, so that the permanent visibility can still be realised.

Local visibility

B is a (non-parameter) local object in a method of A

Local visibility from A to B can be implemented by creating a new instance of B within a method of A or by calling a method that has an instance of B as a return value. This can be done like the following, for example in Java:

B tempObject = anObject.getFoo();

or shorter:


Global visibility

B is in some way globally visible

Global visibility from A to B exists when B is global to A. This visibility exists as long A and B exist and the value of the attribute that refers to B does not change.

Note that this is the least common form of visibility in an object-oriented system.

First example

Communication Diagram

Communication Diagram

Class Diagram

:A calls method get on :Collection <B>. Similarly, :B calls method put on :Collection <A>. If we assume that classes A and B act as managers of collections and the visibility is permanent, then we get the class diagram as below. Note that we usually do not include methods for managing a collection (get, put, select, contains, size) in the design class diagram.

Class Diagram

Second example

Communication Diagram

Communication Diagram

Class Diagram

In response to message m1, :A first calls the class method random on class Math. To invoke a class method no association is needed; we are therefore only dealing with a dependency here. If class Math would change, class A might also have to change. However, permanent visibility is not required. Then a new instantion b of class B is created. The instantion b is then temporarily visible for :A. Finally, b is added by :A to c. This requires permanent visibility from :A to :C.

Class Diagram