views:

31

answers:

1

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?

+1  A: 

I suggest being careful not to fall into what used to be called "paralysis by analysis". Patterns are useful, right up to the point where they make doing what you need to do more difficult. In the words of Wayne's World, "I say hurl!" - or more specifically, hurl the array of objects to YAML and get your output. To paraphrase badly, "If your code spews and you get your output, you're golden. But if the pattern gets you nothing, it was never meant to be".

Look - patterns are one of the Great Ideas - they provide practical guidance on how to handle common situations which are often handled badly. They can help make code better. They can help make code (dare I say it?) beautiful. But, y'know, there's times when you have to roll up your sleeves, hold your nose, and just do what it takes to get the job done. Besides, if you "just do it" you may gain insight on how to "just do it better" - and in the meantime you'll have something that works.

Party on...

Bob Jarvis
Great response. I ended up making another method in the Builder that gets called last. In one in does nothing, in the other it "Hurls." Haha. It worked out, thanks for the words of wisdom.
Mike