D. Rattansingh
D. Rattansingh

Reputation: 1669

UML association with respect to code

I understand code fully however I'm having a bit of trouble understanding UML as it relates to code.

Question 1 - If I have a class

public class one
{....}

public class two
{
    one ob;
    public two(){ob=new one();}
}

I know this is an association but do I show this in a class diagram with an arrow or not?

enter image description here

Question 2 - if I have the following class

public class three
{
   public three(){.........}

   public void method() {  one obt=new one();  }
}

Is this an association between class three and class one? I'm not sure since its being applied from the method.

Upvotes: 2

Views: 312

Answers (2)

sfinnie
sfinnie

Reputation: 9952

I know this is an association but do I show this in a class diagram with an arrow or not?

If class one has no reference to class two then you should have an arrow head. But it points from two to one (opposite way to how you have it shown). Reason: the arrow indicates navigability. Class two can navigate to class one because it holds an instance. The opposite is not true. Technically it should be an open arrowhead, not the closed form you've used.

Is this an association between class three and class one? I'm not sure since its being applied from the method.

If the only reference from three to one is as a method parameter then you'd most likely show as a dependency not an association. Associations declare a systematic relationship among the entities - like a Purchase Order consisting of Order Lines. In code that most often translates to a member variable (or collection thereof). Dependencies are a weaker form of relationship than Associations and don't imply a systematic link.

hth.

Upvotes: 1

vainolo
vainolo

Reputation: 6987

First of all the arrow is in the other direction, from two to one since two points at one. If you don't show it with an arrow you are saying that you don't know the direction of the relations - it can be both one calling two and two calling one. Since you know that two owns one, you should add the arrow direction.

Regarding your second question, yes this is a relation from three to one since three requires one. This relation can be a dependency or association, depending on what three does with one.

Upvotes: 1

Related Questions