What is the difference between these 2 relationships?
Edit: Also if you could provide a simple code example illustrating the difference, that would be really helpful!
So basically the solid line is an association and the dashed/dotted line is a dependency. Associations can also be unidirectional, where one class knows about the other class and the relationship but the other class does not.
"Solid line" means a line with no breaks. Like if you were drawing it with a pencil, you'd put the pencil down and draw without picking it up. "Dotted line" means, as the name implies, a line made up of dots.
Solid arrow heads represent synchronous calls, open arrow heads represent asynchronous messages, and dashed lines represent reply messages.
Directed association The association from one class to another class flows in a single direction only. It is denoted using a solid line with an arrowhead. Example: You can say that there is a directed association relationship between a server and a client.
I'm trying to give simple examples of the two types of lines.
In the first diagram, the solid line shows an association:
If the classes were declared in Java, this would be like ClassA
storing a reference to ClassB
as an attribute (it could be passed in to the constructor, created, etc.). So, you might see something like:
public class ClassA { ClassB theClassB = ... ... }
In the second diagram, it shows a dependency:
A dependency is much weaker than an association. To quote from UML Distilled:
With classes, dependencies exist for various reasons: One class sends a message to another; one class has another as part of its data; one class mentions another as a parameter to an operation. [...] You use dependencies whenever you want to show how changes in one element might alter other elements.
Again, using Java, a couple of examples exist: an argument of type ClassB
is passed to a method, or a method declares a local variable of type ClassB
:
public class ClassA { ... public void someMethod(ClassB arg1) {...} ... public void someOtherMethod() { ClassB localReferenceToClassB = ... } ... }
Other ways ClassA
could depend on ClassB
without having an association (not an exhaustive list):
ClassB
has a static method that ClassA
callsClassA
catches exceptions of type ClassB
ClassB
is modified, ClassA
must also be modified (e.g., some logic is shared)This webpage says enough I think The following text comes from it, but should be enough to understand the difference.
So basically the solid line is an association and the dashed/dotted line is a dependency.
Associations can also be unidirectional, where one class knows about the other class and the relationship but the other class does not. Such associations require an open arrowhead to point to the class that is known and only the known class can have a role name and multiplicity. In the example, the Customer class knows about any number of products purchased but the Product class knows nothing about any customer. The multiplicity "0..*" means zero or more.
A dependency is a weak relationship between two classes and is represented by a dotted line. In the example, there is a dependency between Point and LineSegment, because LineSegment's draw() operation uses the Point class. It indicates that LineSegment has to know about Point, even if it has no attributes of that type. This example also illustrates how class diagrams are used to focus in on what is important in the context, as you wouldn't normally want to show such detailed dependencies for all your class operations.
Since my reputation is only 8 I can't place the images itself, but they can still be found on the webpage I mentioned at the start.
[EDIT]
I don't have code examples right here, but how I personally would explain it is as simple as a car and a door.
When a car has a door (or more) it's just a car
Car --- has a --> Door
But when you have a door which can be opened the door class will have a function like
public void openDoor(){ this.open(); }
To use the function above the car will have to create an instance of the door
Class Car(){ Door door1 = new Door(); door1.open(); }
In this way you have created a dependency.
So the solid line is just pointing an object(1) to another object(2), but when you start using the object(1) it becomes a dependency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With