aggregation vs composition

Aggregation(collection) differs from ordinary composition in that it does not imply ownership. In composition, when the owning object is destroyed, so are the contained objects. In aggregation, this is not necessarily true


Composition(mixture) is a way to combine simple objects or data types into more complex ones. Compositions are a critical building block of many basic data structures



Both denotes relationship between object and only differ in their strength.



UML notations for different kind of dependency between two classes



Composition : Since Engine is part-of Car, relationship between them is Composition. Here is how they are implemented between Java classes.

 public class Car {

    //final will make sure engine is initialized
    private final Engine engine;  

    public Car(){
       engine  = new Engine();
    }
}

class Engine {
    private String type;
}

Aggregation : Since Organization has Person as employees, relationship between them is Aggregation. Here is how they look like in terms of Java classes
public class Organization {
    private List employees;
}


public class Person {
    private String name;   
}

Comments

Popular posts from this blog

Conversion from one file format to other in Apache Spark

How to use (inner) JOIN and group by in apache spark SQL.

Problem: Find top rated movie using HIVE and store the result to HDFS