Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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;
     }
}
share|improve this question
    
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 Jan 11 '13 at 1:06
    
@svick Wikipedia shows both as varieties of composition –  Karthik T Jan 11 '13 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. –  Eric Lippert Jan 14 '13 at 1:26
add comment

3 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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