There two types of relationship between Classes: dependencies and associations.When we talk about one and while we forget another, this is misunderstanding caused by definition of dependency and association.Normally, the definition of those terms are:

Dependency: there are two classes, class A depends on class B. Class B will be affected when class A changed.

Association: An association represents a structural relationship that connects two classes.

 

the key point:  Association imply that there is strong dependency between two classes.In other words, the definition for association should be:

An association represents a structural relationship that connects two classes.class A associate with class B. Class B will be affected when class A changed yet.

 

In my view, dependency emphasize the needs of on class to another. the association emphasize that structure: one class holds the another, it can use freely when it needs.

Last but not least, a simple demo will told us the truth:

The diagram describe the blow relationships: 

Dependency & association in UML

class Program { class Phone { public void SendMessage(string name,string message) { Console.WriteLine("Hi,{0}\n{1}",name,message); } } class Employee { public void SayHello2Customer() { Phone phone = new Phone(); //Dependency, employee needs the phone phone.SendMessage("Someone", "Hello"); } } class Manager { Employee staff = new Employee(); //Association, a structure shwos that Manager employeed the staff. public void SayHello2Customer() { staff.SayHello2Customer(); //Manager can employ the staff do something if he want. } } static void Main(string[] args) { Manager manager = new Manager(); manager.SayHello2Customer(); Console.ReadKey(); } }

相关文章:

  • 2021-11-02
  • 2021-10-30
  • 2021-09-30
  • 2021-09-04
  • 2021-07-04
  • 2022-12-23
  • 2021-10-24
  • 2022-03-06
猜你喜欢
  • 2021-06-02
  • 2021-07-21
  • 2021-12-27
  • 2022-12-23
  • 2021-06-12
  • 2022-12-23
  • 2021-12-17
相关资源
相似解决方案