aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Dependency vs. Association Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Dependency vs. Association" Watch "Dependency vs. Association" New topic
Author

Dependency vs. Association

Edy Yu
Ranch Hand

Joined: Nov 21, 2000
Posts: 264
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 ]

SCJP, SCJD, SCWCD, SCBCD, SCEA, IBM Certified Enterprise Developer, WebSphere Studio V5.0
Sunny Liu
Ranch Hand

Joined: Mar 15, 2002
Posts: 63
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.


If I am rich, I will spend more.<p>IBM 486 (OOAD & UML) & 141 (XML) passed<br />Oracle 1Z0-007 passed<br />MCSD MCDBA MCSE <br />SCJP SCSSA<br />CCNA CNA A+
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
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

Joined: Mar 06, 2001
Posts: 282
How to draw the relationship between A and B
when use UML?
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
In UML a dependency is represented as a dashed arrow.
Class A ---------> Class B
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
You can also look at this thread
Poonam Akash
Ranch Hand

Joined: Jul 26, 2001
Posts: 100
I guess this kind of relationship is an example of Aggregation(a type of dependency).
bye,
Poonam.
Paulo Salgado
Ranch Hand

Joined: Jan 18, 2002
Posts: 98
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

Joined: Apr 01, 2002
Posts: 3
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

Joined: Nov 05, 2000
Posts: 761

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


Regards,

Pho
Jim Baiter
Ranch Hand

Joined: Jan 05, 2001
Posts: 532
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

Joined: Mar 31, 2002
Posts: 1
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


Thanks,<br />Kuppuram
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
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

Joined: Sep 16, 2003
Posts: 1
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

Joined: Nov 13, 2001
Posts: 64
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

Joined: Jul 02, 2003
Posts: 76
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 ]

---<br />Uncle Bob.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dependency vs. Association