property owned by an association vs. property owned by a class? - uml

What is the differences between property owned by an association and a property owned by a class in UML?
is there a simple example helps me understand the differences?

The difference is more conceptual than anything else. If you have a property attached to an association, then you will have an association class, with the desired property.
Here is an example of a mailman sending letters to clients (the attribute weight is bound to the association):

The difference is very concrete, but traditionally ignored.
A is associated to B. A,B are classes.
If B end of the association is "association owned" and navigable, it means, that you can easily reach instance of B from A, either by reference, or by some method(s). It should be shown by arrow.
If B end is "classifier owned", you know a bit more. it means, that A has an attribute, that is a direct reference. (No functions or reference counting here). It should be shown by arrow and "dot"- a small black filled circle.
If you are going to B by a.smth.smthelse.b, it is arrow, but surely no dot.
If you are going to B by a.b, it is arrow and dot.
If you haven't decided yet, it is arrow again.
Traditionally modellers show arrows only. But it is not a good tradition, and is against UML standard. Diagrams are more useful if we decide as much as possible on them, not in code.
If both ends are navigable, both arrows disappear and you may see only dot(s).
The association lines with cross with dot on one side, or arrow on one side and only dot on the other are senseless.

Related

Can inherited arity be overriden?

I have this class/object diagram:
I don’t understand why that object diagram is invalid according to the given class diagram.
In the object diagram, one C object has two links with two T objects, alpha relationship with a T object and beta relationship with another T. So I don’t think it violates the multiplicity constraints.
Could you please explain me why the object diagram is invalid?
Yours is the most interesting question I've seen here in a long time. It is pretty tricky!
The simple reason your instances are incorrect is that every instance of type T must be associated with one C. The top instance of type T in your diagram violates the constraint in association beta. (The multiplicity on the left end of the association.)
There are two faults in the object diagram.
There is only a formal fault in the object diagram, the lines in the objects diagrams between the instances are links, i.e., instances of the associations shown in the class diagram. As the links are instances, the same rules for instance naming apply as to class instances. So change alpha to :alpha and underline it, it is correct. Same for beta.
Further the links are not correct, as there is an beta link from the uppermost T instance missing. Each object of A, and as C is a specialization of A, also C (and B) objects need an alpha link to an S instance. As S is a generalized T, an alpha link between A (or one of its specializations) and S (or one of its specializations) is needed. Further each S (or T) might have arbitrary alpha links to A objects.
Each C object needs to have zero or one beta links to T instances. In the other direction, each T instance needs exactly one C instance via a beta link. This is missing for the uppermost T instance.
Leaving my prior answer below, but thinking twice, the answer is that your class diagram is incomplete.
The two alpha and beta associations have no association-end names. The fact that they have different multiplicities leads to the conclusion that they must be different associations. With names it would look like this:
Expanding the inheritance will make this:
Based on this assumption, my original answer stands like this:
The reason is that a :C has two associations alpha and beta each to another :T object. Not a single alpha to one and a single beta to another. So you need to add a beta to the alpha and vice versa.
Edit And yes, JimL. is correct. Having two alpha-links violates the constraint from the class diagram. So actually you can only have one T linked to C. Which again makes the class model very strange.
The C class has a beta-association to T. C inherits from A and T inherits from S. Since there is a alpha-association from A t0 S this is also inherited. So you have:

UML Circular reference with both aggregation and composition

A few days ago a friend pointed out to me that I had a wrong idea of composition in UML. She was completely right, so I decided to find out what more I could have been wrong about. Right now, there is one more thing that I have doubts about: I have a circular dependency in my codebase that I would like to present in UML form. But how.
In my case the following is true:
Both A and B have a list of C
C has a reference to both A and B to get information from.
C cannot exist if either A or B stops to exist
Both A and B remain to exist after C is deleted from A and/or B
To model this, I've come up with the following UML (I've ommited multiplicities for now, to not crowd the diagram.)
My question is, is this the right way to model such relations?
Problems
Some facts to keep in mind:
Default multiplicity makes your model invalid. A class may only be composed in one other class. When you don't specify multiplicity, you get [1..1]. That default is sad, but true.
The UML spec doesn't define what open-diamond aggregation means.
Your model has many duplicate properties. There is no need for any of the properties in the attribute compartments, as there are already unnamed properties at the ends of every association.
Corrections
Here is a reworking of your model to make it more correct:
Notice the following:
The exclusive-or constraint between the associations means only one of them can exist at a time.
Unfortunately, the multiplicities allow an instance of C to exist without being composed by A or B. (See the reworked model below.)
The property names at the ends of all associations explicitly name what were unnamed in your model. (I also attempted to indicate purpose in the property names.)
The navigability arrows prevent multiple unwanted properties without resorting to duplicative attributes.
Suggested Design
If I correctly understand what your model means, here is how I would probably reverse the implementation into design:
Notice the following:
Class D is abstract (the class name is in italics), meaning it can have no direct instances.
The generalization set says:
An instance cannot be multiply classified by A and B. (I.e., A and B are {disjoint}.)
An instance of D must be an instance of one of the subclasses. (I.e., A and B are {complete}, which is known as a covering axiom.)
The subclasses inherit the ownedC property from class D.
The composing class can now have a multiplicity of [1..1], which no longer allows an instance of C to exist without being composed by an A or a B.
Leave away the open diamonds and make them normal associations. These are no shared aggregations but simple associations. The composite aggregations are ok.
In general there is not much added value in showing aggregations at all. The semantic added value is very low. In the past this was a good hint to help the garbage collection dealing with unneeded objects. But nowadays almost all target languages have built-in efficient garbage collectors. Only in cases where you want an explicit deletion of the aggregated objects you should use the composite aggregation.

What means a unidirectional relationship arrow

I don't understand what the relationship between the two tables in the linked picture mean. And how can I code it?
It's with
virtual public Propriete Proprietes { get; set; }
Geert writes true things, but I think, you need more practical advice.
That one-directional arrow means, that the Enquette class has a field or method, that are of the class Propriete. In 99% it is a simply field of that type. Contemporary standard offers to use a point on the other side of the line for this.
The fact, that the arrow has no name on it, often means that the name of the field is propriete, or Propriete, according to the style accepted by the language and your company. (it is not required in UML standard, but it is widely used). According to the last paragraph of the question, it seems, your company uses this rule, too.
The fact, that there is no arrow on the other end of the line, does not mean, that there is no field or method in Propriete class that has a class of Enquette. (Though it meant that in 90-ties). It merely means we haven't decided yet if there are such fields or not. Or maybe, we consider it to be not important. I.e. it is undefined. The known lack of such connection must be shown by a cross instead of an arrowhead.
So, somewhere in Enquette you have a line:
Propriete propriete;
or
Propriete* Propriete; //if you are in C++
or even maybe
Propriete** Propriete;
or cited by you
virtual public Propriete Proprietes { get; set; } // apparently, C#
It could be either static/class field or instance field - it is not defined in the diagram.
And in the class Propriete there can exist the line:
Enquete enquete; // or some of the mentioned variants
And you are leaving the decision about its existence to the coder.
Notice, that a line without arrowheads means that really there are fields (or methods) for both ends. Simply we don't draw arrowheads at all if the line should show two of them.
So, really, you have a mistake in the question. It is NOT an unidirectional relationship. It is an unidirectional arrow showing a relationship, that MAY BE unidirectional.
The arrow at the end of the Association between the Classes (not tables) indicates that this end is navigable.
This is defined as an operation the Property at the end of the Association. The definition in UML 2.5 specs says:
isNavigable() : Boolean
The query isNavigable() indicates whether it is possible to navigate across the property.
body: not classifier->isEmpty() or association.navigableOwnedEnd->includes(self)
Furthermore it says about the Association Notation:
An open arrowhead on the end of an Association indicates the end is
navigable. A small x on the end of an Association indicates the end is
not navigable.
And about the Navigability on an Association
Navigability means that instances participating in links at runtime
(instances of an Association) can be accessed efficiently from
instances at the other ends of the Association. The precise mechanism
by which such efficient access is achieved is implementation specific.
If an end is not navigable, access from the other ends may or may not
be possible, and if it is, it might not be efficient.
And to conclude something to keep in mind because this convention is still often used:
Navigability notation was often used in the past according to an
informal convention, whereby non-navigable ends were assumed to be
owned by the Association whereas navigable ends were assumed to be
owned by the Classifier at the opposite end. This convention is now
deprecated. Aggregation type, navigability, and end ownership are
separate concepts, each with their own explicit notation. Association
ends owned by classes are always navigable, while those owned by
associations may be navigable or not.
So now you should know what it means. How to code it depends on the programming language, the company standards, the architectural layer, your creativity, etc..

UML Class diagram object composed of 4 objects of another class?

I have one object, call it type A which has four data members of another object type, call it B. How do I show this in a UML class diagram so that its clear there are four B objects type in every A object?
Is the only solution to put "4" next to the arrow head pointing to class B?
It depends on what you want to achive, in sense of how you need to distinguish between those objects in context of their association/link, that is - what kind of role they play:
if there are all equal, no special differences in their role in context of A, them a multiplicity 4..4 will do the job, naming the association end properly (for example my_Bs)
If these object play different role in connection with A, then you can use separate associations with lower multiplicities each one, 2, 3 or even 4 pieces (for example, if B is a Wheel and A is Car, then you can put 2 associations with multiplicities 2..2 each, and call then "front" and "rear", or even 4 associations "front_left", "front_right"...)
Here is how the both cases look like. On the second one I showd different possible options (with max. 5 elements of B), just to give you an idea.
It's probably clear by now, but the fundamental concept here is the role of the association end.
Aleks answer is the best. However you can also represent the multiplicity in one box like this :
You cal also use composite structure diagram. See example below:
From my point of view, myBs defined as an attribute of type B on class A has a different meaning of myBs defined as a association's role between A and B (which is also different as defining it as a composition/aggregation).
If it is an attribute, then it's not a role. In that case, there is only a simple dependency relation between A and B, which must appear in the diagram.
I think that problem comes from unconsciously think from a "Relationnal Data (BMS)" and/or a "Object Oriented Programming" point of view, but UML is not intended for that.
:)

UML Aggregation with and without arrow head

I always thought that the UML aggregate is defined as a black (filled) diamond at the beginning of a path and no arrow head that the end:
|--------| |--------|
| :MyA |<>------| :MyB |
|--------| |--------|
Today I came across a notation like <>-----> (with an explicit arrow head on the right end). So I looked it up in the UML 2.4 specification and actually found references for both versions.
My favourite reference: "UML and Patterns" by Craig Larman only mentions the first version without the arrow. In the UML specification I found a notice about navigable ends, but I am not sure if this is related and if whats the difference?
Could someone explain this more thoroughly and give an example for the use of each version?
Any association end can be designated to be "navigable" with the help of a navigability arrow. However, UML's notion of "navigability" does not have a precise meaning and people confused it with the concept of an association end being owned by the class at the other end. This concept of association end ownership by a class means that the association end corresponds to a reference property of that class. This issue has only been clarified in last year's new UML version 2.5, which has introduced a new visual notation for association end ownership, a "dot" as in . This is most likely the intended meaning of what you came across, namely , and what it really means is the following reference property:
For more explanation see this tutorial.
Additional answer to the comment: If your class model specifies the ownership of all association ends, and your class diagram displays them, and there is no ownership dot (nor a navigability arrow), as in , then, according to UML 2.5, the association end is "owned" by the association itself. Since we do neither have an ownership dot at the MyA end, both ends are "owned" by the composite association. In terms of the code to write/generate, this would mean that the composite association is not implemented by means of reference properties in either MyA or MyB, but has to be implemented with the help of a separate class, say "MyA-has-MyB", which has two reference properties for referencing both the aggregate and the component of any composition link, as in the following class rectangle:
One arrow means the association is navigable this way. No arrows means the association is navigable BOTH ways. Two arrows are omitted.
It could be a problem, because two ends with undefined navigability look out the same way, but it is the standard.
You can read more thoroughly about associations/navigability/aggregations in this my answer https://stackoverflow.com/a/21478862/715269
Direction implies a client/server or master/slave relationship. In the case of aggregation, the usual situation is the programmer uses the aggregate to find the sub-components for that object (e.g., use the car to find the car parts). Directionality towards the part class makes this relationship explicit, though in most cases it is redundant.
An association has two ends. An association’s end is modeled by means of a UML Property which can be owned by the classifier involved at the related end of the association, in that case the association is said to be navigable as the source classifier can directly refer to the target instance (the instance at the other end of the association) by means of that property. Otherwise the property representing the association end may be owned by the association instance itself
see http://lowcoupling.com/post/47802411601/uml-diagrams-and-models-with-papyrus

Resources