Aggregation, Association and Composition
java
Solution
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.
Problem
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?