I'm looking at composition. What does it mean to 'store a reference to a component'?
To me this means that there will be two classes, and that one class will be instantiated from within the other class. Is this correct? How else could you represent a compositional relationship?
How would this particular example be different from an aggregation? (or would that require an object storing reference to instantiated classes created out of it's scope..)
i.e. (Ruby syntax)
class A
def initialize
#stuff
end
end
class B
def initialize
@many_a_instances = []
end
def attach_an_A
@many_a_instances << A.new
end
end
Would it be a better to define class A within class B?
In light of thunderous enthusiasm from would be question answerers... the answer is this (based on conversation with colleagues and friends):
How you define classes is somewhat independent of the relationship between instances of different classes (which is a dependent on how classes are instantiated).
If an instance defines a reference to an instance, then the referenced instance's lifespan is dependant on the object that holds the reference. This defines a compositional relationship.
However, if you instantiated an object and then passed a reference to that object to another class, that would be an example of aggregation. Because the lifespan of the two instantiated objects is independent from each other, but one object still references the other.
So same classes, but different relationships between instances. and in terms of where classes are defined, that will probably be use-case specific.
Any better answer will be marked as accepted over this one :)