Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between dependency and composition?

Tags:

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();
  }
}
like image 368
danihodovic Avatar asked Jan 09 '14 13:01

danihodovic


People also ask

What is the difference between dependency and composition?

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 is a special case of aggregation.

Is composition same as dependency injection?

Dependency Injection is a Design pattern that enables us to write loosely coupled code and enforces, dependent object gives up control of managing their dependencies and instead let a Composition Root inject the dependencies into them.

What is difference between dependency and association?

An association almost always implies that one object has the other object as a field/property/attribute (terminology differs). A dependency typically (but not always) implies that an object accepts another object as a method parameter, instantiates, or uses another object.

What is the difference between composition and aggregation?

1. Association between two objects that illustrate the “has-a” relationship is called Aggregation. A composition defines a part-of a relationship, and both the entities are connected to each other. 2.


2 Answers

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.

like image 66
meaning-matters Avatar answered Sep 21 '22 18:09

meaning-matters


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.

like image 27
TheLostMind Avatar answered Sep 20 '22 18:09

TheLostMind