Saturday, February 23, 2008

Relationships between clases in a UML Diagram

Introduction

I realize a lot of people have a hard time understanding UML Class diagrams because they confuse the kinds of relationships between classes. I decided to list these relationships and write a short description for each one. These relationships could exist between other objects too (e.g. an interface and a class).


Discussion
The relationship between two related classes can only be one of the followings:

Dependency- One class uses or knows about the other. changes to the definition of one class (the supplier) may cause changes to the other class (the client). Is usually thought as "...uses..." and is the weakest relationship between two classes.

Rational Rose definition:
A dependency is a relationship between two model elements in which a change to one model element will affect the other model element. Use a dependency relationship to connect model elements with the same level of meaning. Typically, on class diagrams, a dependency relationship indicates that the operations of the client invoke operations of the supplier.

In UML this is indicated by a dashed line pointing from the dependent (or client) to the independent (or supplier) element.

Examples with UML Diagrams:
World population and population of Germany










Association
-Semantic connection between two classes. An association could be unidirectional or bidirectional. Could be reflexvie (1). Can be named and could have multiplicity. (3)

Rational Rose definition:
An association provides a pathway for communication. The communication can be between use cases, actors, classes or interfaces. Associations are the most general of all relationships and consequentially the most semantically weak. If two objects are usually considered indepen
dently, the relationship is an association

The UML graphical representation of an association relationship is simple line between two objects or an unfilled arrowed lines that connects. Direction of the arrow shows the direction of traversal.

Examples with UML Diagrams:

Bank telelr - customer









Aggregation
-Strong whole-part association. Typically thought as has-part. Could be reflexive.

Rational Rose definition:
An aggregate relationship is conceptually the same as an association with the aggregate adornment set. The aggregate association adornment denotes a whole or part relationship. Aggregate adornments are purely conceptual and have no effect on code generation, nor reverse engineering. Thus, the code generated for an aggregate relationship is the same as for an association relationship.
The aggregate association may be navigable on both ends, and data members are generated for the associated classes on the navigable side(s).

In UML, it is graphically represented as a clear diamond shape on the containing class end of the tree of lines that connect contained class(es) to the containing class.

Examples with UML Diagrams:
Hand and finter







Composition
-Whole part association, Also called Composition Aggregation. The difference between Aggregation and Composition is that in composition the part could only be part of ONE whole. in other words in composition a part must be for only one whole but in aggregation the whole may have any multiplicity. Also in composition parts and wholes are contingent (2). In other words-in composition, when the owning object is destroyed (the whole), so are the contained objects (Parts), however in aggregation, this is not necessarily true.

The UML graphical representation of a composition relationship is a filled diamond shape on the containing class end of the tree of lines that connect contained class(es) to the containing class.

Examples with UML Diagrams:
Coffee table and its legs.



Another example to show the difference between composition and aggregation :A university owns various departments (e.g., chemistry), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors.








Generalization
- One class is more general than the other class. Refers to the concept of inheritance in OOP programming. Usually are not named and don't have multiplicity. Usually thought of as "..is a/an..."


Rational Rose definition:
A generalize relationship is a relationship between a more general class or use case and a more specific class or use case. A generalization is shown as a solid-line path from the more specific element to a more general element. The tip or a generalization is a large hollow triangle pointing to the more general element.


Examples with UML Diagrams:
Animals and cats and dogs















Realization- is between a class and an interface class. And shows that the class is implementing the interface.

Definition of an interface according to Rational Rose: An interface specifies the externally-visible operations of a class and/or component, and has no implementation of its own. An interface typically specifies only a limited part of the behavior of a class or component.

Example:
Let's assume we have two servers (a voice server and a video server), and we want to hire some developers to implement these two servers, we could give them an interface so that they know what methods and attributes they should implement.

In UML the graphical representation of realization is a dashed line with an empty arrow at the end that connects a class to its interface. However in rational rose when you change the stereo type of a class into an interface it will automatically draw a continues line and remove the arrows.




For a tutorial on Java interfaces visit:
http://java.sun.com/docs/books/tutorial/java/concepts/interface.html


This is a neat diagram that shows all
these relationships in a single picture.

I stole it from:
http://www.actionscript.com/Article/tabid/54/ArticleID/an-introduction-to-uml-key-concepts-applied-to-flash-development/Default.aspx

It's a good idea to print it and look at it while you are getting used to UML diagrams.














Practice

It's a good idea to search for class diagrams in Google image search, you should be able to understand all diagrams if you read this articles carefully. So, go search here:

http://images.google.ca/images?um=1&q=class+diagrams


References

I have used examples, definitions and ideas from the following books/resources in this article:

UML 2.0 in a Nutshell, By Dan Pilone, Neil Pitman, O'Reilly ,June 2005, ISBN 0-596-00795-7

Sams Teach Yourself UML in 24 Hours, Third Edition, By Joseph Schmuller, Sams Publishing, March 15, 2004, ISBN 0-672-32640-X

Mastering UML with Rational Rose 2002, Wendy Boggs Michael Boggs, Sybex, 2002, ISBN 0−7821−4017−3

Applying UML and Patterns, Second Edition, Craig Larman

UML Distilled Third Edition, Addison Wesley

Rational Rose Help

Wikipedia


Notes

1) By reflexive I mean it could have the same relationship with itself. For example in a linked list ADT we usually use at least one instance of the linked list class.

2) By contingent I mean their existence and non-existence exist on each other-one can not exist without the other. In other words existence of one implies existence of the other and non-existence of one implies non-existence of the other. For example if there are no table legs, there is no coffee table and also if there is no coffee table, there are no table legs.

3) Multiplicity denotes the number of objects of one class that can relate to one object of an associated class. For example 11 players could be associated with a soccer team.

No comments: