• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

uni-directional association (or Directed Association ) vs dependency

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

     
    mimi mang
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    good points. It's more clear for me now.

    Thanks.
     
    Ranch Hand
    Posts: 254
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 209
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 25
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 209
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 25
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 209
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    you may find this info useful http://www.ibm.com/developerworks/rational/library/content/RationalEdge/sep04/bell (see "Association" section)
     
    Yeah, but is it art? What do you think tiny ad?
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic