Difference between dependency and composition?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 12:00:23

问题


Definitions taken from here

Dependency

Change in structure or behaviour of a class affects the other related class, then there is a dependency between those two classes. It need not be the same vice-versa. When one class contains the other class it this happens.

Composition

Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.

Concrete examples in Java from here and here

Dependency

class Employee {
    private Address address;

    // constructor 
    public Employee( Address newAddress ) {
        this.address = newAddress;
    }

    public Address getAddress() {
    return this.address;
    }
    public void setAddress( Address newAddress ) {
        this.address = newAddress;
    }
}

Composition

final class Car {

  private final Engine engine;

  Car(EngineSpecs specs) {
    engine = new Engine(specs);
  }

  void move() {
    engine.work();
  }
}

回答1:


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.




回答2:


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.




回答3:


Dependency refers to usage of objects only within a functions scope. In other words, instances of the class only exist within the functions (or methods) of the containing class, and are destroyed after functions exit.

The example you gave for Dependency is not a Dependency because the Employee class contains an instance of an Address, this is called an Aggregation. An aggregation is like a Composition except the object can also exist outside of the class which is using it. (It can be located outside the classes scope). In your example you are passing a copy of an Address to the Employee constructor. But since it is created outside of the Employee object, the Address may also exist else where in the program.

Just as with Aggregation, Composition is where the component object is available to the entire composite object. This means that all the methods/functions of the composite object can access the component object. The only difference between Aggregation and Composition is that in Composition the component object only exists inside the composite object and no where else in the program. So when the composite object is destroyed, the given component object is also destroyed, and can not exist anywhere else. In your example, The Car is a composite object and the Engine is a component because that instance of the Engine only exists in that particular Car and no where else.



来源:https://stackoverflow.com/questions/21022012/difference-between-dependency-and-composition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!