Difference between dependency and composition?

后端 未结 3 1360
無奈伤痛
無奈伤痛 2021-02-02 00:46

Definitions taken from here

Dependency

Change in structure or behaviour of a class affects the other related class, then there is a dependency b

3条回答
  •  时光说笑
    2021-02-02 01:14

    Simply put :

    Thanks to Marko Topolnik for this...

    1. Dependency occurs when one object "is dependent" on another. It can occur with or without a relation between the 2 objects. Actually, one object might not even be knowing that another exists, yet they might be dependent. Example : The Producer-Consumer problem. The producer need not know that the consumer exists, yet it has to do wait() and notify(). So, "NO" , dependency is not a subset of association.

    2. Composition : Is a type of association in which the "child" object cannot exist without the parent class. i.e, if the child object exists, then it MUST BE IN THE parent Object and nowhere else.

      EG: A Car(Parent) has Fuel injection system(child). Now, it makes no sense to have a Fuel Injection system outside a car (it will be of no use). i.e, Fuel injection system cannot exist without the car.

    3. Aggregation : Here, the child object can exist outside the parent object. A Car has a Driver. The Driver CAN Exist outside the car.

提交回复
热议问题