Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregation, Association and Composition [duplicate]

Tags:

java

I have such a simple example:

public class Order 
{

 private ArrayList<Product> orders = new ArrayList<Product>();

 public void add(Product p)
 {
  orders.add(p);
 }
}

Is it aggregation or composition? I guess it's composition, because orders will be delated after delete of Order, right? Unfortunately it was a task and answer was different;/ Do you know why?

second problem:

public class Client extends Person 
{
    String adress = "";

    Orders orders = new Orders();

    public Client(String n, String sn)
    {
     name = n;
     surName = sn;
    }

    public String getAddress()
    {
     return adress;
    }
    public Orders getOrders()
    {
     return this.orders; 
    }
}

Is it Association between Client and Orders? My teacher told me that this is association, but I was wondering why it's not a aggregation/composition - he told me that aggregation or composition occur only when one class contains few instances of different class - is that right? I guess not, because e.g. car contains ONE wheel and its aggregation I guess?

What type of relation is that and why?

like image 987
Bukocen Avatar asked Dec 01 '10 13:12

Bukocen


3 Answers

Your first example is aggregation. The variable orders might be deleted when the Order instance is deleted, but each Product still has meaning and can exist outside the Order class.

You're right in your second example. Because Client contains a (has-a) reference to Orders, this is composition (because orders doesn't exist without a Client).

Update to address your comment:

Aggregation and composition are both different types of association, but they're specific types of association. In order for two classes to have just an association without aggregation or composition, they need a weaker link than the example given. Here's a (contrived) example:

class A {
    String phrase = "These pretzels are making me thirsty.";

    public String process(B b) {
        // use a B object to do something
        String tmp = b.doSomething(phrase);

        // do more processing...
        return tmp;
    }
}

class B {
    public String doSomething(String s) {
        // do something with the input string and return
        ...
    }
}

Here there is no composition or aggregation (A does not have it's own reference to a B object), but since an instance of B is used by a method in A, there is an association.

like image 174
Bill the Lizard Avatar answered Nov 15 '22 06:11

Bill the Lizard


I think, the first example is not aggregation but composition. Because here Order composes the Product. If order is deleted then product will be deleted automatically.

class Product;

class Factory {
    Product *product;
    product = new Product();
    product createProduct() {
        return new(product);
    }
};

class Product {
     ArrayList<Order> *orders = new ArrayList<Order>();

     void createOrder() {
         orders.add(OrderInfomation);
     }
}

class Order {
    string orderID;
    string orderName;
    Customer customer;
};

class Customer {
    string cusID;
    string cusName;
    string cusAddress;
};

Here Product can have same order type. And if product is deleted, then order will be deleted. So it is a composition. (highly strongly coupled or death relation)

In your case, Order associates with product. One order can have any product order. So it is a association. If a order is deleted then a product will exists. (lightly coupled) Similarly customer and order has association relationship.

Factory and Product are aggregated. Even product deleted, Factory will exist.

like image 34
RamjeeAnna Avatar answered Nov 15 '22 05:11

RamjeeAnna


In Java truly speaking there is no composition, everything is an aggregation. The composition can exist in language like C++ where the objects can be declared on stack and live and die with the parent object. In Java, everything lives on heap.

For your second question, Order has an aggregation relationship with Product. The association relationship can be when we pass order as an argument to the methods in product. Product does the job with the passed reference but does not caches that reference in some child reference.

like image 30
lalit Avatar answered Nov 15 '22 07:11

lalit