Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Implementation of Aggregation and Composition

Reply
Thread Tools

Implementation of Aggregation and Composition

 
 
David B.
Guest
Posts: n/a
 
      01-06-2009
Hello everybody,

I would like to implemente the "Aggregation" - as in
http://en.wikipedia.org/wiki/Aggrega...ed_programming) -
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      01-06-2009
On 06.01.2009 12:25, David B. wrote:
> Hello everybody,
>
> I would like to implemente the "Aggregation" - as in
> http://en.wikipedia.org/wiki/Aggrega...ed_programming) -
> 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.


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

--
remember.guy do |as, often| as.you_can - without end
 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      01-06-2009
David B. wrote:
> I would like to implemente the "Aggregation" - as in
> http://en.wikipedia.org/wiki/Aggrega...ed_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).
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Tiago Nogueira
Guest
Posts: n/a
 
      01-06-2009
Brian Candler escreveu:
> David B. wrote:
>
>> I would like to implemente the "Aggregation" - as in
>> http://en.wikipedia.org/wiki/Aggrega...ed_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

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      01-06-2009
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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Tiago Nogueira
Guest
Posts: n/a
 
      01-06-2009
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

 
Reply With Quote
 
Panda Beer
Guest
Posts: n/a
 
      01-06-2009
Many thanks, Robert Klemme and Brian Candler! Now I see how to process!
=D
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Aggregation vs. composition Rick Osborn Java 11 08-31-2009 08:45 AM
Composition versus Implementation Inheritance chsalvia@gmail.com C++ 8 07-29-2007 07:28 PM
Association, Composition, Aggregation Salman C++ 4 02-21-2007 09:07 PM
aggregation vs. composition Gary Wessle C++ 2 11-26-2006 03:45 PM
composition/aggregation: would like to use constructor body rather than initializer list Chris K C++ 1 04-17-2004 08:38 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57