I have an instance of the Builder pattern in my project right now. Currently, the supported output format is CSV, however I would now like to include YAML. Easy, I thought. I have all of the supporting code in to make the type change.
I'm finding myself to be in a bit of a complex. The intent of using the Builder pattern was to construct the output file step by step. To me, this seems directly contradictory to YAML - Get all of your objects into an array and call YAML::dump().
The good news is that I do have an array of those objects. It gets passed into the Director. Here's a snippet from the construct() method of the Director.
def construct(aDataAccessObjectCollection)
@daos = aDataAccessObjectCollection
result = ''
@daos.each do |dao|
@builder.build_food_name(dao.get_property('name'))
@builder.build_calories(dao.get_property('calories'))
@builder.build_keywords(dao.get_property('keywords'))
result += (@builder.get_result + "\n")
@builder.flush
end
File.open(@file_name, 'w') do |file|
file.write(result)
end
end
I'm not sure how to be able to accommodate for both the CSV and YAML formats. Any ideas?