Tutorial On Association
By: Robin Chander
What is an Association
Relationship in UML modeling?
- In UML models , an
Association is a relationship between such as classes , that describes the
reasons for the relationship and rules to govern that relationship.
An association represents a
structural relationship that connects two classifiers. Like attributes ,
association records the properties of classifiers.
An association is nothing but
a line drawn between the participating classes.
Under what circumstances it
is used?
- It can be used to show the
design decisions that one make about the classes in the application that
contain data and to show which of those classes need to share data.
How it looks in UML?
- In UML class diagrams ,
association relationships in JAVA application represents the following things:
1) A semantic relationship
between two or more classes that specifies connections between their instances.
2)A structural relationship
that specifies that objects of one class are connected to objects of second
class.
a) Bidirectional Association
Relationship:
In bidirectional association relationship ,
simply a line will be shown which indicates association between the classes ,
there will be no arrowhead sign on either sides of the line.
b) Unidirectional Association
Relationship:
In unidirectional association
relationship , there will be a line with arrowhead pointing towards the target.
Here Class One is source and Class Two is target.
How it is implemented in
code?
-In Java there are three
steps to implement association relationship.
1) The Foremost thing thing
is , we need to declare an instance variable of the class.
2) Assignment of existing
instance to instance variable.
3) Parameter of known class
in other class constructor.
Let us take Cup-Coffee
example to illustrate the above mentioned points.
package tutorial;
public class Cup {
private Coffee
_coffee;
public Cup(Coffee
coffee) {
_coffee
= coffee;
}
}
* Code in green colour is instance variable declaration and the
constructor definition is in blue colour.
How
is Composition Relationship different from Association Relationship?
- In UML class diagrams the
Composition relationship is shown using a solid line connecting two classes
involved in relationship . The one end of the line is marked with a diamond.
Here , First Class is the
source and Second Class is the target.
Composition Relationship in
code:
Let us assume a class named
Cup to have Coffee as property.
First of all , we need to
declare an instance variable.
package tutorial;
public class
Cup {
private Coffee
_coffee;
public Cup() {
_coffee = new tutorial.Coffee();
}
}
* The code written in Green is instance variable declaration and the code
written in Blue is constructor definition.