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,105
  • 7
  • 126
  • 176
user3396486
  • 401
  • 4
  • 9
  • 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 – gnat Mar 14 '17 at 14:13
  • 2
    ...see also: [Aggregation vs Composition](http://softwareengineering.stackexchange.com/a/61527/31260) – gnat Mar 14 '17 at 14:19
  • 1
    Also, similar answers over on SO: [What is the difference between aggregation, composition and dependency?](http://stackoverflow.com/questions/1644273/what-is-the-difference-between-aggregation-composition-and-dependency) or [Difference between association, aggregation and composition](http://stackoverflow.com/questions/885937/difference-between-association-aggregation-and-composition) – Ben Cottrell Mar 14 '17 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. – user3396486 Mar 14 '17 at 14:26
  • why didn't you use tag [tag:c#]? – gnat Mar 14 '17 at 19:39

1 Answers1

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.

Ben Cottrell
  • 11,656
  • 4
  • 30
  • 41
  • 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. – user3396486 Mar 14 '17 at 15:16