4

Please excuse the poor example/analogy, I'm only interested in the code sample.

I have a Dinner_Chair class (inherited from Chair class). It is as follows.

Dinner_Chair = class(Chair)
 Private
 theUser: Person;
 Public
 Dinner_Chair()
 {
 Back = new Back();
 Seat = new Seat();
 }
End

I also have a simple Person class, that as you can see, is associated with the Dinner_Chair class.

My question is this. Because the Person class is not instantiated in the Dinner_Chair class, is this an example of aggregation?

This is to consolidate my understanding of entry level OOP relationships.

FrustratedWithFormsDesigner
46.3k7 gold badges130 silver badges176 bronze badges
asked Mar 14, 2017 at 14:09
5
  • 1
    what programming language is this? I ask because the answers would heavily depend on that: in some languages code like this can simply be blocked by compiler Commented Mar 14, 2017 at 14:13
  • 2
    ...see also: Aggregation vs Composition Commented Mar 14, 2017 at 14:19
  • 1
    Also, similar answers over on SO: What is the difference between aggregation, composition and dependency? or Difference between association, aggregation and composition Commented Mar 14, 2017 at 14:23
  • yes - the design principle is well documented. However, my question pertains to actual simple implementation of aggregation, and in C#. Post links are useful, but are indirectly addressing question. Commented Mar 14, 2017 at 14:26
  • why didn't you use tag c#? Commented Mar 14, 2017 at 19:39

1 Answer 1

2

In code terms it could be aggregation but not necessarily. In the real world of dining chairs and people/diners, the relationship isn't aggregation because both objects can exist independently of each other.

The differentiation with regards to the code sample is whether a Dinner_Chair is a valid object without a Person.

Aggregation implies the Person is a required dependency of Dinner_Chair - i.e. Dinner_Chair isn't valid without a Person.

Association implies the Person is an optional dependency of Dinner_Chair - i.e. Dinner_Chair is valid without a Person.

answered Mar 14, 2017 at 14:52
1
  • Interesting. I missed that 'key' idea you've highlighted! Excellent. In my code sample, it supposes there must be a person associated with the dinner_chair, that is, upon dinner_chair instantiation. Because I have a separate Person class - and I may instantiate a new Person object at will - I guess this infers that it must be aggregation. Yes, this is not realistic, but I can see now that the dinner_chair requires a person, before it can be instantiated. Commented Mar 14, 2017 at 15:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.