java - Aggregation, Association and Composition -


this question has answer here:

i have such simple example:

public class order  {   private arraylist<product> orders = new arraylist<product>();   public void add(product p)  {   orders.add(p);  } } 

is aggregation or composition? guess it's composition, because orders delated after delete of order, right? unfortunately task , answer different;/ 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 association between client , orders? teacher told me association, wondering why it's not aggregation/composition - told me aggregation or composition occur when 1 class contains few instances of different class - right? guess not, because e.g. car contains 1 wheel , aggregation guess?

what type of relation , why?

your first example aggregation. variable orders might deleted when order instance deleted, each product still has meaning , can exist outside order class.

you're right in second example. because client contains (has-a) reference orders, composition (because orders doesn't exist without client).

update address comment:

aggregation , composition both different types of association, they're specific types of association. in order 2 classes have association without aggregation or composition, need weaker link example given. here's (contrived) example:

class {     string phrase = "these pretzels making me thirsty.";      public string process(b b) {         // use b object         string tmp = b.dosomething(phrase);          // more processing...         return tmp;     } }  class b {     public string dosomething(string s) {         // input string , return         ...     } } 

here there no composition or aggregation (a not have it's own reference b object), since instance of b used by method in a, there association.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -