Advertisement
saasbook

Fixing OCP with abstract factory

Oct 15th, 2012
983
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.46 KB | None | 0 0
  1. class Report
  2.   def output
  3.     formatter =
  4.       case @format
  5.       when :html
  6.         HtmlFormatter.new(self)
  7.       when :pdf
  8.         PdfFormatter.new(self)
  9.         # ...etc
  10.       end
  11.   end
  12. end
  13.  
  14. class Report
  15.   def output
  16.     formatter_class =
  17.       begin
  18.         @format.to_s.classify.constantize
  19.       rescue NameError
  20.         # ...handle 'invalid formatter type'
  21.       end
  22.     formatter = formatter_class.send(:new, self)
  23.     # etc
  24.   end
  25. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement