Re: Implementation of Aggregation and Composition



Brian Candler escreveu:
David B. wrote:

I would like to implemente the "Aggregation" - as in
http://en.wikipedia.org/wiki/Aggregation_(object-oriented_programming) -
and the "Composition" - as


Wikipedia says that Composition is where a structure directly includes
its members, whereas Aggregation is where a structure only contains
references to those members.

This distinction doesn't apply in Ruby; everything is a reference, so
Aggregation is your only option.

class Professor; end

class Department
attr_accessor :members
def initialize
@members = []
end
end

class University
attr_accessor :faculty
def initialize
@faculty = []
end
end

u = University.new
d = Department.new
u.faculty << d
p = Professor.new
d.members << p

Neither the arrays nor their elements are 'directly contained'; rather,
references to those objects are stored. For example:

@faculty
University ------------> Array ---------> Department(1)
'---------> Department(2)

If you no longer hold a reference to the University anywhere, then it
will eventually be garbage collected. If this means that nothing else
holds a reference to that particular Array, then that will be
garbage-collected too. Ditto for the Departments, if there were no other
references to them apart from those in the Array.

Arrays are untyped collections. Ruby won't enforce that
University#faculty may only contain Department objects, nor that each
Department may only be referenced from a single University (as the UML
shows).

It's right. And , dont worry , but probaply you can not implements some
"design patterns" too.
Am i right Brian?
- tiago nogueira

.



Relevant Pages

  • Re: Implementation of Aggregation and Composition
    ... references to those members. ... Aggregation is your only option. ... references to those objects are stored. ... Arrays are untyped collections. ...
    (comp.lang.ruby)
  • Re: When A Have a Collections of Bs -- a Design Question
    ... Robert C. Martin wrote: ... >>class Product { ... > *might* be aggregation, or it might just be association. ... Product.removePartmethod release all the references to the part and ...
    (comp.object)
  • Re: A Question about Domain Object
    ... says domain objects should know how to recognize which references indicate aggregation and which ones indicate association. ... Objects should abstract /intrinsic/ properties from the problem space entity that do not depend on the context. ... // implement & instantiate aggregation ... One reason is that one can easily "daisy-chain" relationship paths by "walking" a chain of references to get to the right object. ...
    (comp.object)
  • Re: Arrarys of reference
    ... to initialize the references correctly. ... Maybe because arrays of references often couldn't be ... | Register DCSR; ...
    (comp.lang.cpp)
  • Re: complex data structure
    ... > Jeff wrote: ... > is really the structure you want (scalar references to array references ... > dumped the keys/values to that hash. ... I had really wanted to push arrays on $hash ...
    (comp.lang.perl.misc)