0

I'm confused between aggregation and containment. I'm wondering if the following represent an aggregation or containment?

class Auto 
{

   private string model;
   private int speed;

     class AutoCustomer
     {
        public string LastName;
        public string Address;
        public DateTime DateOfPurchase;
     }
}
3
  • This is a nested class, which (at least in your code) has nothing to do with aggregation or containment. Also, did you mean “composition” instead of “containment”?
    – svick
    Commented Jan 11, 2013 at 1:06
  • @svick Wikipedia shows both as varieties of composition
    – Karthik T
    Commented Jan 11, 2013 at 1:19
  • 3
    A block of concrete is an aggregation of cement and gravel. A bowl is a container of fruit. An auto is not made out of customers, and does not contain customers, so the answer to your question is "no". It is an example of neither. Commented Jan 14, 2013 at 1:26

3 Answers 3

1

The way to understand aggregation versus containment is association versus ownership. Does a car "have" an engine? Yes, it wouldn't be a car without it (in most situations). Does a fruit bowl "have" fruit? Yes; even if there are currently zero fruit in the bowl.

Does a car "have" a customer? No. The dealership "has" a customer—maybe—and the dealership "has" cars. The dealership may wish to associate a customer with a car they're interested in, but the car doesn't own that customer.

1

A good litmus test for aggregation vs. composition is asking yourself if you would delete the child-object when you delete the parent-object.

When you don't need the Auto anymore, do you still need the AutoCustomer it references? When you do, you have a case of aggregation. When you don't need it, you have a case of composition.

0

If your class Auto is going to have a reference to the class AutoCustomer, then this represents an example of Aggregation, since the Auto class doesnt OWN the AutoCustomer instance.

But it is sketchy.. since the data members of AutoCustomer are so specific that it might imply ownership after all, which means its just vanilla composition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.