This article is Part-1 of a four part series. In this post the Dependency Relationship is discussed as to how they are represented in UML with code examples. In the next three parts other class relationships such as Association, Aggregation, Composition, Generalization and Realization would be discussed.
Dependency
A dependency is a relationship that signifies that a single or a set of model elements requires other model elements for their specification or implementation. This means that the complete semantics of the depending elements is either semantically or structurally dependent on the definition of the supplier element(s). - UML 2.0 Superstructure Specification Page 58.
A Dependency relationship indicates that a change to one class (the supplier) may cause a change to another class (the client). Dependency is the weakest form of relationships. In UML a dependency relationship is represented as a dashed line between two model elements with the arrow head at the supplier’s end.
Dependency relationships are inexpensive from resource consumption viewpoint and are transient i.e. their life is for a limited duration. There are three ways how a client can make a reference to a supplier class.
- As a Parameter: When the supplier class is used as a parameter in an operation or as a return type of an operation in the client or the consuming class. For example EmailAddressValidator uses the Customer object to do the email validation.
class EmailValidator {
public boolean validate(Customer customer) throws InvalidEmailException {
String emailAddress = customer.getEmailAddress();
}
- As a Local Variable: When the supplier instance is created locally in the body of an operation of the consuming class. For example EmailValidator creates an instance of InvalidEmailException when the email is in invalid.
class EmailValidator {
public boolean validate(Customer customer) throws InvalidEmailException {
String emailAddress = customer.getEmailAddress();
if(/*emailAddress starts with a number*/){
throw new InvalidEmailException (”invalid email: cannot start with a number”);
}
The UML representation of both the examples in above is shown below.
- Global Reference: When a client class uses a globally scoped supplier, for example if Java is your implementation language then a global supplier would be a singleton.
So that covers an overview about Dependency Relationship and how to model one in UML.