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.
-
1what 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 compilergnat– gnat2017年03月14日 14:13:20 +00:00Commented Mar 14, 2017 at 14:13
-
2...see also: Aggregation vs Compositiongnat– gnat2017年03月14日 14:19:21 +00:00Commented Mar 14, 2017 at 14:19
-
1Also, similar answers over on SO: What is the difference between aggregation, composition and dependency? or Difference between association, aggregation and compositionBen Cottrell– Ben Cottrell2017年03月14日 14:23:17 +00:00Commented 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.user3396486– user33964862017年03月14日 14:26:42 +00:00Commented Mar 14, 2017 at 14:26
-
why didn't you use tag c#?gnat– gnat2017年03月14日 19:39:04 +00:00Commented Mar 14, 2017 at 19:39
1 Answer 1
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
.
-
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– user33964862017年03月14日 15:16:15 +00:00Commented Mar 14, 2017 at 15:16