Difference between dependency and composition?

后端 未结 3 1328
無奈伤痛
無奈伤痛 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:22

    The difference can be seen in the two constructors:

    • Dependency: The Address object comes from outside, it's allocated somewhere else. This means that the Address and Employee objects exists separately, and only depend on each other.

    • Composition: Here you see that a new Engine is created inside Car. The Engine object is part of the Car. This means that a Car is composed of an Engine.

提交回复
热议问题