Difference between association and dependency?

2019-01-10 03:33发布

In a UML class diagram, what is the difference between an association relationship and a dependency relationship?

From what I know, an association is a stronger relationship than a dependency, but I'm not sure how it is stronger.

Any example would be more than welcome :)

标签: uml
7条回答
小情绪 Triste *
2楼-- · 2019-01-10 04:00

Dependency - A change in a class affects the change in it's dependent class. Example- Circle is dependent on Shape (an interface). If you change Shape , it affects Circle too. So, Circle has a dependency on Shape.

Association- means there is a certain relationship between 2 objects

(one-one, one-many,many-many)

Association is of 2 types-

  1. Composition
  2. Aggregation

    1) Composition- stronger Association or relationship between 2 objects. You are creating an object of a class B inside another class A

 public class A {
       B b;
       public void setB(){
         this.b= new B();
        }
     }

If we delete class A , B won't exist( B object is created inside A only).

Another example -Body & Liver .Liver can't exist outside Body.

2) Aggregation - weaker type of Association between 2 objects.

public class A {       
             B b;
             public void setB(B b_ref){
                 this.b= b_ref;   
                /* object B is passed as an argument of a method */
              }
   }

Even if you delete class A, B will exist outside(B is created outside and passed to Class A)

Another example of this- Man & Car . Man has a Car but Man & Car exist independently.

查看更多
登录 后发表回答