Hi everyone,
as you know I started a few days ago the Ruby Patterns Wiki and now I
am starting to put in some samples. As you know, although I am not
new to software development, I am still a newbie in Ruby.
I was thinking about the Factory Method Pattern and following came to
my mind. If we follow the Pattern by the book (GoF) we would have
following code (simplified for the purpose of clarity):
class Product
def hi
“hello there”
end
end
class ConcreteProduct < Product
def hi
“Hello from the concrete implementation”
end
end
class Creator
def create_product
end
end
class ConcreteCreator < Creator
def create_product
ConcreteProduct.new
end
end
Obviously this code strikes in Ruby (specially the creator class that
doesn’t make much sense to me). How would you people think this could
be implemented in Ruby? Or better to say, what is the Ruby way to
solve this intent of the Factory Method Pattern: Define an interface
for creating an object, but let subclasses decide which class to
instantiate. Factory Method lets a class defer instantiation to
subclasses.
Cheers,
Enrique Comba R.