89

What is the difference between these 2 relationships?

enter image description here

Edit: Also if you could provide a simple code example illustrating the difference, that would be really helpful!

NPS
  • 6,003
  • 11
  • 53
  • 90

6 Answers6

58

I'm trying to give simple examples of the two types of lines.

In the first diagram, the solid line shows an association:

PlantUML diagram of a directed 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:

PlantUML diagram of 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 calls
  • ClassA catches exceptions of type ClassB
  • Whenever ClassB is modified, ClassA must also be modified (e.g., some logic is shared)
Community
  • 1
  • 1
Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
  • 1
    From your answer, I think the two idea can be differentiated by the scope of `ClassB` object: for association it has class-level scope, while the other method-level only. – NeoZoom.lua Nov 24 '20 at 15:58
26

Your question gave me a good chance to learn myself, here is what I found -

enter image description here

Association: Ownership of another type (e.g. 'A' owns a 'B')

//@assoc  The Player(A) has some Dice(B)
class Player {
    Dice myDice;
}

Dependency: Use of another type (e.g. 'C' uses a 'D')

//@dep    The Player(C) uses some Dice(D) when playing a game
class Player {
    rollYahtzee(Dice someDice);
}

Here's a crisp ref I found - Association vs. Dependency

J-Dizzle
  • 4,861
  • 4
  • 40
  • 50
22

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.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Mathieu Brouwers
  • 564
  • 7
  • 19
  • Unfortunately, I still can't make out the difference. Do you think you could provide simple code examples that would illustrate the difference between the two relationships? – NPS Nov 17 '14 at 22:35
  • 6
    I don't think the above code example is correct. There is no direct UML to code translation, but if anything, an association usually translates into an attribute of the opposite type. So a Car having and attribute of type SteeringWheel, or an attribute of type List could be the translation of an association. A dependency is generally a weaker relationship. Something like a Car having an operation DriveTo(Location destination). Location is known and used by Car, so therefore there is a dependency to Location. – Geert Bellekens Nov 18 '14 at 07:45
  • I believe it is slightly more specific. You describe two associations here, "Car has a Door", and "Car's door can be opened". They are passive, not actually using Door's objects. For a dependency, you actively use Door's objects: if Door has objects "Width" and "Max_aperture_angle", and Car has a method "Max_car_width" using both objects, then you have a dependency. – Gabriel123 Oct 05 '20 at 11:30
15

Okay, since you didn't accept the first answer; let me try.

Arrow 1: A normal association

enter image description here

UML has different types of lines and arrows. Above is the simple association arrow, that means that one class can have a link to the other class. Below I will explain each type WITH code examples.

  • In the first example, you can see that there isn't really specified who knows who (who is the owner of the relationship). An animal can know the human and the human can know the animal. It's not specified and thus not really helpful for the programmer.
  • In the second example, the artist can have a guitar. Because there is an arrow and there isn't one on the other side, we know that the guitar doesn't know the artist. A guitar is an object that can totally exist on its own and doesn't need anybody.
  • In the third example, you see a marriage. Really simple; the husband knows the wife and the wife knows her husband. In our situation, the husband has only one wife and vice versa.

How do we accomplish this usually in code?

class Husband{
    Wife bestWomanInTheWorld;

    public Husband(Wife theWife){
        this.bestWomanInTheWorld = theWife;
    }
}

Because the husband always needs a wife, we put the required relationship in the constructor. Because an artist can have a guitar, we would leave the constructor empty like this:

class Artist{
    List<Guitar> guitars;

    public Artist(){
    }

    public AddGuitarToCollection(Guitar newGuitar){
        Guitars.Add(newGuitar);
    }
}

So, that's how we accomplish this in code (most of the time!). You won't usually need different types of lines and arrows if you are new to programming. Keep it simple.

Arrow 2: Dependency

Okay, so we know about normal associations which we will use most of the time. But when will we use the 'dependency' arrow? Well, lets define a dependency (wikipedia):

Dependency is a weaker form of bond which indicates that one class depends on 
another because it uses it at some point in time. One class depends on 
another if the independent class is a parameter variable or local variable of 
a method of the dependent class. This is different from an association, where 
an attribute of the dependent class is an instance of the independent class. 
Sometimes the relationship between two classes is very weak. They are not 
implemented with member variables at all. Rather they might be implemented as 
member function arguments.

If there is a connection, relation, association etc. that requires to be present, to the classA to work; it's a dependency. Example: Husband needs the Wife to exist. A car needs a wheel to be a car (and drive). A car factory needs a car class to make an object from it. Your RSSNewsItem class needs an XMLReader class to do anything.

When to use which?

Well, this is the only valid question in my eyes; since google shows alot of valid answers to your question. Try to never use a dependency in a class diagram because it usually means that you aren't specific enough. Always aim for associations, realisations etc. Only use realisations (in my opinion) if there is a required need to use an other class without maintaining a relationship. Example; Utility classes (like the XMLReader).

If you have any questions after reading this full explanation, feel free to ask. :-)

shanwije
  • 600
  • 7
  • 17
guidsdo
  • 432
  • 2
  • 9
  • 3
    be careful not to confuse navigability (the arrowhead) with ownership. Ownership is modeled as a bullet at the end of the association (not shown in your example). – Geert Bellekens Nov 29 '14 at 05:50
  • 1
    Yes, that is true but the thing is; barely anyone uses the ownership bullet, it is out of the scope of the answer and not necessary in most cases (only if it is unclear). That is the same reason why I discourage anyone to use the dependency association. – guidsdo Dec 02 '14 at 09:10
  • So in the Wife class, it should be implemented the same way Husband's, because she also needs him, right? – testing_22 Apr 11 '22 at 21:13
7

Dotted line indicates dependency to (in the direction of the arrow). Assuming you have assembled your source code neatly into separate files and headers for each class - the give away is simply that the code includes the line #include ClassB.h.

HOWEVER The fact of the matter is that all class relationships (generalisation, realisation, composition, aggregation, association etc) all inherit the dependency relationship. For this reason I never use dotted arrows when documenting code. Where possible I would aim to document the relationship in more specific terms eg. diamonds, triangles etc. If I don't know the exact relationship my starting point is a solid line with arrows (an association, with (implicit) dependence).

Notwithstanding this the dotted arrow notation can be useful in other aspects of UML modelling eg. showing dependencies to requirements in Use Case analysis for example. NOTE The Thought Police would have us reduce coupling & dependencies between classes by using interfaces (pure virtual classes) as far as practical.

Whilst pure virtual classes offer the prospect of multiple inheritance and tightest coupling of all between classes as is possible. Interface classes have the advantage that they are made entirely out of dark matter and so totally invisible to the police. With this in mind it is possible to write c++ code with apparently zero coupling between classes -which they love because they never really did understand all those funny looking symbols anyway.

ANC
  • 71
  • 1
  • 1
-8

dotted mean implements (an interface) solid means extends (a base class)

aaron p.
  • 17
  • 2