This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
When I draw the diagram, I feel it's hard to make decision when I should use association or dependency. For example, for entity relations, I know we should use association including bi-directional, uni-directional, aggregate or composite relations, Then for the relation between control objects, or relation between control object and entity object, for example, EJBs with DAOs, DAOs with entitys etc. I am always confused to use uni-directional association(line with arrow head to dependent object) or dependency(dashed line with arrow head to dependent object) for such realtion.
Here is my take on it. Association represents "has-a" relationship, dependency represents a fact that one entity uses another to get something done (one entity depends on the another entity's behaviour). E.g. association - Newspaper has Subscribers (1-to-many relationship); dependency - Newspaper Subscription service uses Delivery service
Hi, I'm new here. Sorry to hijack the thread but I'm having trouble with grocking when to use unidirectiona lassociations as well. I have two classes, Person and Role and I don't know whether to go with
I'm leaning towards the first based on this definition: "In a uni-directional association, two classes are related, but only one class knows that the relationship exists.". The idea is that Role should hold information on the status of the User (so that the system can tell if the user have admin status or not) and I don't see any reason for the User-class to know about the Role-class. Is my reasoning completely wrong?
Joined: Apr 11, 2004
I guess Role-User is a "many-to-many" relationship where a particular user can have more than one role and vice versa a particular role can have more than one user associated with it. I'd make this relationship a bi-directional relationship where a user has a collection of Roles that he or she has, and a role has a collection of Users that are associated with it.
From the implementation point of view such relationship can be implemented with the help of the mapping (association) class. One instance of such class represents a mapping between a user and a role. It can also have additional fields such as the date when a user was granted this role. On the database level, this mapping class can be represented by a table with the foreign key columns to the User and Role tables.
Joined: Jan 19, 2010
Thanks for the reply. Actually a User can only have one role within the system. He/she is either an administrator or a regular user. The class-diagram I'm making is reversed engineered from the following script:
CREATE TABLE role (
role_id BIGINT PRIMARY KEY,
ok, I'd still make it bi-directional - a user knows about a role it's got, and a role has a collection of users
Joined: Jan 19, 2010
Alex Sharkoff wrote:ok, I'd still make it bi-directional - a user knows about a role it's got, and a role has a collection of users
I appreciate the input, bi-directional it is. But I still don't really understand the concepts involved. The only clear-cut example of a uni-directional association seems to be between the Person and Address classes:
Person lives_at Address
Are there other typical cases when it's appropriate?