aspose file tools*
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes uni-directional association (or Directed Association ) vs dependency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "uni-directional association (or Directed Association ) vs dependency" Watch "uni-directional association (or Directed Association ) vs dependency" New topic
Author

uni-directional association (or Directed Association ) vs dependency

mimi mang
Ranch Hand

Joined: Dec 02, 2009
Posts: 36
Hello Ranchers,

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.

Any explanation or idea would be appreciated.

Thanks.
Alex Sharkoff
Ranch Hand

Joined: Apr 11, 2004
Posts: 209
Hi Mimi,

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


Alex (SCJP 1.4, SCBCD 1.3, SCWCD 1.4, SCJD 1.4)
Rahul Mishra
Ranch Hand

Joined: Jan 22, 2006
Posts: 211
Association and dependencies depict varying level of coupling between two objects.

I agree with Alex, but to derive thumb rules, here is what i believe -

  • If Class X has an instance level variable for Class Y , Class X is associated to class Y.
  • If Class X has methods which return or accept 'types' of Class Y , then class X depends on Y.



  • Association is a stronger relationship than a dependency.

    I have followed the above rules (in the order mentioned) for my designs so far.

    Regards


    OCMJEA/SCEA, SCDJWS, SCBCD 1.3, SCJP 1.4
    My SCEA experience:http://javalogue.blogspot.com/
    mimi mang
    Ranch Hand

    Joined: Dec 02, 2009
    Posts: 36
    good points. It's more clear for me now.

    Thanks.
    J J Wright
    Ranch Hand

    Joined: Jul 02, 2008
    Posts: 254
    Another way of thinking about it:

  • Associations exist for the lifetime of the object
  • Dependencies are transitory relationships


  • Check out Scott Ambler's guidelines. Ambler's Element's of UML 2.0 Style is a really worthwhile book - I found it far more useful than UML Distilled.


    SCJP, SCWCD, SCBCD, SCEA 5
    Kristofer Hindersson
    Greenhorn

    Joined: Jan 19, 2010
    Posts: 25
    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

    Role(name:String) --> User(name:String, password:String)

    or

    Role(name:String) --- User(name:String, password:String)

    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?
    Alex Sharkoff
    Ranch Hand

    Joined: Apr 11, 2004
    Posts: 209
    Hi Kristofer,

    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.
    Kristofer Hindersson
    Greenhorn

    Joined: Jan 19, 2010
    Posts: 25
    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,
    name VARCHAR(255)
    );

    CREATE TABLE person (
    person_id BIGINT PRIMARY KEY,
    name VARCHAR(255),
    surname VARCHAR(255),
    ssn VARCHAR(255),
    email VARCHAR(255),
    password VARCHAR(255),
    role_id BIGINT REFERENCES role,
    username VARCHAR(255)
    );

    Here's what I got so far:
    Alex Sharkoff
    Ranch Hand

    Joined: Apr 11, 2004
    Posts: 209
    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
    Kristofer Hindersson
    Greenhorn

    Joined: Jan 19, 2010
    Posts: 25
    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?
    Alex Sharkoff
    Ranch Hand

    Joined: Apr 11, 2004
    Posts: 209
    you may find this info useful http://www.ibm.com/developerworks/rational/library/content/RationalEdge/sep04/bell (see "Association" section)
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: uni-directional association (or Directed Association ) vs dependency