What I would like to ask is that if one class has a variable of array, should I include it as attribute along with the association or only the association?
So, for example, if I want to draw a simple diagram about association between professor and lecture, which side would be correct (Upper one or the lower one)?
- Lecture may have more than one instructor
You would use the dot notation with role names like this:
It means that Professor has * lectures as owned property and vice versa Lecture has 1..* teachers.
The dot and role name are located "on the opposite site" of the owning class.
Related
For example, let's say I have a situation like this
Can I write an OCL expression that counts how many students a school has, such as:
School.students->count()
?
Or would it be incorrect? And in that case, how would I count the number of students?
Yes, the dot is the correct OCL syntax, here.
This is explained in the OCL specifications, section 7.4.10 on navigation operators:
The "." navigation operator supports navigation from an object using a property or operation.
The "->" navigation operator supports navigation from a collection using a property, operation or iteration.
Furthermore, section 7.5.3 on navigation of properties explain that:
Starting from a specific object, we can navigate an association on the class diagram to refer to other objects and their properties. To do so, we navigate the association by using the opposite association-end:
object.associationEndName
The value of this expression is the set of objects on the other side of the associationEndName association.
There is just one issue here: students is not formally defined. In principle you should use the role name indicated at the association end (next to the *), and if no such role is given in the class diagram, you should use the class name instead, so student.
Not related: count() is used with an argument regarding the objects that have to be counted in the set. If you mean the number of students associated with a given school, you should use ->size()
In an UML class diagram:
a) Do you have to state attributes that are aggregated? Or is it enough with the arrows indicating aggregation?
b) Do I have to add "id" as an attribute or is it a given?
Thanks.
You are using a shared aggregation in the picture. That does not have any defined semantics as per UML 2.5 (see p. 110). If you need a composite aggregation the diamond must be filled. In that case the aggregated object will be deleted along with the aggregating one (the latter must assure that constraint). In your model it makes no sense. No employee aggregates a department. Even vice versa I would have doubts or at least reason for discussion.
An id is only needed if it has a business purpose (e.g. an article number). If you transform your model to a database you introduce an artificial id for technical reasons. But on an abstract business level they are not modeled.
Your models only differ in the use of attributes for associated classes. The B variant is preferred. But you need to place the attributes as role names towards the associated classes (as -department and -branch). What you have placed in the middle of the connectors is rather the association name. Badly chosen with the + in front. Naming associations is rarely needed. So get rid of that. Role names shall be placed near the class that takes the role. Also it's a good idea to use the dot-notation to show that the roles represent owned properties. Just place a small black dot near the left hand side of both (near where the role names should go).
As for the dot-notation UML 2.5 states on p. 18:
Dot notation is used to denote association end ownership, where the dot shows that the Class at the other end of the line owns the Property whose type is the Class touched by the dot. See 11.5.4 for details of Association notation and 11.5.5 for examples.
Also as JimL. commented the A-version uses associations plus attributes which introduces redundancy. This is not illegal but likely not intended and at least leads to confusion.
I have a question about my UML diagram. I have made a system consisting of a "User" parent class and the two child classes inherit from the "User". Now my question is how to show the multiplicity notation between them, just like the multiplicity notation is shown between "Booking" and "client", where more clients can only have one booking each and the notation would be many to one.
The question is:
The user in our system can only be a practitioner or a client, not both at the same time -->, therefore, the notation would be 1 to 1
There are more users and the different users could be either a practitioner or a client --> therefore the notation would be 1..n to 1..n
What is the right way to write it? Is it wrong to write multiplicity notation when a class inherits? Please help I have spend like 2 hours on figuring it out.
You don't. There are no multiplicities on a Generalization in UML.
The fact that your User is either a Cient or Practitioner can be expressed by setting your User abstract. The notation for abstract classes is to set the name in italic. This way you cannot instantiate User instance directly, but you have to use one of the concrete subclasses.
The second phrase doesn't really constrain anything, so you don't have to mention it at all.
Remark: the guillemets « and » are reserved in UML to denote stereotypes and keywords. As it is right now your User class is really an unnamed class with stereotype «User»
Let's assume I have 2 classes with association relation.
What is the difference between adding attributes to one of the classes and adding an Association Class with attributes?
I understand that association class describes the association relation, but cannot I use simple class attributes instead?
What are the added values of Association Class?
Association and attributes are actually different renderings of the same thing. The difference is more a bit of esoteric nature. An association is visually more exposed than an attribute. So you would use the attribute if it has a more local importance rather than a more system wide in case of an association.
What you should not do is to use both. When using an association and you need to name it then use a role along with the association and do not repeat it as attribute.
An association class (AC) is actually a combination of a class and an association (in UML it inherits from both). This way the pure relation between two classes can have methods and attributes itself. An association class is used if you want to relate two classes with M-N relation. E.g. the AC between Student and Exam describes the results a student has achieved in exams. So you have a Class 1-M AC N-1 Class relation rather than having this information as array attributes inside either of the opposing classes.
As a side note (since you tagged it EA): EA offers a drop down in the roles which is fed from attributes of the class. You should not make use of that. This is some heritage and/or a misinterpretation of the definition. As said: its not wrong, but also not good style.
You say you already have two classes with an association between them. In that case, there is a huge difference between adding attributes to one of those two classes and changing the association into an association class with its own attributes. They are not at all similar.
Unnamed properties already exist at each end of the association and are typed by their adjacent class. Adding redundant attributes is unnecessary and would be incorrect.
An association class allows you to record additional facts about a link between instances of the other two classes, such as the point in time the link came into existence. Association classes are like associative entities in a database.
I think part of your question is also about when to use an association vs when to use attributes. The UML spec doesn't provide any guidance on this. However, common practice is to use an attribute only for types that have no identity, such as the number "5", the string "Hello", or the combination of "7" with an enumeration literal of "pounds". In UML, these types are called primitives and datatypes. If you want a property typed by one of those, use an attribute; otherwise, use an association with named association ends.
When creating relationships in an Object Class Diagram for an Object Relational Database, should the diamonds on the ends of the relationship links be filled in or not.
Here is an image of my Class diagram:
http://canning.co.nz/Weltec/Class_Diagram.png
It's a choice between Composition and Aggregation, which Wikipedia explains quite well.
In practice though, I think a valid answer is to just not worry about the difference, unless it's a school assignment. I've found that trying to make very detailed UML diagrams isn't terribly useful in practice.
Composition relationship has a coloured diamond shape structure ending at a class if it belongs to a 'is a' relationship (i.e- the entity cannot exist without the parent class) whereas the aggregation has an empty diamond shape ending at a class if it belongs to a 'has a' relationship (i.e- the entity can exist without the existence of the parent class.
A full diamond denotes Composition, or a 'owns' relationship. You use it when the referenced entity can't exist without the class representing it. An example would be order to order item. The order item just doesn't make sense without the order.
An empty diamond denotes Aggregation, or a 'has' relationship. A quick glance at your diagram makes me think this is the correct diagram element to use in your case.
But I agree with #mpartel: If there aren't any specific requirements to distinguish between the two just ignore the diamonds.