Association, Aggregation, and Composition with example




Most confusing topics of java are association, aggregation, and composition. In this post, we will try to understand three important concepts: association, aggregation, and composition.

The association relationship is a way of that a class holds a reference to another class. The relationship can be bi-directional with each class holding a reference to the other, but they have their own object life time. In this relationship no one is owner. They can exist without each other.

Aggregation is a special type of association. The aggregate class contains a reference to another class and is owner of this relationship. It is whole-part relationship. Each class referenced is considered to be part-of the aggregate (whole) class. The referenced class objects can not belong to any other object.

What do mean ownership?

If a class X contain reference of class Y and again class Y contain reference of class X then no clear ownership can be determined and the relationship is simply association.

If a class X contain reference of class Y and again class Y has no association with another object then class X has ownership and the relationship is aggregation.



Example of Association

/**

 *

 * @author Navindra Jha

 */

public class Husband {

    private Wife wife;

    private String name;

    public void setWife(Wife wife) {

        this.wife=wife;

    }

    public Husband() {

     

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getWifeName() {

        return wife.getName();

    }

    //other methods

}

public class Wife {

    private Husband husband;

    private String name;

    public Wife() {

     

    }

    public void setHusband(Husband husband) {

        this.husband = husband;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getHusbandName() {

        return husband.getName();

    }

    //other methods

}

public class Main {

    public static void main(String[] args) {

      //condition I

    

        Wife wife=new Wife();

        wife.setName("Sita");

        Husband hub=new Husband();

        hub.setWife(wife);

        String wname=hub.getWifeName();

        System.out.println(wname);

    

        //condition II

      /*  Husband hub=new Husband();

        hub.setName("Ram");

        Wife wife=new Wife();   

        wife.setHusband(hub);

        String hname=wife.getHusbandName();

        System.out.println(hname);

      */

    }

}

Both classes have their own life time that means if object of one class is deleted no necessary associated object of another class is also deleted. They are independent to each other.

 Both use each other not one of them is owner.

Example of Aggregation

import java.util.ArrayList;

import java.util.List;

/**

 *

 * @author Navindra Jha

 */

public class Student {

    private String name;

    ArrayList<Address> list=new ArrayList<Address>();

    Student() {

     

    }

    List<Address> getAddress(){

        return list;

    }

    void setAddress(Address address){

        list.add(address);

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

 

}

class Address {

    private String city;

    private String state;

    public String getCity() {

        return city;

    }

    public void setCity(String city) {

        this.city = city;

    }

    public String getState() {

        return state;

    }

    public void setState(String state) {

        this.state = state;

    }

    //other methods

}

class Main {

    public static void main(String as[]) {

        Address address1 = new Address();

        Address address2 = new Address();

        address1.setCity("New Delhi");

        address1.setState("Delhi");

        Student student = new Student();

        student.setAddress(address1);

        address2.setCity("Laxmi Nagar");

        address2.setState("Delhi");

        student.setAddress(address2);

        List<Address> list=student.getAddress();

        for (Address address : list) {

            System.out.println(address.getCity()+", "+address.getState());

        }

    }

}

Whole-part relationship between Student (whole) and Address (part).

Student class is owner of this relationship. But both classes have their own life time that means if object of one class is deleted no necessary associated object of another class is also deleted. They are independent to each other.

Composition takes the Aggregation relationship one step further by ensuring that the containing object is responsible for the lifetime of the object it holds. If Object Y is contained within Object X, then Object X is responsible for the creation and destruction of Object Y. In other words, if Parent goes for garbage collection then Child also has to be garbage collected.

Example of Composition:

public class Parent {

private Child child;

     Parent(Child child) {

        this.child = child;

    }

    //other methods

}

class Child{

 

}

class Main{

    public static void main(String as[]){

        Child child=new Child();

        Parent parent=new Parent(child);

    }

}

Whole-part relationship between Parent (whole) and Child (part).

Parent class is owner of this relationship. But if object of Parent class is deleted also object of Child class is deleted. Child class has no independent life time.


No comments:

Post a Comment