Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "uses" and "depends upon"?

In this beginners guide to Dependency Injection I noticed that the UML diagram distinguishes between "uses" and "depends upon".

Since both require some form of a reference in the class that "uses" or "depends upon", I wonder: What is really the difference between the two?

like image 929
Regex Rookie Avatar asked Jun 30 '11 21:06

Regex Rookie


2 Answers

Check out the blockquotes about relationship types taken from IBM Rational Software Architect documentation.

"depends upon" means the following:

A dependency relationship indicates that changes to one model element (the supplier or independent model element) can cause changes in another model element (the client or dependent model element). The supplier model element is independent because a change in the client does not affect it. The client model element depends on the supplier because a change to the supplier affects the client.

"uses" means the following:

A usage relationship is a dependency relationship in which one model element requires the presence of another model element (or set of model elements) for its full implementation or operation. The model element that requires the presence of another model element is the client, and the model element whose presence is required is the supplier. Although a usage relationship indicates an ongoing requirement, it also indicates that the connection between the two model elements is not always meaningful or present.

As I read it "usage" is a less strict "dependency".

like image 88
Andrey Adamovich Avatar answered Oct 05 '22 11:10

Andrey Adamovich


"Uses" is where one Class refers to another Class for some of it's operations.

"Depends on" is where a Class A uses another Class B within it's implementation (e.g. as a parameter to a method). In this case changing Class B may necessitate a change to class A.

Note I've said Class, but it applies equally to Interfaces.

Wikipedia has a good article on this: http://en.wikipedia.org/wiki/Dependency_%28UML%29

So for example you could have a Uses relationship between a Class Driver and an Interface IVehicle which exposes a method called Drive(). Changes to the implementation of Drive do not require any changes to Driver, so you say Driver uses IVehicle.

However Class Driver has a Dependency on Class Hand, since Driver has two properties: Hand LeftHand and Hand RightHand. If the implementation of these changed, one would need to consider if Driver needed updating accordingly.

like image 20
BonyT Avatar answered Oct 05 '22 13:10

BonyT