I have a situation where the key of two tables will map to one row of a third table.
For example, let's say that each combination of classroom and topic will map to one teacher.
How can I represent that any tuplet (classroom, topic) is a one to one relationship with a teacher ?
I would simply model that as an aggregation like so:
Using an association class like pointed out by #xmojmr would probably only make sense if there is some operation connected (as the posted link also states).
Whether you use a simple association or a composition between Course and Teacher depends on your domain. The shown way will fix it and a course only exists if all 3 parts are defined. Using a simple association will tell that a course virtually exists and can be held by an arbitrary teacher. Still then topic/class room relate as tuple to a teacher.
Related
I have short question about class diagrams. In my book we have class Person and class Gender and agregation arrow between them(with diamond pointing to person). Now, in general when I want to decide whether we have agregation or not I am using one of these two rules:
1.When you destroy class that is whole, than part can exist without it;
2.Class that is part in agregation relation, can be mutual to one or more wholes.
Now if we look at this example and rule number 2, it is OK, because one gender is mutual to one or more persons. But for the first one, if there is not person, than we can't talk about gender right?So I would set composition here. Probably I am missing main difference between these two. Any help is appriciated.
In general
Your rule about when using aggregation is not wrong. But it's unnecessarily complex. There is a simpler much simpler rule about when you'd better use aggregation: never.
This may sound provocative, but the hard truth is that the meaning of aggregation is not defined in the UML specifications, which makes it ambiguous and subject to a lot of unnecessary time-consuming debates:
Sometimes a Property is used to model circumstances in which one instance is used to group together a set of instances; this is called aggregation. (...) Precise semantics of shared aggregation varies by application area and modeler.- UML specifications 2.5.1, page 112.
I know, it comes as a shock. For years in my career, I have myself selected very carefully aggregation whenever there was a part-whole relation with non-exclusive ownership. But when I came accross James Rumbaugh famous quote, I challenged my own assumptions and realized how vain and subjective this quest was:
Keep in mind that aggregation is association. Aggregation conveys the thought that the aggregate is inherently the sum of its parts. In fact, the only real semantics that it adds to association is the constraint that chains of aggregate links may not form cycle (...) In spite of the few semantics attached to aggregation, everybody thinks it is necessary (for different reasons). Think of it as a modeling placebo.- James Rumbaugh in Unified Modeling Language Reference Manual, chapter 14.
So, whenever you have aggregation in a model, you could simply replace it with an association without real loss of information.
In your specific example
The association: Person ----- Gender expresses perfectly that a person has a gender, and that several persons can share the same gender.
If you want to be super-accurate, you could use the dot notation (with a small dot on Gender side). This would convey the information that Person owns the end of the association.
Composition would definitely be wrong here, because it's an exclusive ownership and no two persons could share the same gender.
Aggregation is ambigous: what is the whole, what is the part? If gender is a part, wouldn't character be a part as well. And what with the name, then ?
A final remark: if you want to implement this with Person having a gender:Gender property (an OOP mechanism called "object composition") the, you don't need aggregation (even if it's a popular practice).
I know there are many articles and many answers on this question but I really not understand it.I read many answers but I not understand it fully. I know what is association, aggregation, composition. My question is simple that what is the difference between only aggregation and association and when to use them. I am not talking about composition.
For example course and student are two classes. Now which relationship is between them. Is association or Is aggregation ?
"Aggregation" means to add things together. In the context of UML class modelling it means that one class is part of another class. To be clear (it sounds like you already know) composition is a form of aggregation -- composition simply means that the class that is being composed cannot exist outside the composing class (also known as "strong aggregation"), whereas aggregation means that the composed can exist outside of the composing (also known as "weak aggregation"). For example, a car might aggregate four wheels but these wheels can exist when removed from the car (weak form), whereas the mind cannot exist outside of the brain (strong form).
"Association" means that two things are associated somehow. It means nothing more than that in the context of UML class modelling.
Given this, it seems obvious that the relationship between a "Course" and a "Student" is an association as neither aggregates the other. That is assuming that you and I mean the same thing by Course and Student, of course.
Scenario
A teacher at a university is able to search for a student by using their first name and last name. Similarly, a student can search for a teacher using their first name and last name.
What I've Done
I have used an association line with a label, searchesFor, to denote that a teacher can search for a student and vice versa. I have also used the no more than one multiplicity notation.
Question
If I don't use a filled arrow next to searchesFor to indicate the direction of the relationship, would my solution be naturally read as stated in the scenario?
No.
The UML specification does not provide any standard in such case. It is quite often that you don't show filled triangles, placing them only when the understanding might be unclear so the reader can think you just didn't notice ambiguity here.
You might use textual annotation (comment) to make it clear.
Also beware of the "database like" multiplicity usage. The multiplicity of one at each end means there's only object on each link end. If a teacher has searched a student, do you want to limit this students search to this single teacher only?
Finally search for is a very short lasting relation. Are you sure it is worth documenting like that at all?
I think your design is just wrong. There must be some instance to collect all teachers and all students from where you can get by name:
Sure you could simply relate students to teacher with a m-n relation. But that would result in a bad design. When you insert a new student, this must be added to all existing teachers (so they can find appropriately). Vice versa teacher/student applies the same way.
Whether or not you have a single Staff instance or separate ones for teachers and students depends.
I have a problem with my UML diagram and I'll be happy if you help me. I have relationships like
Object A (1)<>----(0..*) Object B
Object B (1)<>----(0..*) Object A
and I'd like to unite them. How can I do it? Thanks a lot.
Can you please define the term "unite them"? What exactly would you like to achive? An example could probably help.
Meanwhile, I can try to guess and give you two possible solutions. Maybe they help you to rephrase your question or even to find the solution:
In the Solution 1, I've just made a single relationship that describes both of yours. This can be used if there is only a single and clear criterion of the linkage between the objects. A typical n..m relationship. Objects A will each hold a collection of related objects B and vice versa.
For example a Person (A on the diagram) can join several Clubs (B) and a Club can have several members - there is only one logical relation behind this situation - membership.
Solution 2 is where there are actually 2 different ways to relate between those elements, each one 1..n. So, A holds a collection of Bs and B holds collection of As, but they are unrelated.
Extending the same example - a Person (A) can join only 1 Club (B) and a Club can have many members and hold their reference (col_a on the diagram). In the same time, a Club can have only 1 owner, and a Person can own several Clubs (col_b). Here we have two different logical relations - membership and ownership.
Of course, other multiplicities and navigabilities are possible, this is just an example to give you an idea.
Does one of these situations sound like yours?
UPDATE (after the 1st comment):
So, here is the updated solution 1:
This is an aggregation used here, and this is more a Group-member relationship. It suits perfectly the description of my first solution up there. Members (B) can be "shared" between the Groups (A) and Gruop does not have any special control over their lifetime.
The real Whole-part relationship would employ composition instead of aggregation (visually depicted with a black diamond, instead of a white one). Its semantics it that Whole object has a full control over the life of the contained objects (parts). In consequence, the Parts cannot be shared between several Wholes and must be destroyed if the Whole itself is destroyed.
Now you just need to find out which situation describes your problem the best, pick-up on of this solutions and eventually fine tune the multiplicities.
Here is a way you could represent this scenario in UML.
One server can contain 0 or many Functions (ie. aggregate relationship).
Each function must belong to one server. Or if it is a distributed function then it can belong to many servers.
I find these two examples conceptually identical - yet one is a composition and the other aggregation.
In the first example, the relationship 'class (has-a) students' is a compositon.
A class contains students. A student cannot exist without a class.
There exists composition between class and students.
In the second example, the relationship 'department (has-a) professors' is an aggregation.
If the university closes, the departments will no longer exist, but
the professors in those departments will continue to exist
In my opinion the first one is plain wrong. Notice that in the comment section of this SO question #TallPaul is questioning the first example as well. I think in practice it would delete all students enrolled in a class after each semester from the system. Moreover, the students would probably have to be created by the class on its initialisation, because composition in C++ is usually implemented as private attribute (not pointer). Am I right? Is there any way the first example makes sense?
There is no absolute truth and it all depends on the system you are modeling. You can create a system where students are instances that exist only in a specific class and when the class is deleted, so are the students. This may make sense when you don't want to store student information between classes for example.
Yes, those are weak examples, a Class must have a Subject would be a much better example for Composition. The relationship between a Class and Students is Aggregation because the life-time of the two is different.
See [UML Associations in Java] for more detailed examples1