5

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?

Johan
  • 74,508
  • 24
  • 191
  • 319
Bukocen
  • 51
  • 1
  • 1
  • 4

5 Answers5

6

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.

Community
  • 1
  • 1
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • Is there any chance that second example would be considered as association? I mean does such construct: Orders orders = new Orders(); mean that this is already aggregation? Does Association between class A and B exist only if we e.g. have method in A which takes the instance of B, but A doesn't have reference to object of class B? ( B object = new B() ). – Bukocen Dec 01 '10 at 14:28
  • @Bukocen: My reply was too long for a comment. See my edit to my answer. – Bill the Lizard Dec 01 '10 at 15:01
  • And in your edited example, there is a dependency between A and B, right? – Alex Turbin Dec 01 '10 at 21:46
  • @Alex: Yes, A depends on B to do part of its work. – Bill the Lizard Dec 01 '10 at 21:51
  • the second example in the question should be composition right? because when Client dies the Order's object dies as well. – user20358 Sep 26 '12 at 14:35
2

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.

Perception
  • 79,279
  • 19
  • 185
  • 195
RamjeeAnna
  • 21
  • 1
1

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.

lalit
  • 1,485
  • 2
  • 17
  • 32
  • Hmm i what if we kill instance of Order on purpose? Then we loose the reference to it's fields (an also to "orders"...) even if it would still exist in memory isn't it the same as the kill of all Products (from the programmer's point of view? - as we can't refere to them anymore) – Bukocen Dec 01 '10 at 13:45
  • I believe you meant using the inner object to be declared in another (object) object as an ordinary class/struct member, i.e. not a pointer nor reference. In fact if you allocate memory for the outer object on the heap (malloc/new) all their members will live with it on the heap too, but you still don't need to manage their life cycle by hand and their occupy single, continuous memory block. – Przemek Kryger Dec 01 '10 at 13:48
  • 1
    @Bukocen That's true, however you may have `Product`s, that were members of `Order`, referenced from somewhere else, hence their life cycle is not tied to the `Order`'s one. What @lalit meant was using another type of defining object in C/C++ that will tie members life cycle to the defining class/struct. – Przemek Kryger Dec 01 '10 at 13:52
  • @Przemek you are correct. What I mean is that the inner objects are ordinary class/members. Than it does not matter if it is on stack or on heap. Thanks for clarifying further. – lalit Dec 01 '10 at 14:11
1

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.

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
0

@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 ;)

Community
  • 1
  • 1
Bukocen
  • 51
  • 1
  • 1
  • 4
  • If class A has an instance of class B, the it is either composition or aggregation. My example was to show that you can have association without having either. Notice how my class A does not store an instance of class B anywhere, it just uses the one passed to it. – Bill the Lizard Dec 01 '10 at 22:05
  • Yes, I understand that it doesn't store an instance of class B... but does it mean that the example which was fiven in that topic is not really association but aggregation? (I mean the one with B* itsB) ? Its a pointer so it can store (well, ok - point) instance. – Bukocen Dec 02 '10 at 09:08