I would like to implemente the “Aggregation” - as in Object composition - Wikipedia -
and the “Composition” - as Object composition - Wikipedia. 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.
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).
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?
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 "/