Forum: Ruby Implementation of Aggregation and Composition

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
013264c787b7ef3c0f919017d4305b36?d=identicon&s=25 David B. (dendicus)
on 2009-01-06 12:25
Hello everybody,

I would like to implemente the "Aggregation" - as in
http://en.wikipedia.org/wiki/Aggregation_(object-o...) -
and the "Composition" - as
http://en.wikipedia.org/wiki/Object_composition. I hope thanks to this
be able to manage my objects such as my UML model. But I don't exacly
see the basic Ruby implementation of, and which could be the more sexy
way.

Thanks to any suggestions and help.

D.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-01-06 13:52
(Received via mailing list)
On 06.01.2009 12:25, David B. wrote:
> Thanks to any suggestions and help.
class Foo

   attr_accessor :aggregate
   attr_reader   :composite

   def initialize
     @composite = Part.new
   end

end

f = Foo.new
a = AnotherClass.new
f.aggregate = a

Cheers

  robert
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-06 15:05
David B. wrote:
> I would like to implemente the "Aggregation" - as in
> http://en.wikipedia.org/wiki/Aggregation_(object-o...) -
> 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).
A745f7d401d0a9bf80a5d5d94c961a05?d=identicon&s=25 Tiago Nogueira (Guest)
on 2009-01-06 16:34
(Received via mailing list)
Brian Candler escreveu:
>
> end
> u.faculty << d
> 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
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-01-06 20:12
Tiago Nogueira wrote:
> It's right. And , dont worry , but probaply  you can not implements some
> "design patterns" too.
> Am i right Brian?

Depends what you're talking about. Certainly some "design patterns" for
other languages are irrelevant in Ruby.
A745f7d401d0a9bf80a5d5d94c961a05?d=identicon&s=25 Tiago Nogueira (Guest)
on 2009-01-06 20:55
(Received via mailing list)
Brian Candler escreveu:
> Tiago Nogueira wrote:
>
>> It's right. And , dont worry , but probaply  you can not implements some
>> "design patterns" too.
>> Am i right Brian?
>>
>
> Depends what you're talking about. Certainly some "design patterns" for
> other languages are irrelevant in Ruby.
>
Exactly.The fact of some patterns be irrelevant in ruby is one of some
things that blow some minds for the people that are stating in ruby
language. It happened with me. Here in Brazil  Java haves too much
power  and when  i  decided to change to Ruby  it was hard. And , of
course , in Java we can implements some patterns that are totally
irrelevant in ruby because of  "ruby's /astonishments "/ :-)
- tiago
viva ruby
7995848b62bf9c645e859060ba1c856c?d=identicon&s=25 Panda Beer (pandabeer)
on 2009-01-06 21:38
Many thanks, Robert Klemme and Brian Candler! Now I see how to process!
=D
This topic is locked and can not be replied to.