Friday 21 March 2014

Association, Aggregation and Composition

These are the OOP concepts having some similarities and differences representing Has-A-Relationship. Here we try to describe these in details.

Association

Definition: “When an object has a reference (Pointer) to another object without dependencies”.
Or you can say it is just a link between two objects. In association initialization of referenced object is not performed.
This relationship has following properties.
1.       No parent or child object.
2.       Each object has its own separate lifecycle.
3.       Objects has no dependency on each other.
4.       Represented by just reference (pointer). Like a link.
Here are the types of association
1.       Unidirectional association
2.       Bidirectional association
3.       Composition
4.       Aggregation

Unidirectional association

When we have reference from one object to another object like customer has order. Customer has just reference to order (pointer in c++). Here customer and order are not dependent on eachother, They have their own dependencies.
/**
 * @author Abdul-Rehman | Mar 20, 2014
 *
 *         This class represents a simple Order which shows unidirectional
 *         association from @Customer and @Order
 */
public class Order {

}
/**
 * @author Abdul-Rehman | Mar 20, 2014
 *
 *         This class represents a simple customer which shows unidirectional
 *         association between @Customer and @Order
 */
public class Customer {

        @SuppressWarnings("unused")
        /**
         * Here we have just reference to order class And we can traverse from @customer
         * to @Order
         */
        private Order order;
}

Bidirectional association

When we have bidirectional reference b/w objects like customer has order and one order can be associated to any other customer. Customer and order are not dependent on each other, they have their own dependencies.
/**
 * @author Abdul-Rehman | Mar 20, 2014
 *
 *         This class represents a simple customer which shows unidirectional
 *         association between @Customer and @Order
 */
public class Customer {

       @SuppressWarnings("unused")
       /**
        * Here we have just reference to order class And we can traverse from @customer
        * to @Order
        */
       private Order order;
}

/**
 * @author Abdul-Rehman | Mar 20, 2014
 *
 *         This class represents a simple Order which shows bidirectional
 *         association from @Order to @Customer and @Customer to @Order
 */
public class Order {
        @SuppressWarnings("unused")
        /**
         * Here we have just reference to customer class And we can traverse from  @Order to @customer
         */
        private Customer customer;

Order() {
                orderId++;
                System.out.println("Order with ID = "+orderId+" is saved");
        }

        public int getOrderId() {
                return this.orderId;
        }
}

Aggregation

Definition: “When an object has a reference to another object and the referenced Object has its own lifecycle and dependencies”.
In Aggregation referenced object is not initialized, it is just reference to another initialized object.
This relationship has following properties.
1.       Owner and child relationship.
2.       Child object has its own separate lifecycle.
3.       More than one owner objects can have reference to a same child object.
4.       Child object is not garbage collected if parent is deleted.
5.       Child is initialized outside the owner object.
Example: In example customer and manager have reference to a same order object (child object).If the customer, manager or both objects are deleted then there is no effect on the order object. This type of relationship is called AGGREGATION. In AggregationTest class sample test for aggregation relationship is performed and result is something like this.
Order with ID = 1 is saved
Customer with ID = 1 and Order ID = 1 is Registered
Manager with ID = 1 and Order ID = 1 is Registered
Here we see that the Both Customer and Manager have reference to a single order
Source Code
/**
 * @author Abdul-Rehman | Mar 20, 2014
 *
 *         This class represents a simple customer which shows unidirectional
 *         association between @Customer and @Order
 */
public class Customer {
        /**
         * Here we have just reference to order class And we can traverse from @customer
         * to @Order Here Order can not be initialized because in aggregation it is
         * reference to an independent object
         */
        private Order order = null;
        private static int customerID = 0;

        public Customer(Order order) throws Exception {
                if (order != null) {
                        this.order = order;
                        customerID++;
                        System.out.println("Customer with ID = " + customerID
                                        + " and Order ID = " + order.getOrderId()
                                        + " is Registered");
                } else {
                        throw new Exception("Customer is not initialized");
                }
        }

        public Order getOrder() {
                return this.order;
        }

        public int getCustomerId() {
                return customerID;
        }
}
/**
 * @author Abdul-Rehman | Mar 21, 2014
 *
 *         This class represents a manager. Which has (aggregation)reference to @Order
 */
public class Manager {
        /**
         * Here Order can not be initialized because in aggregation it is reference
         * to an independent object
         */
        private Order order = null;
        private static int managerID = 0;

        public Manager(Order order) throws Exception {
                if (order != null) {
                        this.order = order;
                        managerID++;
                        System.out.println("Manager with ID = " + managerID
                                        + " and Order ID = " + order.getOrderId()
                                        + " is Registered");
                } else {
                        throw new Exception("Customer is not initialized");
                }
        }

        public Order getOrder() {
                return this.order;
        }

        public int getManagerId() {
                return managerID;
        }

}
/**
 * @author Abdul-Rehman | Mar 21, 2014
 *
 *         This class is sample class to test aggregation relationships
 */
public class AggregationTest {

        /**
         * This is main method which is used for testing purpose
         *
         * @param args
         */
        public static void main(String[] args) {
                try {
                        Order order = new Order();
                        Customer customer = new Customer(order);
                        Manager manager = new Manager(order);
                        System.err
                                        .println("Here we see that the Both Customer and Manager have refernce to a single order");
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

}

Composition

Definition: “When an object has a reference to another object and the referenced Object is completely dependent on its owner object”.
We can say that the child object is only and only part of its owner object, it cannot be reused.
In Composition the initialization of referenced (child) object is mandatory. The composed object is initialized inside its owner object.
This relationship has following properties.
1.       Owner (component) and child (composed) relationship.
2.       The lifecycle of child object is dependent on its owner object.
3.       Child object is garbage collected if parent is deleted.
4.       Child is initialized inside the owner object.
Example: In example customer and manager have reference to a different order objects (child object). Here we see that the Both Customer and Manager have reference to a different order objects. If manager or customer is deleted the specific order object referenced by specific owner is also deleted. This type of relationship is called Composition. In CompositionTest class sample test for composition relationship is performed and result is something like this.
Order with ID = 1 is saved
Customer with ID = 1 and Order ID = 1 is Registered
Order with ID = 2 is saved
Manager with ID = 1 and Order ID = 2 is Registered
Here we see that the Both Customer and Manager have reference to a different order objects. If manager or customer is deleted the specific order object referenced by specific owner is also deleted

Source Code
public class Manager {
        /**
         * Here Order is initialized because in Composition it is reference
         * To an dependent object
         */
        private Order order = null;
        private static int managerID = 0;

        public Manager() throws Exception {
                try {
                        this.order = new Order();
                        managerID++;
                        System.out.println("Manager with ID = " + managerID
                                        + " and Order ID = " + order.getOrderId()
                                        + " is Registered");
                } catch(Exception e) {
                        throw new Exception("Customer is not initialized"+e.toString());
                }
        }

        public Order getOrder() {
                return this.order;
        }

        public int getManagerId() {
                return managerID;
        }

}
public class Customer {
        /**
         * Here Order is initialized because in Composition it is reference
         * To an dependent object
         */
        private Order order = null;
        private static int customerID = 0;

        public Customer() throws Exception {
                try {
                        this.order = new Order();
                        customerID++;
                        System.out.println("Customer with ID = " + customerID
                                        + " and Order ID = " + order.getOrderId()
                                        + " is Registered");
                } catch (Exception e) {
                        throw new Exception("Customer is not initialized"+e.toString());
                }
        }

        public Order getOrder() {
                return this.order;
        }

        public int getCustomerId() {
                return customerID;
        }
}
public class CompositionTest {

        /**
         * This is main method which is used for testing purpose
         *
         * @param args
         */
        public static void main(String[] args) {
                try {
                        Customer customer = new Customer();
                        Manager manager = new Manager();
                        System.err
                                        .println("Here we see that the Both Customer and Manager have "
                                                        + "refernce to a different order objects. If manager or customer "
                                                        + "is deleted the specific order oject refernced by that owner is "
                                                        + "also deleted");
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

}

Logical Difference


Association
Aggregation
Composition
Relationship
Link
Owner-Child
Owner-Child
dependencies
No
No
Child depends on owner
Life cycle
Each object has independent lifecycle
Owner and child object has independent lifecycle.
If owner dies child does not
If owner dies child also dies.
Child only belongs to specific owner
Access
Any object can access anyone
More than one owners can access same child
Owner has all rights on its child


Source Code: You can download source code from here and test the example.