Aggregation, Association and Composition [duplicate]
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?
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.
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.
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.
both aggregation and composition are associations. aggregation implies a whole/part relationship. composition is aggregation with a lifetime responsibility: http://ootips.org/uml-hasa.html
in your example, the answer is probably aggregation since there is a whole/part relationship.
normally your order would have line-items (as opposed to just products) and these line items would be considered composition.
@Bill the Lizard
Thank you for you explanation, but in your example of association there is still no reference/pointer in the one class to another...
I asked whether it's possible to have in class A field: B instanceOfB = new B() and tell that A and B are in Association (but not aggregation, nor composition!)
I am thinking about it, cause according what is said here it's not possible but... I found here: Difference between association, aggregation and composition such example:
[Example:]
|A|----------->|B|
class A
{
private:
B* itsB;
};
This is given as an example of association and there is pointer to some other class... (I guess we can more or less treat reference in Java class in the same way we treat this pointer here) So in Java it would look like:
[Example:]
|A|----------->|B|
class A
{
private:
B itsB;
};
does it mean we can have association in the example above in some cases? Is the difference in the way we think about classes? If A is car and B is wheel it would be aggregation, as car has wheel. If A is e.g. Person and B is e.g. Country it would be association, because Person doesn't have a country, but we maybe want to remember the name of the Country this person recently visited? Or if in class A there is something like "B itsB;" it means ALWAYS that this is aggregation or composition, no matter how we think about these classes?
Is my way of thinking correct or am I talking bullshit, which is highly probable, cause I am just guessing ;)
精彩评论