Home > Articles > Software Development & Management > UML

This chapter is from the book 16.11 Dependency

16.11 Dependency

Dependency lines may be used on any diagram, but are especially common on class and package diagrams. The UML includes a general dependency relationship that indicates that a client element (of any kind, including classes, packages, use cases, and so on) has knowledge of another supplier element and that a change in the supplier could affect the client. That’s a broad relationship!

Dependency is illustrated with a dashed arrow line from the client to supplier.

Dependency can be viewed as another version of coupling, a traditional term in software development when an element is coupled to or depends on another.

There are many kinds of dependency; here are some common types in terms of objects and class diagrams:

  • having an attribute of the supplier type
  • sending a message to a supplier; the visibility to the supplier could be:
    • an attribute, a parameter variable, a local variable, a global variable, or class visibility (invoking static or class methods)
  • receiving a parameter of the supplier type
  • the supplier is a superclass or interface

All of these could be shown with a dependency line in the UML, but some of these types already have special lines that suggest the dependency. For example, there’s a special UML line to show the superclass, one to show implementation of an interface, and one for attributes (the attribute-as-association line).

So, for those cases, it is not useful to use the dependency line. For example, in Figure 16.6 a Sale has some kind of dependency on SalesLineItems by virtue of the association line. Since there’s already an association line between these two elements, adding a second dashed arrow dependency line is redundant.

Therefore, when to show a dependency?

Guideline: In class diagrams use the dependency line to depict global, parameter variable, local variable, and static-method (when a call is made to a static method of another class) dependency between objects.

For example, the following Java code shows an updatePriceFor method in the Sale class:

public class Sale
{
public void updatePriceFor( ProductDescription description )
{
	Money basePrice = description.getPrice();
	//…
}
// …
}

The updatePriceFor method receives a ProductDescription parameter object and then sends it a getPrice message. Therefore, the Sale object has parameter visibility to the ProductDescription, and message-sending coupling, and thus a dependency on the ProductDescription. If the latter class changed, the Sale class could be affected. This dependency can be shown in a class diagram (Figure 16.9).

Figure 16.9 Showing dependency.

Another example: The following Java code shows a doX method in the Foo class:

public class Foo
{
public void doX()
{
	System.runFinalization();
	//…
}
// …
}

The doX method invokes a static method on the System class. Therefore, the Foo object has a static-method dependency on the System class. This dependency can be shown in a class diagram (Figure 16.10).

Figure 16.10 Showing dependency.

Dependency Labels

To show the type of dependency, or to help a tool with code generation, the dependency line can be labeled with keywords or stereotypes.[6] See Figure 16.11.

Figure 16.11 Optional dependency labels in the UML.

  • + Share This
  • 🔖 Save To Your Account