UML Design class diagram: a class with a different class as an attribute?

I'm having a pretty tough time trying to figure out how to model a certain scenario as a UML design class diagram.

Suppose I have the following situation:

I have a class named CPoint that has two attributes, x and y (coordinates in the R2 plane). On the other hand, I have a class named CLine that needs to have two CPoint as attributes.

This is pretty straight forward code (I'm using C ++ in my example):

class CPoint{
    float x;
    float y;
    //Constructor, gets and sets here
}

      

And for CLine:

class CLine{
    CPoint p1;
    CPoint p2;
    //Constructor, gets and sets here
}

      

Now my question is, how do I model such a thing in the UML?

I thought of something similar:

Attempt 1

But then I was told that this violates the principles of object-oriented modeling, so I did this:

Attempt 2

But that doesn't convince me at all. Also, I read about design patterns and came to this UML design by reading about singlons:

Singleton design pattern

Which makes me think my initial approach was right. Also, I can see that my first approach is just fine if I talk about it as a C ++ program. However, in java, I still need to create an object by executing new CPoint(0, 0)

in the CLine constructor. I am really confused about this.

So how do I simulate this situation? Perhaps I am being too specific when trying to simulate a situation?

Thanks in advance! This keeps me awake at night.

+3


source to share


3 answers


In the UML, an association or attribute (property) is more or less the same, so both are correct.

In most UML tools however they are different things.



There is no rule here, but there are best practices. My UML Best Practice: Attribute or Association says:

Use associations for classes and attributes for data types

+4


source


If your CLine has exactly two ends, represented by a dot, then you can define it in the UML as a CLine class with attributes (just like your CLine in the first example is OK, but without the association "is"), or you can design it as a CLine Class with two CPoint associations. The multiplicity in the CPoint will be 1 with p1 for the first and p2 for the second on the CPoint side.



+2


source


There is no better solution. It depends on the context and what you want to model. I agree with Vladimir that you will have two connections with the roles p1 and p2. Members x and y must be private. I think (-x, -y)

not public either (+x, +y)

. Alternatively, you could model the relationship as an aggregate or compound (open or closed diamond symbol), but if one point can be the end point of two strings, this is not appropriate. Again, this depends on what you want to model. If you plot a new point in the line constructor, as pointed out in the question, then you probably want to use the composition relationship, since those points don't exist without a line.

(Btw, in the code the coordinates are float and in the chart ints).

+2


source







All Articles