• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dependency vs. Association

 
Edy Yu
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm always confused by those two relationships.
Could some one explain the difference between them? It will be great if you can explain it from Java perspective.
By the way, one of my confusion is:
class A {
public void methodA() {
B b = new B();
b.methodB;
}
}
class B {
public void methodB() {
}
}
What's the relationship between class A and class B and why?
[ March 27, 2002: Message edited by: Edy Yu ]
 
Sunny Liu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is Association, Class A knows Class B.
Dependency is between packages. Usually it happens on that you are using third-party utility in your code. but it is not very clear between Dependency and Association. once there is association between classes, if they are in differrent package, then dependency is exist also.
I put them in this way, Association is class level relationship and dependency is package level relationship.
If I am wrong, please correct me.
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe it represents a dependency relationship. An association represents a permanent relationship which will exist during the whole life of the object. The object "b" is a object created locally within a method and will not exist at the end of the method.
So I believe there is dependency between object A and object B.
 
sarah Marsh
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to draw the relationship between A and B
when use UML?
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In UML a dependency is represented as a dashed arrow.
Class A ---------> Class B
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also look at this thread
 
Poonam Akash
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess this kind of relationship is an example of Aggregation(a type of dependency).
bye,
Poonam.
 
Paulo Salgado
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
Agree with CJ Jack, it's a dependency. And agree for the same reason. CT Arrington's book Enterprise Java with UML has a good discussion on that in the first chapters.
 
Thien Nguyen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If class A has a member data B b, then class A assosiates with class B .
Otherwise, if some A's method has the local variable B b or the argument B b, then A depends on B .
[ April 03, 2002: Message edited by: Thien Nguyen ]
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by cj jack:
An association represents a permanent relationship which will exist during the whole life of the object.

I don't agree with this explanation.
Consider this association:

Depending on how an object instance of C1 is created; it might not have any initial reference to C2. This is similar to the case where an instance of C2 might be created locally within a method of C1.
Cheers,
Pho
 
Jim Baiter
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The glossary in the UML specification defines these terms as follows:
association -
The semantic relationship between two or more
classifiers that specifies connections among their instances.

dependency -
A relationship between two modeling elements, in which a change to one modeling element (the independent element) will affect the other modeling element (the dependent element).
 
Kuppuram
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, we will take dependency for our argument.
We ask a question -
how A is dependent on B - just by calling a method on B from A.
I don't think.
Class A is not using any attribute of Class B and Class B's method is not returning any value.
From this arguement, we can conclude A is not dependent on B.
So A and B are are just associated.
Thanks,
G Kuppuram
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The UML includes a general dependency relationship, which indicates that one element (of any kind, including classes, use cases, and so on) has knowledge of another element. It is illustrated with a dashed arrow line. In class diagrams the dependency relationship is useful to depict non-attribute visibility between classes, in other words parameter, global, or locally declared visibility. By contrast, plain attribute visibility is shown with a regular association line and a navigability(Larman).
Hope this helps.
craig
 
Joe Javaman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a tidbit from "UML Distilled Second Edition":
An association represents a permanent link between two objects. That is, the link exists during the whole lives of the objects, although the instances that are connected may change over time (or, in an optional assocation, be empty). So a parameter reference, or the creation of an object, does not imply an association; you model those as dependencies.
Hopefully this helps clear up any confusion. Have a nice day.
 
Roshan Lal
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edy Yu:
I'm always confused by those two relationships.
Could some one explain the difference between them? It will be great if you can explain it from Java perspective.
By the way, one of my confusion is:
class A {
public void methodA() {
B b = new B();
b.methodB;
}
}
class B {
public void methodB() {
}
}
What's the relationship between class A and class B and why?
[ March 27, 2002: Message edited by: Edy Yu ]

I have been confused by this too, but now I think I know.
The releationship above is a dependancy as cj jack points out.
Association are also kind of dependancy only in the sense if the independent class changes then the dependant class gets affected.
But Association is more stronger relationship than dependancy because an instance of class A should know the existance of an instance of class B and should be able to send message to it.
In the above, the instance of B is created by A, it is not known to A beforehand and it is known to A only locally in the methodA. Once methodA finshes, existence of 'b' is no longer availabe to A.

Also, from an implementation perspective there is no difference between
association and aggregation/composition, atleast in Java. The aggregation
is a stronger form of association where we can think one end as whole
and the other as part. Similarily, composition is stronger form of aggregation in the sense the composed objects cannot exist independently
of containing object.The aggregation puts more constraints on association
and composition puts more constrainst on aggregation.
Hope that helps
Roshan
 
Robert Martin
Author
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edy Yu:
I'm always confused by those two relationships.
Could some one explain the difference between them? It will be great if you can explain it from Java perspective.

An association (and therefore an aggregation and a composition) is the relationship between classes that allows a message to be sent between objects. A dependency is simply the use of a name. Any time a message is sent between two instances, it implies an association between the classes.
So:

Of course UML is developing many different kinds of "slang" or colloquialisms that do not conform with the original intent. Thus, I, and several others, use dependency for this situation:

This is just a shorthand for the more proper:

Originally posted by Edy Yu:
I'm always confused by those two relationships.
By the way, one of my confusion is:
class A {
public void methodA() {
B b = new B();
b.methodB;
}
}
class B {
public void methodB() {
}
}
What's the relationship between class A and class B and why?
[ March 27, 2002: Message edited by: Edy Yu ]

The relationship is an association with a << creates >> stereotype:

[ September 16, 2003: Message edited by: Robert Martin ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic