Wednesday, July 26, 2006

Ruby Composite Pattern

I have found this intresting implementation of the composite pattern based on the missing_method ruby structure...

class Quelindo
def
method_missing(method_id, *args)
puts "Hee hola, no hay ningun metodo que se llame #{method_id}"
end
end

# implementemos esto para ver como va...
c=Quelindo.new
c.sarasa

class Leaf
def scale(factor)
end

def addChild(kid)
# do nothing or raise an exception
end

def getKid( index)
# do nothing or raise an exception
nil
end
end


class Composite
def initialize
@kids = []
end
def
method_missing(method_id, *args)
@kids.each do kid
kid.send(method_id, *args)
end
end
def addChild( kid)
@kids <<>


# heee bueno esta es la implementacion
c = Composite.new
c.addChild("Hola")
c.addChild(Composite.new)
a = Leaf.new
c.addChild(a)
puts c.getChild(1)

1 comment:

Anonymous said...

Your are Excellent. And so is your site! Keep up the good work. Bookmarked.
»